Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2 dataTable row index without dataModel

I've been using ui:repeat to generate tables. With ui:repeat it's easy to get the rows numbered using the varStatus. I'm depending on the row id's from varStatus when calling the backing bean to handle list navigation, for example moving an item up or down in the list.

Now I'd like to create a composite component that builds a customizable table with all functionality needed for marking rows, moving them up/down etc. To do this, the ui:repeat won't work as I can't see a way of handling different number of columns; I need to specify headings in one place and the body in another (and I've not reached the knowledge point where I can create a custom component). Therefore I went on to datatable instead, as using that means specifying the headers as facets at the same place as the body content.

That worked well until I noticed that for some reason there were no varStatus anymore. I have put a lot of work into handling list presentation without any need for a datamodel and I do not want to start using that. Is there any way that I can get the current row number as it is displayed in the table, without using datamodel?

Also I'm using viewScope and if I've understood correctly that means I cannot bind the dataTable to the bean.

like image 573
nivis Avatar asked Jan 31 '13 18:01

nivis


1 Answers

Just bind the table to the view itself instead of to a bean.

<h:dataTable binding="#{table}" ...>

Then you can use #{table.rowIndex} where necessary. E.g.

<h:column>#{table.rowIndex + 1}</h:column>

Note that the code is as-is and that the EL variable name table is fully to your choice.

See also:

  • How does the 'binding' attribute work in JSF? When and how should it be used?
like image 188
BalusC Avatar answered Sep 27 '22 19:09

BalusC