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