Richfaces数据表复选框,用于选择所有行

时间:2014-12-14 21:19:12

标签: java datatable richfaces seam2

我正在尝试实现一个复选框标题,用于选择所有richFaces dataTable行。 调用setSelction但是selection.size = 0; backing bean是seam 2.2.2中的EntityHome 知道选择行没有设置为选择的原因吗?

由于

<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>

豆:

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

1 个答案:

答案 0 :(得分:0)

您可以利用此页面。还有更多示例richfaces数据表下载war文件并导入本地工作区。

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

您可以在Eclipse Java平台中运行项目时进行测试,可以将该URL称为本地端口号。如果您没有Java平台,则可以反编译war文件并获取必要的文件和crud.jsp

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

您应该查看MyCrudBean.java和crud.jsp文件。这些代码将解决您的问题。

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>