-1

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.

  • Is `selectAllBox` fired and did you try to render whole table? – Emil Sierżęga Aug 24 '16 at 07:39
  • @EmilSierżęga Yeah its fired but no such changes seen on the list of checkboxes. I did not render the whole table but the checkboxes column. Thanks for the reply! – Sarthak Joshi Aug 24 '16 at 07:50
  • You have to render whole table. You can't render just "a column". In rendered HTML there's no such a thing a column, so you wan't to render several ``'s with id like `tableId:_NUMBER_:selectedForWHProcess`. Read this two topics: http://stackoverflow.com/questions/6706849/how-to-do-a-column-level-render-in-datatable-in-jsf and http://stackoverflow.com/questions/16043218/refer-to-jsf-dynamically-generated-ids-based-on-iteration-index – Emil Sierżęga Aug 24 '16 at 08:03
  • @EmilSierżęga Hey thanks! But how shall I do that? I am new in JSF. Sorry! :) – Sarthak Joshi Aug 25 '16 at 01:25
  • And why don't you want to try rendering whole table? – Emil Sierżęga Aug 25 '16 at 07:31
  • @EmilSierżęga Actually I did render the Whole rich:dataTable with . But Nothing happens. – Sarthak Joshi Aug 25 '16 at 08:02
  • @BalusC Please help me! – Sarthak Joshi Aug 26 '16 at 05:03
  • Calling someone out like that is extremely rude! If someone wants to help you, he/she will. Now... I gave you working example in my answer. – Emil Sierżęga Aug 30 '16 at 09:17

2 Answers2

0

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);
        }
    }
    
Community
  • 1
  • 1
Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
  • Thanks Emil! I really appreciate your effort. I did not use @PostConstruct. But now it works. The only problem now I have is while pagination all the checked boxes are unchecked but the rows are selected. – Sarthak Joshi Aug 30 '16 at 10:04
  • Glad I could help, but it looks like the problem is solved since you've marked your answer as accepted :-) – Emil Sierżęga Aug 30 '16 at 10:06
  • Actually I accepted your answer! I miss checked mine! :P Accepted your answer again! Thanks! : ) – Sarthak Joshi Aug 30 '16 at 11:10
0

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