What call search() Client API function of autocomplete ?

UI Components for JSF
Post Reply
schlebe
Posts: 212
Joined: 25 Sep 2015, 13:32
Location: Colmar-Berg (Luxembourg)

11 Feb 2016, 12:32

Recently, I have written an OnKeyDown handler on a <p:autocomplete> widget to intercept the ARROW-DOWN key.

My XHMTL code is like

Code: Select all

<p:autoComplete 
    id="SearchFormBrand"
    size="25"        
    immediate="true"
    value="#{postLabListViewController.postLabellingSearchCriteria.brand}"
    completeMethod="#{postLabListViewController.searchFormBrandAutoCompleteItems}"
    minQueryLength="1"
    dropdown="true"
    onkeydown="AutoCompleteOnKeyDown(this, event);"
    >
The function that implement OnKeyDown handler is

Code: Select all

function AutoCompleteOnKeyDown(oField, e)
    {
    if (e.keyCode == 40) // Pressing on ARROW-DOWN
        {
        if (oField.value === "")
            {
            oField.search("*");    
            }
        }
    }
The AutoCompleteOnkeyDown event is correctly called, but the search() function do nothing !

What function is called on server ?

I will that search() function call the completMethod() function defined in <p:autocomplete> to open a listbox with all choice possible.
PrimeFaces 6.2.4
Mojarra 2.2.13
Glassfish 4.1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

11 Feb 2016, 12:35

Learn about PF widgets and widgetVar ;)
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

schlebe
Posts: 212
Joined: 25 Sep 2015, 13:32
Location: Colmar-Berg (Luxembourg)

11 Feb 2016, 13:28

Your response doesn't help me.

On Netbeans 8, the search() method on oField is accepted by "intellisense", so I think that my object oField represent the widget !

Can you give me an orientation because
Learn about PF widgets and widgetVar
is not enough to help me.

There is nothing in Primefaces documentation that explain correclty theses concepts when it is used in a JavaScript ;)
PrimeFaces 6.2.4
Mojarra 2.2.13
Glassfish 4.1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

11 Feb 2016, 13:41

It's really so hard to google about "PrimeFaces widget"?....

I'm sure that NetBeans doesn't provide intellisense for PF widgets ;)

JS API can obly be accessed by the widget instance, not the html element.
Therefore every component wich has a JS API, also have a "widget", which can be accessed by the widgetVar.

http://blog.hatemalimam.com/intro-to-pr ... widgetvar/
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

schlebe
Posts: 212
Joined: 25 Sep 2015, 13:32
Location: Colmar-Berg (Luxembourg)

12 Feb 2016, 09:24

tandraschko wrote: I'm sure that NetBeans doesn't provide intellisense for PF widgets ;)
I'm sure that NetBeans has proposed a search() function just after typing "oField."

BUT, I agreee with you and it is certainly a search() method distinct of what I will !
PrimeFaces 6.2.4
Mojarra 2.2.13
Glassfish 4.1

schlebe
Posts: 212
Joined: 25 Sep 2015, 13:32
Location: Colmar-Berg (Luxembourg)

12 Feb 2016, 09:40

Thanks for your help.

I have modified my code in 2 places.

First, in <p:autoComplet> widget, I have added a "widgetVar" attribute with EXACTLY same value as "id" attribute.

Then, my JavaScript code look like

Code: Select all

function AutoCompleteOnKeyDown(oField, e)
    {
    if (e.keyCode === 40) // Pressing on ARROW-DOWN
        {
        // WidgetVar must be equal to Id define in AutoComplete component    
        var sWidgetVarName = oField.id; 
                    
        // remove part before last : character
        var iPos = sWidgetVarName.lastIndexOf(":");
        sWidgetVarName = sWidgetVarName.substring(iPos+1); 
        // remove '_input' at end of string
        var iPos = sWidgetVarName.lastIndexOf("_");
        sWidgetVarName = sWidgetVarName.substring(0,iPos); 
                    
        var oWidgetVar = PF(sWidgetVarName);    
                    
        if (!oWidgetVar)
            {
            alert("WIDGET-VAR attribute is not equal to ID attribute (AutoComplete:" + oField.id);        
            }
        else
            {
            // show propositions ONLY if panel is not already displayed
            // to allow to navigate in panel with UP and DOWN arrow keys !
            if (oField.value === "")
                {
                if (!oWidgetVar.panel.attr('style'))
                    {
                    // show suggestions panel
                    oWidgetVar.search("*");
                    }
                else    
                if (oWidgetVar.panel.attr('style').indexOf("visiblity: visible") > 0)
                    {
                    // show suggestions panel
                    oWidgetVar.search("*");
                    }
                }
            }
        }
    }
It is a big workaround with some tips that depends on how PrimaFaces is named his variable.

I have also try a solution where, in onkeydown() function I pass not "this" but PF('<name of widgetVar').

This solution has no workaround since I must not find/construct the sWidgetVar string.

But this is peanful, because I must type the widgetVar for all <p:autoComplete> widget.

If somebody has a more elegant solution, I'm insteresting !

Is there a manner to pass widgetVar object like

onkeydown="AutoCompleteOnKeyDown(widgetVar, event);"

:?:
Last edited by schlebe on 12 Feb 2016, 09:45, edited 1 time in total.
PrimeFaces 6.2.4
Mojarra 2.2.13
Glassfish 4.1

schlebe
Posts: 212
Joined: 25 Sep 2015, 13:32
Location: Colmar-Berg (Luxembourg)

12 Feb 2016, 09:43

To answer to question in title.

The search() function of autoComplete widget call the ViewController beans method defined in completeMethod attribute. :)
PrimeFaces 6.2.4
Mojarra 2.2.13
Glassfish 4.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 30 guests