Primefaces dataTable rowIndexVar与分页
问题描述:
我正在使用primefaces数据表与单行选择和分页。对于每一行,我都有一些命令链接(其中之一是禁用的),我希望用户单击以在该行上执行操作(例如:删除,编辑)。 为了提供用户点击右侧按钮上的按钮的视觉确认,我希望点击时选择该行,然后继续所需的bean方法。 如果表格只有一个页面或者页面为1,则这是按预期工作的;在所有其他页面上,我得到一个NullPointerException。
我想出了为什么会发生这种情况:我使用rowIndexVar来执行选择,但它似乎返回与整个表相关的索引,而selectRow方法需要与当前页面相关的索引。这意味着如果每页有10行,并且在第二页中点击第三行,返回的索引是“12”而不是“2”,selectRow(12,false)返回null,因为只有10个项目页。Primefaces dataTable rowIndexVar与分页
我的问题是:如何传递正确的rowIndex,以便在所有页面上都能正确选择?
的支持bean是ViewScoped和方法不是什么了不起的事,但因为它有大量的代码与Web服务和JAXB生成的类与我公司签订保密协议,我不能在这里贴(我已经通过共享数据表代码来推送它)。
<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。
在此先感谢
答
它可以使用此功能从分页程序获取当前页面:
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>
我希望能对您有所帮助。
问候。
感谢您的回复,尽管一年后嘿嘿。事实上,这将部分解决问题,因为有一个rowsPerPageTemplate允许用户选择每页的行数。当然,如果人们也可以得到这个数字(我相信这是可能的),那么问题就解决了。我希望这会在将来帮助其他人,所以我会选择你的答案作为求解器;) – H3LL0 2014-07-10 10:34:27