2016-08-24 79 views
-1

我有rich:dataTable里面的复选框列表,我想用标题列中的单个复选框同时检查所有框。JSF-2使用单个复选框选择/取消选中所有复选框列表

<rich:column id="includeInWHMapping" > 
     <f:facet name="header"> 
     <h:selectBooleanCheckbox value="#{checkallbox.selectAll}"> 
      <f:ajax actionListener="#{checkallbox.selectAllBox}" render="selectedForWHProcess" /> 
     </h:selectBooleanCheckbox> 
     </f:facet>   
     <h:selectBooleanCheckbox id="selectedForWHProcess" value="#{checkallbox.checked[data]}">  
     <f:ajax actionListener="#{checkallbox.selectAllRows}"/> 
     </h:selectBooleanCheckbox></rich:column> 

守则checkallbox豆:

private Map<StandardStructure, Boolean> checked = new HashMap<StandardStructure, Boolean>();   
private boolean selectAll; 

public boolean isSelectAll() { 
    return selectAll; 
} 

public void setSelectAll(boolean selectAll) { 
    this.selectAll = selectAll; 
} 

public Map<StandardStructure, Boolean> getChecked() { 
    return checked; 
} 

public void setChecked(Map<StandardStructure, Boolean> checked) { 
    this.checked = checked; 
} 

public void selectAllBox(ValueChangeEvent e){ 
    boolean newSelectAll = (Boolean) e.getNewValue(); 
    Iterator<StandardStructure> keys = checked.keySet().iterator(); 
    while(keys.hasNext()){ 
     StandardStructure ss = keys.next(); 
     checked.put(ss, newSelectAll); 
    } 
} 

当我检查了H:的标题栏没有selectBooleanCheckBox发生。我在这里错过了什么?我是否也必须为“selectAll”属性实现Map?

谢谢。

+0

'selectAllBox'是否被触发,您是否尝试渲染整个表? –

+0

@EmilSierżęga是的,但它没有发生在复选框列表上的变化。我没有渲染整个表格,但复选框列。谢谢回复! –

+0

你必须渲染整个表格。你不能渲染“一列”。在呈现的HTML中,没有列是这样的东西,所以你不想渲染几个像'tableId:_NUMBER_:selectedForWHProcess'这样的id。阅读这两个主题:http://stackoverflow.com/questions/6706849/how-to-do-a-column-level-render-in-datatable-in-jsf和http://stackoverflow.com/questions/16043218/refer-to-jsf-dynamic-generated-ids-based-ite-index- –

回答

0

首先。 f:ajax没有actionListener,它有一个listener。阅读文档here。第二件事,你可以在h:selectBooleanCheckbox上使用valueChangeListener,只有在那里。第三,听筒里面的行箱是错误的。基本上,它看起来像你需要阅读this topic

现在,这里是工作示例:

<h:form> 
    <rich:dataTable value="#{checkallbox.allValues}" var="data" id="dataTable"> 
     <rich:column> 
      <f:facet name="header"> 
       <h:selectBooleanCheckbox value="#{checkallbox.selectAll}" 
        valueChangeListener="#{checkallbox.selectAllBox}"> 
        <f:ajax render="dataTable" /> 
       </h:selectBooleanCheckbox> 
      </f:facet> 
      <h:selectBooleanCheckbox value="#{checkallbox.checked[data]}"> 
       <f:ajax /> 
      </h:selectBooleanCheckbox> 
     </rich:column> 

     <!-- other columns --> 
    </rich:dataTable> 
</h:form> 

其他与您的代码可能出现的问题(因为你已经分享了一部分)。

  • 由于您正在执行ajax,因此数据表需要处于窗体中。
  • 你在地图上的键是对象。你必须确保equals方法是好的。在95%的情况下,默认值不是,特别是如果它们是@Entity
  • 您必须确保地图在开始时填入false。我用@PostConstruct

    @PostConstruct 
    protected void performPostConstructAction() { 
        for (StandardStructure s : getAllValues()) { 
         checked.put(s, false); 
        } 
    } 
    
+0

谢谢埃米尔!我非常感谢你的努力。我没有使用@PostConstruct。但现在它起作用了。我现在唯一的问题是分页时,所有选中的框都未选中,但行被选中。 –

+0

很高兴我能提供帮助,但看起来问题已经解决,因为您已将您的答案标记为已接受:-) –

+0

其实我接受了您的答案!我想念我的! :P再次接受你的答案!谢谢! :) –

0

感谢埃米尔!我解决了它。

public void selectAllBox(){ 
    if(!selectAll){ 
     for(StandardStructure item : ssTable.getDto()){ 
      checked.put(item, true); 
     } 
    }else{ 
     for(StandardStructure item : ssTable.getDto()){ 
      checked.put(item, false); 
     } 
    } 
} 
+0

这不是你的问题的答案,而是你想要的。 –

相关问题