5 Replies Latest reply on Dec 20, 2007 8:55 PM by sunyson

    Toggling all checkbox values in a dataTable column

    mdmaurer

      I have a simple dataTable with a column containing a checkbox. There is also a checkbox in the header for that column. When checked, I want it to toggle the value of the checkbox in each row. All the code executes properly, but it never renders the new checkbox values in the rows. It does work fine if I set the dataTable value with value="#{testBean.tableData}" (where tableData is an ArrayList). But it doesn't work if I bind the dataTable with binding="#{testBean.uiData}" (where uiData is a UIData object). Why???

      Try running this code and see what I mean. Here it is:

      TestCheckboxTable.jsp

      <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
      <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
      <%@taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>
      
      <f:view>
      
       <body>
      
       <h:form id="theForm">
      
       <h:dataTable id="theTable" binding="#{testBean.uiData}" var="tableRow" border="1">
       <h:column>
       <f:facet name="header">
       <h:outputText value="NAME" />
       </f:facet>
       <h:outputText value="#{tableRow.name}" />
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:outputText value="NUMBER" />
       </f:facet>
       <h:outputText value="#{tableRow.number}" />
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:panelGrid columns="2">
       <h:outputText value="Checkbox"/>
       <h:selectBooleanCheckbox
       value="#{testBean.toggleAll}"
       valueChangeListener="#{testBean.toggleAllEvent}">
       <a4j:support event="onclick" reRender="theTable,checkbox" />
       </h:selectBooleanCheckbox>
       </h:panelGrid>
       </f:facet>
       <h:selectBooleanCheckbox id="checkbox" value="#{tableRow.checkbox}" />
       </h:column>
       </h:dataTable>
      
       <p>
       <h:commandButton value="Add Row" actionListener="#{testBean.addRow}"/>
      
       </h:form>
      
       </body>
      
      </f:view>
      


      TestBean.java
      import javax.faces.component.UIData;
      import javax.faces.event.*;
      import java.util.*;
      
      public class TestBean
      {
       private boolean toggleAll = false;
       private UIData mUiData = new UIData();
      
       public void addRow( ActionEvent e )
       {
       ArrayList<TableRow> tableData = new ArrayList<TableRow>();
       String str = new Integer( mUiData.getRowCount() ).toString();
       TableRow tr = null;
       for ( int i = 0; i < mUiData.getRowCount(); i++ )
       {
       mUiData.setRowIndex( i );
       tr = (TableRow)mUiData.getRowData();
       tableData.add( tr );
       }
       tableData.add( new TableRow( "Name"+str, str, false ) );
       mUiData.setValue( tableData );
       }
      
       public boolean getToggleAll()
       {
       return toggleAll;
       }
      
       public void setToggleAll( boolean pVal )
       {
       toggleAll = pVal;
       }
      
       public void toggleAllEvent( ValueChangeEvent e )
       {
       boolean newVal = ((Boolean)e.getNewValue()).booleanValue();
       newVal="+newVal+", count="+mUiData.getRowCount() );
       ArrayList<TableRow> tableData = new ArrayList<TableRow>();
       TableRow tr = null;
       for ( int i = 0; i < mUiData.getRowCount(); i++ )
       {
       mUiData.setRowIndex( i );
       tr = (TableRow)mUiData.getRowData();
       tr.setCheckbox( newVal );
       tableData.add( tr );
       }
       mUiData.setValue( tableData );
       }
      
      
       public UIData getUiData()
       {
       return mUiData;
       }
      
       public void setUiData( UIData uiData )
       {
       mUiData = uiData;
       }
      
      /////// INNER CLASS FOR TABLE ROW //////////////////////
       public class TableRow
       {
       private String name;
       private String number;
       private boolean checkbox;
      
       public TableRow( String pName, String pAge, boolean pCheckbox )
       {
       name = pName;
       number = pAge;
       checkbox = pCheckbox;
       }
      
       public String getName()
       {
       return name;
       }
       public void setName( String pVal )
       {
       name = pVal;
       }
       public String getNumber()
       {
       return number;
       }
       public void setNumber( String pVal )
       {
       number = pVal;
       }
       public boolean getCheckbox()
       {
       return checkbox;
       }
       public void setCheckbox( boolean pVal )
       {
       checkbox = pVal;
       }
       }
      /////// END OF INNER CLASS //////////////////////
      
      }