0
votes

I am studying a PrimeFaces AutoComplete demo. I shortenied it from the full showcase demo. http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml

AutoCompleteBean.java

 @ManagedBean
public class AutoCompleteBean {         
    private Query query;
    private List<Query> queries = new ArrayList<Query>();

    @PostConstruct
    public void init() {
        queries.add(new Query(0, "Afterdark", "afterdark"));
        queries.add(new Query(1, "Afternoon", "afternoon"));
        queries.add(new Query(2, "Afterwork", "afterwork"));
        queries.add(new Query(3, "Aristo", "aristo"));            
    }

    public List<Query> completeQuery(String query) { 
       List<Query> filteredQueries = new ArrayList<Query>();
        for (int i = 0; i < queries.size(); i++) {
            Query skin = queries.get(i);
            if(skin.getName().toLowerCase().contains(query)) {
                filteredQueries.add(skin);
            }
        }

        return filteredQueries;
    }

    public void onItemSelect(SelectEvent event) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject().toString()));
    }

    public Query getQuery() {
        return query;
    }

    public void setQuery(Query query) {
        this.query = query;
    }
}

Query.java

public class Query {

    private int id;   
    private String displayName;  
    private String name;

    public Query() {}

    public Query(int id, String displayName, String name) {
        this.id = id;
        this.displayName = displayName;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

I omitted a Convert class, which I think is not that relevant.

search.xhtml

<h:form>
    <p:growl id="msgs" showDetail="true" />
    <h:panelGrid columns="2" cellpadding="5">
        <p:autoComplete id="queryPojo" value="#{autoCompleteView.query}"
        completeMethod="#{autoCompleteView.completeQuery}" var="query"
                    itemLabel="#{query.displayName}" itemValue="#{query}"
                    converter="queryConverter" forceSelection="true" />

        <p:commandButton value="search" oncomplete="PF('dlg').show()"/>

    </h:panelGrid>
</h:form>

I have three questions for this:

1) completeMethod="#{autoCompleteView.completeQuery}": completeQuery method is called without passing a parameter, but it's defined as completeQuery(String query). How does this work?

2) value="#{autoCompleteView.query}". Query is an object defined in AutoCompleteBean. How can this Query object take user input string as its value? Usually InputText's value is good for taking user's input, which is a String value.

3) Can I still add an attribute "action=..." to the p:autoComplete componenet?

1

1 Answers

0
votes

The converter class that you omitted here plays the real game.... Lets see your questions

As you see converter class overrides 2 methods

getAsString and getAsObject

1)the value

completeMethod="#{autoCompleteView.completeQuery}

gets refactred to

autoCompleteView.completeQuery(autoCompleteView.query);

as you can find to string method in Query class.

2).as converter is defined for autocomplete it calls getAsString method to render on screen. when selected getAsObject method is called to convert string value to object(Query).

3)you can use ajax select event

<p:ajax event="select" listener="#{autoCompleteView.someMethod}">

or call a remoteCommand by onSelect attribute in p:autoComplete

    <p:autoComplete id="queryPojo" value="#{autoCompleteView.query}" onSelect="someRemoteCommand();"
  completeMethod="#{autoCompleteView.completeQuery}" var="query"
                    itemLabel="#{query.displayName}" itemValue="#{query}"
                    converter="queryConverter" forceSelection="true" />
<p:remoteCommand name="someRemoteCommand" update="queryPojo" actionListener="#{autoCompleteView.execute}" />