Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

richfaces - select All checkbox in JSF without using Javascript

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...