1

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>

and a method

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

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Aditzu
  • 696
  • 1
  • 14
  • 25
  • I want to get the id of one cell from the row which it will be updated which is created dynamic. Eg (tableForm:labelsList:1:myId) where 1 is rowIndex – Aditzu Jan 15 '15 at 15:47
  • You could use `` to set the rowIndex value in your bean, have you tried this? – Pellizon Jan 15 '15 at 15:51
  • Yes I already tried to put on rowEditor but I get an error like " Parent is not of type ActionSource, type is: org.primefaces.component.roweditor.RowEditor@38523852 " – Aditzu Jan 15 '15 at 15:56
  • Did you put inside ? – Pellizon Jan 15 '15 at 15:58
  • yes but the setter of the property is not called and I don't know why. I have snipped code: where currentRow is a property of my bean – Aditzu Jan 15 '15 at 16:02
  • http://stackoverflow.com/questions/15876257/fsetpropertyactionlistener-doesnt-set-the-value-however-action-is-triggered It seems the f:setPropertyActionListener work only on commandButton or commandLink – Aditzu Jan 15 '15 at 16:13

1 Answers1

6

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
}

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

kolossus
  • 20,559
  • 3
  • 52
  • 104