Rajat Gupta
Rajat Gupta

Reputation: 26597

Listening to onSelect events from Autocomplete (Primefaces) component

I am trying to listen to select event from autocomplete using attribute selectListener. I am passing a remoteCommand as select listener. But the selectListener never calls this remoteCommand method.

My code follows:

<h:form>
    <p:autoComplete autocomplete="true" completeMethod="#{search.fetchSuggestions}" value="#{search.selectedSuggestion}" selectListener="moveToSelectedPage()"/>

    <p:remoteCommand name="moveToSelectedPage" action="firstPage.xhtml?faces-redirect=true" />
</h:form>

All I am trying to do is, navigating to a different page after the user selects a particular suggested item among suggestions made by autocomplete.

Upvotes: 3

Views: 16088

Answers (2)

BalusC
BalusC

Reputation: 1108742

The selectListener attribute should refer to a managed bean method taking SelectEvent and returning void, not to some arbitrary JavaScript function.

See also the PrimeFaces <p:autoComplete> showcase page.

<p:autoComplete selectListener="#{autoCompleteBean.handleSelect}" ... />  

with

public void handleSelect(SelectEvent event) {  
    // ... 
}

Upvotes: 8

Dave Mulligan
Dave Mulligan

Reputation: 1673

Looking at PrimeFaces version 3.5, it appears that the selectListener attribute is no longer available for the AutoComplete component. The link in BalusC's answer leads to the correct place, where it shows the new approach to be to include a <p:ajax> tag inside the <p:autocomplete>:

<p:autoComplete id="acSimple" value="#{autoCompleteBean.txt1}" completeMethod="#{autoCompleteBean.complete}">  
  <p:ajax event="itemSelect" listener="#{autoCompleteBean.handleSelect}" update="messages" />  
</p:autoComplete>

Upvotes: 9

Related Questions