Check "All" in JSF without using Javascript

I am trying to select / deselect all the checkboxes in a datatable using a single checkbox. Since I am trying to install it on a server, I cannot do this. I was looking for solutions, but could not figure out how to do this 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 mark all the values ​​in the list both truewhen the checkbox is checked and when the checkbox is unchecked false.

But the page does not reload the data correctly.

Is my method right for the problem? Or is there an effective alternative?

+3
source share
2 answers

, ValueChangeEvent , , , .
public void selectAllComponents(ValueChangeEvent event)

if (event.getPhase() != PhaseId.INVOKE_APPLICATION) {
    event.setPhase(PhaseId.INVOKE_APPLICATON);
    event.queue();
 } else {
    //do your stuff here
 }
+7

,

selectAll = (Boolean) event.getNewValue();

+1

Source: https://habr.com/ru/post/1769728/


All Articles