【问题标题】:Richfaces Datatable Checkbox for Selecting All Rows用于选择所有行的 Richfaces 数据表复选框
【发布时间】:2014-12-14 21:19:12
【问题描述】:

我正在尝试实现一个复选框标题以选择所有richFaces dataTable 行。 调用 setSelction 但 selection.size = 0; 支持 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;
}

【问题讨论】:

  • 我不确定您要做什么,选中复选框不会选中该行。
  • 我想使用一个复选框来选择/取消选择rich:dataTable或extendedDataTable中的所有行

标签: java datatable richfaces seam2


【解决方案1】:

你可以利用这个页面。有更多的例子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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2014-08-09
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2017-10-07
    • 2019-03-10
    相关资源
    最近更新 更多