在不使用Javascript的情况下选择JSF中的All复选框

Abd*_*dul 3 jsf richfaces

我试图使用一个复选框选择/取消选中数据表中的所有复选框.当我试图在服务器上设置它时,我无法这样做.我一直在寻找解决方案,但无法在服务器端获得如何完成.

这是代码.

xhtml文件###

<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>
Run Code Online (Sandbox Code Playgroud)

Java文件

// 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);
  }
 }
Run Code Online (Sandbox Code Playgroud)

我正在标记列表中的所有值,如true选中复选框和false未选中时.

但该页面无法正确重新加载数据.

我的方法是否正确处理问题?还是有一种有效的替代方案?

Ada*_*dam 7

这是因为它ValueChangeEvent发生在更新模型阶段之前,因此您更改的值会被覆盖.
这样做public void selectAllComponents(ValueChangeEvent event)

if (event.getPhase() != PhaseId.INVOKE_APPLICATION) {
    event.setPhase(PhaseId.INVOKE_APPLICATON);
    event.queue();
 } else {
    //do your stuff here
 }
Run Code Online (Sandbox Code Playgroud)

  • 正确的方法名称是getPhaseId()和setPhaseId(),如规范http://download.oracle.com/docs/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.1_01/docs/api/javax/faces/event/中所述FacesEvent.html (3认同)