繁体   English   中英

Primefaces dataTable rowIndexVar with pagination

[英]Primefaces dataTable rowIndexVar with pagination

我正在使用具有单行选择和分页的primefaces数据表。 对于每一行,我都有一些命令链接(其中一个被禁用),我希望用户单击该链接以对该行执行操作(例如:删除,编辑)。 为了提供用户单击右行按钮的视觉确认,我希望在单击时选择行,然后继续执行所需的bean方法。 如果表只有一个页面或者它是第1页,那么这是按预期工作的; 在所有其他页面上,我得到一个NullPointerException。
我弄清楚为什么会发生这种情况:我使用rowIndexVar来执行选择,但它似乎返回与整个表相关的索引,而selectRow方法需要一个与当前页面相关的索引。 这意味着如果我每页有10行并且我点击第二页中的第三行,则返回的索引是“12”而不是“2”而selectRow(12,false)返回null,因为只有10个项目页。

我的问题是: 如何传递正确的rowIndex,以便在所有页面上得到正确的选择?

支持bean是ViewScoped,方法并不是什么特别的,但因为它有很多与webservices和jaxb生成的类相关的代码,而且我签了一个NDA,我不能在这里粘贴它(我已经通过共享它来推动它了)数据表代码)。

<p:dataTable id="dataTable" var="item" rowIndexVar="rowIndex"
    value="#{dgRefTypePubBean.itemList}"      
    widgetVar="itemsTable"
    filteredValue="#{dgRefTypePubBean.filteredItems}" paginator="true"
    rows="10"
    paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    rowsPerPageTemplate="2,5,10,20" rowKey="#{item.EID}"
    selection="#{dgRefTypePubBean.selectedItem}" selectionMode="single"
    emptyMessage="#{msgs['dgreftype.datatable.message.emptyList']}"
    resizableColumns="false">

    <p:ajax event="page" oncomplete="itemsTable.unselectAllRows(); itemsTable.selectRow(0, false)" />
    <p:ajax event="rowSelect" listener="#{dgRefTypePubBean.onRowSelect}"
        update=":form:obs :form:index_info :form:elimination_just :form:left_footer :form:right_footer" />
    <p:ajax event="rowUnselect" listener="#{dgRefTypePubBean.onRowUnselect}" update=":form:obs" />

    <p:column style="text-align:right; width:22px; border:none; background:white">
        <h:graphicImage rendered="#{item.EState==2}" library="images" name="data-delete.png" width="15" height="15" style="border:none; padding:0" />
    </p:column>
    <p:column id="codColumn" headerText="#{msgs['dgreftype.datatable.label.functionalCode']}" filterStyle="height:10px; font-weight:normal" style="text-align:left; width:120px" filterBy="#{item.EFunctionalCode}">
        <h:outputText value="#{item.EFunctionalCode}" />
    </p:column>
    <p:column id="designationColumn" headerText="#{msgs['dgreftype.datatable.label.name']}" filterStyle="height:10px; font-weight:normal" style="text-align:left; word-wrap: break-word" filterBy="#{item.EName}">
        <h:outputText value="#{item.EName}" />
    </p:column>
    <p:column id="variableColumn" headerText="#{msgs['dgreftype.datatable.label.variableTypeName']}" filterStyle="height:10px; font-weight:normal" style="text-align:left; width:200px" filterBy="#{item.EVariableTypeName}">
        <h:outputText value="#{item.EVariableTypeName}" />
    </p:column>
    <p:column id="buttonsColumn" style="width:55px">
        <h:panelGrid columns="3" style="border-collapse:separate; border:none !important">
            <h:commandLink onclick="itemsTable.unselectAllRows(); itemsTable.selectRow(#{rowIndex}, false)" action="#{dgRefTypePubBean.editSelectedItem()}">
                <h:graphicImage library="images" name="edit-text.png" width="15" height="15" style="border:none" />
            </h:commandLink>
            <h:graphicImage library="images" name="detail-disabled.png" width="15" height="15" style="border:none" onclick="itemsTable.unselectAllRows(); itemsTable.selectRow(#{rowIndex}, false)" />
            <h:commandLink onclick="itemsTable.unselectAllRows(); itemsTable.selectRow(#{rowIndex}, false); confirmation.show(); return false;">
                <h:graphicImage library="images" name="edit-delete.png" width="15" height="15" style="border:none" />
            </h:commandLink>
        </h:panelGrid>
    </p:column>
</p:dataTable>

我正在使用JSF 2.0和Primefaces 3.4.2。

提前致谢

可以使用此函数从paginator获取当前页面:

itemsTable.paginator.getCurrentPage()

因此,我们可以从此值计算正确的行索引,#{rowIndex}的值和每页的最大行数。

 <h:commandLink onclick="itemsTable.unselectAllRows(); itemsTable.selectRow(#{rowIndex}-itemsTable.paginator.getCurrentPage()*10)" action="#{dgRefTypePubBean.editSelectedItem()}">
      <h:graphicImage library="images" name="edit-text.png" width="15" height="15" style="border:none" />
 </h:commandLink>

我希望这有帮助。

问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM