2015-01-15 26 views
1

我有一個簡單的問題,但我無法找到正確的答案。通過RowEditEvent獲取rowIndex

我呈現一個數據表,這是非常複雜的:

<p:dataTable var="label" value="#{labelsManager.labelsList}" rowKey="#{label.cod}" editable="true" 
           rowsPerPageTemplate="5,10,15,30" paginator="true" paginatorPosition="bottom" rows="30" 
           paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
           rendered="#{not empty labelsManager.labelsList}" scrollable="true" scrollHeight="300" id="labelsList" sortMode="multiple" 
           selection="#{labelsManager.selectedLabelsForDelete}"> 
<p:ajax event="rowEdit" listener="#{labelsManager.onRowEdit}" /> 
...some code... 
</dataTable> 

和方法

public void onRowEdit(RowEditEvent event) { 
... here I want to get the index on the current row... 
} 

當我想編輯的行我想也得到當前行的指數,這將被編輯。我搜索了很多,但我看不到如何從RowEditEvent中提取id。

我也嘗試將行的索引作爲屬性發送,但沒有成功。有任何想法嗎?謝謝!

+0

我想買一個的ID它將被更新的行中的單元格創建爲動態。例如(tableForm:labelsList:1:myId)其中1是rowIndex – Aditzu 2015-01-15 15:47:44

+0

您可以使用''在bean中設置rowIndex值,您是否嘗試過? – Pellizon 2015-01-15 15:51:56

+0

是的,我已經試圖把rowEditor 但我得到一個錯誤,如「父不是類型的ActionSource,類型是:[email protected] 」 – Aditzu 2015-01-15 15:56:30

回答

5

您的數據表已經通過您使用rowKey="#{label.cod}"設置的屬性傳送活動行信息。這讓你有多種選擇,其中最靈活的是在DataTablegetRowIndex()變量,像這樣:

public void onRowEdit(RowEditEvent event) { 
    AjaxBehaviorEvent evt = (AjaxBehaviorEvent)event; 
    (DataTable) table = evt.getSource(); 
    int activeRow = table.getRowIndex(); //do whatever you want with it 
} 

另外,如果您的列表項已正確地equalshashCode,你也應該能通過使用List上的indexOf方法檢索當前選定對象的索引(數據表中項目的順序對於後備列表中項目的排序非常可靠)。這使得您的選擇:

public void onRowEdit(RowEditEvent event) { 

    Label theLabel = (Label)event.getObject(); //I'm assuming the item is of type Label 
    int theIndex = labelslist.indexOf(theLabel); 

} 

當然,另一種方法是不是特別有效(列表中需要直通每一個項目來比較運行),但它是非常簡單

+0

我還在等待代碼:D – Aditzu 2015-01-15 16:18:00

+0

我已經做了,你絕對正確。謝謝!附:我可以直接從事件調用getSource()方法。我不需要evt變量 – Aditzu 2015-01-15 16:24:49

+0

不客氣@Aditzu – kolossus 2015-01-15 16:30:37

相關問題