JSF-2 Select/Unselect all the list of checkboxes with single checkbox

别等时光非礼了梦想. 提交于 2019-12-25 19:56:12

问题


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.


回答1:


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);
        }
    }
    



回答2:


Thanks Emil! I solved it.

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


来源:https://stackoverflow.com/questions/39114337/jsf-2-select-unselect-all-the-list-of-checkboxes-with-single-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!