kem
kem

Reputation: 407

Richfaces Datatable Checkbox for Selecting All Rows

I am trying to implement a checkbox header for selecting all richFaces dataTable rows. setSelction is called but selection.size = 0; the backing bean is an EntityHome from seam 2.2.2 Any idea why selected rows are not set into selection?

Thanks

<h:form styleClass="association" id="heavyChainsChildren">

<rich:extendedDataTable
                    onRowMouseOver="this.style.backgroundColor='#F1F1F1'"
                    onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'"
                    value="#{myEntityHome.myData}"
                    selectionMode="multi"
                    selection="#{myEntityHome.selection}"
                    var="entry"
                    rendered="#{not empty myEntityHome.myData}"
                    rowClasses="rvgRowOne,rvgRowTwo"
                    id="myDataTable">


                    <rich:column align="center" width="150px" sortable="false">
                        <f:facet name="header">
                            <h:panelGroup style="width:150px;" layout="block">
                                <script type="text/javascript">
                                    //<![CDATA[
                                    function checkAllCheckboxesInTable(inputId, state) {
                                        var commonIdPart = inputId.substr(0, inputId.lastIndexOf(':'));
                                        var tableId = commonIdPart + ':tu'
                                        var tableElement = document.getElementById(tableId);
                                        var inputs = tableElement.getElementsByTagName('input');
                                        for (var i = 0; i <= inputs.length; i++) {
                                            var input = inputs[i];
                                            if (input.getAttribute('type') == 'checkbox' && state) {
                                                input.setAttribute('checked', state);
                                            } else {
                                                input.removeAttribute('checked');
                                            }
                                        }
                                    }
                                    //]]>
                                </script>
                                <h:selectBooleanCheckbox id="checkAll" title="#{bundle.CHECK_ALL}" onclick="checkAllCheckboxesInTable(this.id, this.checked);">
                                    <a:support event="onchange" reRender="myDataTable"
                                               />
                                </h:selectBooleanCheckbox>
                                <h:outputText value="#{bundle.IDENTITY_CHECKBOX_SELECT_ALL}"/>
                            </h:panelGroup>
                        </f:facet>
                        <h:selectBooleanCheckbox id="checkEntry" value="#{entry.selected}" disabled="false"/>
                    </rich:column>

bean:

@Name("myEntityHome")
public class MyEntityHome extends EntityHome<MyEntity> {

private static final Logger log = LoggerFactory.getLogger(MyEntityHome.class);
private SimpleSelection selection;

public SimpleSelection getSelection() {
    log.info("getSelection called ....");
    return selection;
}

public void setSelection(SimpleSelection selection) {
    log.info("setSelection called ...");
    this.selection = selection;
}

Upvotes: 0

Views: 1448

Answers (1)

Yasin
Yasin

Reputation: 133

You can take advantage of this page.There are more examples richfaces datatable Download war file and import your local workspace.

http://balusc.omnifaces.org/2006/06/using-datatables.html#DownloadWAR

You can do test when you run project in eclipse java platform you can call this url as your local port number. If you don't have java platform you can decompile war file and take neccessary file and crud.jsp

http://localhost:8083/UsingDatatables/crud/crud.jsf

You should look at MyCrudBean.java and crud.jsp files. These codes will handle your problem.

MyCrudBean:

  private Map<Integer, Boolean> selectedRows = new HashMap<Integer, Boolean>();
  private List<MyData> dataList;

  /**
 * Select all items.
 */
public void actionSelectAll() {

    // Toggle.
    selectAll = !selectAll;

    // Radio buttons doesn't allow multiple selections. Switch to checkboxes.
    if (selectAll) {
        selectMultiple = true;
    }

    // Set selection.
    for (int index = 0; index < dataList.size(); index++) {
        selectedRows.put(index, selectAll);
    }

    log(selectedRows);
}

crud.jsp

                <h:column>
                    <f:facet name="header"><h:outputText value="#{myCrudBundle.headerColumnRowNumber}" /></f:facet>
                    <h:outputText value="#{myCrudBean.dataTable.rowIndex + 1}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:commandLink action="#{myCrudBean.actionToggleSelect}" disabled="#{myCrudBean.editMode}" title="#{myCrudBundle.titleToggleSelect}">
                            <h:outputText value="#{myCrudBundle.headerColumnSelect}" />
                        </h:commandLink>
                    </f:facet>
                    <h:selectBooleanCheckbox value="#{myCrudBean.selectedRow}" disabled="#{myCrudBean.editMode}" rendered="#{myCrudBean.selectMultiple}" />
                    <h:selectOneRadio valueChangeListener="#{myCrudBean.setSelectedItem}" disabled="#{myCrudBean.editMode}" rendered="#{!myCrudBean.selectMultiple}" onchange="dataTableSelectOneRadio(this);">
                        <f:selectItem itemValue="null" />
                    </h:selectOneRadio>
                </h:column>

                    <h:column>
                    <f:facet name="header">
                        <h:panelGroup>
                            <h:commandLink actionListener="#{myCrudBean.actionSort}" disabled="#{myCrudBean.editMode}" title="#{myCrudBundle.titleSortName}">
                                <f:attribute name="sortField" value="name" />
                                <h:outputText value="#{myCrudBundle.headerColumnName}" />
                            </h:commandLink>
                            <h:outputText value="&#0160;&#9650;" escape="false" rendered="#{myCrudBean.sortField == 'name' && !myCrudBean.sortAscending}" />
                            <h:outputText value="&#0160;&#9660;" escape="false" rendered="#{myCrudBean.sortField == 'name' && myCrudBean.sortAscending}" />
                        </h:panelGroup>
                    </f:facet>
                    <h:outputText value="#{dataItem.name}" rendered="#{!myCrudBean.editModeRow}" />
                    <h:inputText id="name" value="#{dataItem.name}" rendered="#{myCrudBean.editModeRow}" label="Row ##{myCrudBean.dataTable.rowIndex + 1} Name" required="#{!empty param['crud:table:save']}" styleClass="input" size="15" />
                </h:column>



                <f:facet name="footer">
                    <h:panelGrid columns="4">

                        <h:panelGroup>
                            <h:commandButton value="#{myCrudBundle.buttonSelectAll}" action="#{myCrudBean.actionSelectAll}" rendered="#{!myCrudBean.editMode && !myCrudBean.selectAll}" styleClass="input" />
                            <h:commandButton value="#{myCrudBundle.buttonUnselectAll}" action="#{myCrudBean.actionSelectAll}" rendered="#{!myCrudBean.editMode && myCrudBean.selectAll}" styleClass="input" />

                        </h:panelGroup>
                    </h:panelGrid>
                </f:facet>
            </h:dataTable>


        </h:form>

Upvotes: 0

Related Questions