3

I am trying to select/unselect all checkboxes in the datatable using a single checkbox. As I am trying to set it on the server, I am unable to do so. I have looked around for solutions but could not get how to accomplish on the server side.

Here is the code.

xhtml file###

<rich:column styleClass="center-aligned-text">
         <f:facet name="header">
          <h:selectBooleanCheckbox id="selectAll" title="selectAll" valueChangeListener="#{workspace.selectAllComponents}">
           <a4j:support event="onclick" reRender="listcomponents"/>
          </h:selectBooleanCheckbox>
         </f:facet>

         <h:selectBooleanCheckbox id="selectComponent" 
          value="#{workspace.selectedComponentIds[componentInfo.id]}">
         </h:selectBooleanCheckbox>
        </rich:column>

Java File

// Select All and delete
 public void selectAllComponents(ValueChangeEvent event){

  // If the check all button is checked, set all the checkboxes as selected 
  if(!selectAll)
  {
   changeMap(selectedComponentIds,true);
   setSelectAll(true);
  }
  else // If the button is unchecked, unselect all the checkboxes
  { 
   changeMap(selectedComponentIds,false);
   setSelectAll(false);
  }
 }

 public void changeMap(Map<Long,Boolean> selectedComponentMap, Boolean blnValue){
  if(selectedComponentMap != null){
   Iterator<Long> itr = selectedComponentMap.keySet().iterator();
   while(itr.hasNext()){
    selectedComponentMap.put(itr.next(), blnValue);
   }
   setSelectedComponentIds(selectedComponentMap);
  }
 }

I am marking all the values in the list as true when the checkbox is checked and false when unchecked.

But the page doesn't reload the data properly.

Is my method to approach to the problem correct? Or is there a efficient alternative?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Abdul
  • 694
  • 4
  • 14
  • 29
  • See http://stackoverflow.com/questions/2929037/select-and-unselect-list-of-checkboxes-with-single-checkbox – Krumb Apr 26 '12 at 16:24

2 Answers2

7

It's because the ValueChangeEvent occurs before the update model phase, so the value you change gets overwritten.
Do this in the public void selectAllComponents(ValueChangeEvent event)

if (event.getPhase() != PhaseId.INVOKE_APPLICATION) {
    event.setPhase(PhaseId.INVOKE_APPLICATON);
    event.queue();
 } else {
    //do your stuff here
 }
Adam
  • 5,045
  • 1
  • 25
  • 31
  • 3
    correct method names are getPhaseId() and setPhaseId() as described in spec http://download.oracle.com/docs/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.1_01/docs/api/javax/faces/event/FacesEvent.html – uthark Jan 15 '11 at 05:43
1

Or just before your code add next line code in your original method

selectAll = (Boolean) event.getNewValue();

Eldin
  • 11
  • 1
  • It didn't work for me without including the code given in the Adam's answer here: http://stackoverflow.com/questions/3942060/select-all-checkbox-in-jsf-without-using-javascript?rq=1 – A_J Nov 21 '15 at 06:48