JSF 2 dataTable row index without dataModel

nivis picture nivis · Jan 31, 2013 · Viewed 13.6k times · Source

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.

Answer

BalusC picture BalusC · Jan 31, 2013

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>