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
284 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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

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

...