JSF-2使用单个复选框选中/取消选中所有复选框列表 [英] JSF-2 Select/Unselect all the list of checkboxes with single checkbox
问题描述
我有rich:dataTable里面的复选框列表,我想用一个复选框从标题列一次检查所有的框。
< 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 =selectedForWHProcessvalue =#{checkallbox.checked [data]}>
< f:ajax actionListener =#{checkallbox.selectAllRows}/>
< / h:selectBooleanCheckbox>< / rich:column>
Checkallbox中的代码Bean:
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();
迭代器< StandardStructure> keys = checked.keySet()。iterator();
while(keys.hasNext()){
StandardStructure ss = keys.next();
checked.put(ss,newSelectAll);
}
}
当我检查h:selectBooleanCheckBox的标题列发生。我在这里缺少什么?是否应该为selectAll属性实现Map?
谢谢。
首先。 f:ajax
没有 actionListener
,它有一个侦听器
。阅读文档此处。第二件事,你可以在 h:selectBooleanCheckbox
上使用 valueChangeListener
第三,list里面的行框是错误的。基本上,您似乎需要阅读此主题。 p>
现在,这里是一个工作示例:
< h:form> ;
< rich:dataTable value =#{checkallbox.allValues}var =dataid =dataTable>
< rich:column>
< f:facet name =header>
< h:selectBooleanCheckbox value =#{checkallbox.selectAll}
valueChangeListener =#{checkallbox.selectAllBox}>
< f:ajax render =dataTable/>
< / h:selectBooleanCheckbox>
< h:selectBooleanCheckbox value =#{checkallbox.checked [data]}>
< f:ajax />
< / h:selectBooleanCheckbox>
< / rich:column>
<! - 其他列 - >
< / rich:dataTable>
< / h:form>
您的代码可能遇到的其他问题(因为您只共享了一部分)。
- 数据表需要采用表格形式,因为您正在执行ajax。
- 是对象。你必须确保
equals
方法是好的。在95%的情况下,默认值不是,特别是如果他们@Entity
。 -
则在开始时使用false填充地图。我使用
@PostConstruct
:@PostConstruct
protected void performPostConstructAction(){
for(StandardStructure s:getAllValues()){
checked.put(s,false);
}
}
I have list of check boxes inside rich:dataTable and I want to check all the boxes at once with a single check box from header column.
<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>
Code in checkallbox Bean:
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);
}
}
When I check the h:selectBooleanCheckBox of header column nothing happens. What am I missing here? Should I have to implement Map for "selectAll" property too?
Thanks.
First of all. f:ajax
doesn't have actionListener
, it has a listener
. Read the docs here. Second thing, you can use valueChangeListener
on h:selectBooleanCheckbox
and only there. Third, listener inside rows boxes is wrong. Basically, it looks like you need to read this topic.
Now, here is the working example:
<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>
Other possible problems with your code (since you've shared just a part).
- The data table needs to be in form, since you're executing ajax inside.
- Your keys in map are objects. You have to make sure that
equals
method is good. In 95% of case the default is not, especially if they are@Entity
. You have to make sure that the map is populated with false at the beginning. I use
@PostConstruct
:@PostConstruct protected void performPostConstructAction() { for (StandardStructure s : getAllValues()) { checked.put(s, false); } }
这篇关于JSF-2使用单个复选框选中/取消选中所有复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!