1

How could I Uncheck all checkboxes from selectManyCheckbox when choosing "No" from selectOneRadio

<p:selectOneRadio id="radio" value="#{myView.myObject.myBoolean}">
<f:selectItem itemLabel="Si" itemValue="#{true}"/>
<f:selectItem itemLabel="No" itemValue="#{false}"/>
<p:ajax process="radio" event="valueChange" update="@widgetVar(displayPanel)"/>
<p:ajax update="@this"/>
</p:selectOneRadio>

<p:panel widgetVar="displayPanel">
<p:outputPanel rendered="#{myView.myObject.myBoolean}">
<p:selectManyCheckbox widgetVar="mySelections" value="#{myView.myObject.myObjectList}" layout="grid" columns="8" styleClass="grid-checkbox">
<p:ajax update="@this"/>
<f:selectItems value="#{myView.things}" var="thing" itemLabel="#{thing.idThing}" itemValue="#{thing.thing}"/>
</p:selectManyCheckbox> 
</p:outputPanel>
</p:panel>

As seen, update="@widgetVar(displayPanel)" will show the outputPanel when myBoolean is true and viceversa.

What I need to achieve is to uncheck all the already selected checkboxes (mySelections) when myBoolean is false (by selecting "No" from selectOneRadio above.

Possible? If so, Could you please show me how, I'm just starting on PrimeFaces.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • Use listener in you , call a method in your MyView bean, inside the method you can clear your myObect.myObjectList. Try it – Asgar Aug 03 '22 at 15:26
  • Thanks Agar, I've been there actually, I would need to stay away from bean and achieve the uncheck from the xhtml itself – Roger Castillo Aug 03 '22 at 20:32

1 Answers1

0

Normally, the easiest way would be using the client side API uncheckAll. However, there is a bug. But, I've fixed it. So you can wait for PrimeFaces 12.0.0-RC3 or create a Monkey Patch.

A simple demo would be:

<p:selectOneRadio id="radio" value="#{testView.bool}"
                  onchange="if(this.value==='false'){PF('mySelections').uncheckAll()}">
    <f:selectItem itemLabel="Si" itemValue="true"/>
    <f:selectItem itemLabel="No" itemValue="false"/>
</p:selectOneRadio>

<p:selectManyCheckbox widgetVar="mySelections" value="#{testView.strings}">
    <f:selectItems value="#{['a','b','c']}"/>
</p:selectManyCheckbox>

This eliminates the need to use Ajax.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102