user1801069
user1801069

Reputation: 75

Preselected value in the PrimeFaces autoComplete

Is there a way to "combine" the SelectOneManu and autoComplete feature? When the form is loaded I'd like that input field to display the current value of the bean property, plus ability to select a new value with autoComplete.

Upvotes: 3

Views: 10173

Answers (1)

kolossus
kolossus

Reputation: 20691

Primefaces already provides this already in the autocomplete component. Just look on the demo site. By adding the dropdown="true" on the autocomplete menu, you enable support for a dropdown. Concretely, follow the following steps to get your results

  1. Set dropdown="true" on your autocomplete menu. Then set the completeMethod to correspond to a method on your backing bean that returns a list of the items you want to show up in the dropdown menu.

  2. To preset the value on the autocomplete component, simply initialise the value in the backing bean to whatever you want. Take the following as an example. If you have

    <p:autoComplete id="dd" dropdown="true" value="#{yourBackingBean.myVariable}" completeMethod="#{yourBackingBean.loadOptions}" />  
    

    In your backing bean, you initialise the myVariable type during it's declaration

    String myVariable = "Desired Value";
    
  3. If you're going to be populating the dropdown list with a list of complex/POJO types (and as a result, bind the value attribute to a complex type in the backing bean), you'll need to use the converter based autocomplete component implementation

Upvotes: 6

Related Questions