Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

p:ajax getter for update being called before listener

Tags:

jsf

primefaces

Here is my checkbox. I am preparing a comma delimited string of component ids in the listener. The problem here is the getter getUpdateComponentList() is being called before the listener is called. So the string is never updated.

<p:outputPanel>
<h:selectManyCheckbox value="#{form.colors}">
  <f:selectItems value="#{form.colorItems}"/>
  <p:ajax listener="#{form.testListener}" event="change" update="#{form.updateComponentList}"  />
</h:selectManyCheckbox>
</p:outputPanel>
like image 236
Ravi Kadaboina Avatar asked Mar 15 '12 16:03

Ravi Kadaboina


2 Answers

That's expected behaviour. PrimeFaces (and standard JSF) does not re-evaluate the update (and render, oncomplete, etc) attributes on a per-request basis. They are evaluated on a per-view basis. RichFaces, for example, does it in its <a4j:ajax> and yields exactly the expected behaviour.

For PrimeFaces, your best bet is to remove the update attribute and use RequestContext#addPartialUpdateTarget() or #addPartialUpdateTargets() in the action method instead.

E.g.

RequestContext.getCurrentInstance().addPartialUpdateTargets(updateComponentList);

It takes a Collection<String> such as List<String> or Set<String>.

By the way, that event="change" is unnecessary. Just use the component's default event.


Update for users of a newer PrimeFaces version who are reading this answer later on and can't find the aforementioned methods which are indeed removed in a newer PrimeFaces version; use one of the two update() methods instead (one takes a String and other takes a Collection<String>).

RequestContext.getCurrentInstance().update(updateComponentList);
like image 180
BalusC Avatar answered Nov 04 '22 16:11

BalusC


I faced a similar problem about 'update' getting executed before the 'listener'. Putting two 'p:ajax' -one with the listener and other with the update- did the trick.

In your case:

<p:outputPanel>
  <h:selectManyCheckbox value="#{form.colors}">
    <f:selectItems value="#{form.colorItems}"/>
    <p:ajax event="change" listener="#{form.testListener}" />
    <p:ajax event="change" update="#{form.updateComponentList}" />
  </h:selectManyCheckbox>
</p:outputPanel>
like image 43
Gayolomao Avatar answered Nov 04 '22 16:11

Gayolomao