Passing completeMethod of p: autoComplete

I am using the PrimeFaces p:autoComplete in the search form of my project. The user can choose how many and which form elements (search parameters) he wants to include, so I need to pass the identifier to completeMethod for each of them. I tried adding onfocus=".." to pass the object to the bean, but this will only be activated when the element is first loaded.

My question is: how do I pass the completeMethod attribute?

XHTML element (simple):

 <p:autoComplete value="#{filter.value}" label="dynamic search attribute" completeMethod="#{myBean.complete}" /> 

bean (simple):

 @Named("myBean") public class MyController implements Serializable { public List<String> complete(String query) { List<String> results = new ArrayList<String>(); // ... code return results; } } 

In theory, this would seem like an ideal solution:

 <p:autoComplete value="#{filter.value}" label="dynamic search attribute" completeMethod="#{myBean.complete(filter)}" /> 

And again bean:

 @Named("myBean") public class MyController implements Serializable { public List<String> complete(String query, FilterObject o) { List<String> results = new ArrayList<String>(); // ... database query based on FilterObject o return results; } } 
+7
source share
1 answer

You can set it as an attribute:

 <p:autoComplete value="#{filter.value}" label="dynamic search attribute" completeMethod="#{myBean.complete}"> <f:attribute name="filter" value="#{filter}" /> </p:autoComplete> 

and get it with UIComponent#getCurrentComponent() :

 public List<String> complete(String query) { FacesContext context = FacesContext.getCurrentInstance(); FilterObject o = (FilterObject) UIComponent.getCurrentComponent(context).getAttributes().get("filter"); // ... } 

Alternatively, since #{filter} appears in your case already in the EL area, you can also leave <f:attribute> and get it by evaluating the EL expression programmatically with Application#evaluateExpressionGet() :

 public List<String> complete(String query) { FacesContext context = FacesContext.getCurrentInstance(); FilterObject o = context.getApplication().evaluateExpressionGet(context, "#{filter}", FilterObject.class); // ... } 

Or, if it is also an @Named bean, you can simply @Inject it in the parent bean

 @Inject private FilterObject o; 
+22
source

All Articles