0

There's a checkbox inside a form that needs to be double-clicked for values to be passed to the bean. I suppose there's a double submit, but don't know how to get rid of it.

Code right now:

<p:selectManyCheckbox id="displayBy" style="font-size: 12px" value="#{detailedDashboardMB.displayBy}" valueChangeListener="#{detailedDashboardMB.onDisplayByClick}" columns="6" layout="grid">
    <f:ajax render=":messages:growl detailed-chart" />
    <f:selectItem itemLabel="Weekday" itemValue="Weekday" />
</p:selectManyCheckbox>

Event method:

public void onDisplayByClick() {
    jsfUtils.messages("INFO", "Selected:", this.displayBy.toString());
}

First click (selecting the option) displays:

Selected: []

Second click (unselecting the option) displays:

Selected: [Weekday]

Rasshu
  • 1,764
  • 6
  • 22
  • 53

2 Answers2

0

ValueChangeListener doesn't work as you are expecting, it's a common mistake, maybe because it's name.

Check this answer by BalusC about the difference between valueChangeListener and <f:ajax>

[...] The valueChangeListener will only be invoked when the form is submitted and the submitted value is different from the initial value. [...]

So... since the ValueChangeListener only works when the form is submitted, and that is not what you want to do, you can use <p:ajax> to update the components and call a method in your bean:

<p:selectManyCheckbox id="displayBy" style="font-size: 12px" value="#{detailedDashboardMB.displayBy}" columns="6" layout="grid">
    <p:ajax update=":messages:growl detailed-chart" listener="#{detailedDashboardMB.onDisplayByClick()}" />
....

I'm not sure why your code take 2 clicks to work, but try to do with the <p:ajax> example.

Community
  • 1
  • 1
Pellizon
  • 1,365
  • 2
  • 12
  • 26
0

Ok I understood your problem. valueChangeListener is calling before the update model values, for this reason your displayBy seems not sinchronized.

Below the right code:

public void onDisplayByClick(ValueChangeEvent e) {
log.debug("OLD: " + e.getOldValue()); //is empty at first time
log.debug("NEW: " + e.getNewValue()); // is not anymore at first time
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Checkbox Selected", e.getNewValue().toString());

FacesContext.getCurrentInstance().addMessage(null, message);
}
giaffa86
  • 708
  • 1
  • 9
  • 22