通过RowEditEvent获取rowIndex [英] Get rowIndex via RowEditEvent

查看:104
本文介绍了通过RowEditEvent获取rowIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,但是我找不到正确的答案.

I have a simple question but I was unable to find the right answer.

我渲染了一个非常复杂的数据表:

I render a datatable which is pretty complex:

<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.

When I want to edit a row I want also to get the index of the current row which will be edited. I searched a lot but I can't see how I can extract the id from the RowEditEvent.

我还尝试将行的索引作为属性发送,但是没有成功.有任何想法吗?谢谢!

I also tried to sent as an attribute the index of the row but without success. Any ideas? Thanks!

推荐答案

您的数据表已经通过您使用rowKey="#{label.cod}"设置的属性携带了活动行信息.这给您留下了许多选择,其中最灵活的是DataTable类上的getRowIndex()变量,如下所示:

Your datatable already carries the active row information via the attribute you set with rowKey="#{label.cod}". This leaves you a number of options, of which the most flexible is the getRowIndex() variable on the DataTable class like so:

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方法来检索当前所选对象的索引(项的顺序)就后备列表中项目的顺序而言,数据表中的数据是非常可靠的).这让您可以选择:

Alternatively, if your list items have properly implemented equals and hashCode, you should also be able to retrieve index of the currently selected object by simply using the indexOf method on List (the ordering of items in a datatable is pretty reliable with regard to the ordering of the items in the backing list). That leaves you the option of :

 public void onRowEdit(RowEditEvent event) {

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

 }

当然,替代方法并不是特别有效(列表需要遍历每个项目才能进行比较),但是非常简单

Granted, the alternative is not particularly efficient (the list needs to run thru every item to compare), but it's quite straightforward

这篇关于通过RowEditEvent获取rowIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆