2013-02-27 103 views
3

我正在使用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。

在此先感謝

回答

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> 

我希望能對您有所幫助。

問候。

+0

感謝您的回覆,儘管一年後嘿嘿。事實上,這將部分解決問題,因爲有一個rowsPerPageTemplate允許用戶選擇每頁的行數。當然,如果人們也可以得到這個數字(我相信這是可能的),那麼問題就解決了。我希望這會在將來幫助其他人,所以我會選擇你的答案作爲求解器;) – H3LL0 2014-07-10 10:34:27