tags:

views:

40

answers:

1

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?

+1  A: 

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
Thanks Adam, your solution works perfectly.
Abdul