3

how can I get row number in Primefaces (2.2) DataTable by clicking button in some row? I need pass this number to javascript code with prompt() function.

1 solution

using WidgetVar.selection but in this case I use manually row selection and then click button -> bad scenario.

<p:column> <p:commandButton id="someButton" value="Button" actionListener="#{managedBean.someEvent}" onclick="return jsMethod(widgetVar.selection)" /> </p:column>

Thank You!

MaximG
  • 141
  • 2
  • 3
  • 12
  • With "row number", do you mean row ID or row index? I assumed row index, but the `selection` uses row ID. – BalusC Mar 13 '12 at 18:39
  • I mean row index (for example: 0,1,2 etc.) Maybe it's mistake to use 'selection' but I get correct 'ID' (0,1,2 etc.). I use this indexes to find 'hiddenInput' by Id (thank you for this article http://balusc.blogspot.com/2009/05/javajspjsf-and-javascript.html) and enter comment from 'js prompt()' to server side code for appropriate object in datatable. Thank you. – MaximG Mar 13 '12 at 19:56
  • Oh, that's maybe PF 2.2 specific. – BalusC Mar 13 '12 at 20:07

1 Answers1

4

You can use UIData#getRowIndex() for this.

<p:dataTable binding="#{table}" ...>
    <p:column>
        <p:commandButton ... onclick="return jsMethod(#{table.rowIndex})" />
    </p:column>
</p:dataTable>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi. It works but with binding don't work SortBy in columns... Where is my mistake? Thank you! – MaximG Mar 15 '12 at 15:33
  • It works, table must be in request-scoped bean. http://myfaces.apache.org/orchestra/myfaces-orchestra-core/component-bindings.html – MaximG Mar 23 '12 at 11:08
  • Uh, no, you should not bind it to the bean, but to the view. Use exactly the code as above. Use `#{table}` and not `#{someBean.table}`. – BalusC Mar 23 '12 at 11:09
  • @BalusC i'm facing the same issue so i want know,what is the type of table property in the managed bean is it an int ? – Amira Manai Aug 30 '12 at 10:23
  • 2
    @Amira: There's no `table` property in the managed bean. The code is just complete as-is. The component is been bound into the EL scope, not to a managed bean. – BalusC Aug 30 '12 at 10:24
  • get it now but in my case i want to retrieve the row number in my managed bean ,so i'll try this solution http://stackoverflow.com/questions/5698513/row-numbering-with-pdatatable – Amira Manai Aug 30 '12 at 10:31
  • your solution works well here's how i used it: – Amira Manai Aug 30 '12 at 10:55