How to display the row index in a JSF datatable

datatablejsf

In a JSF dataTable I want to display the row index next to the rows… like:

Column A    Column B
1           xxx
2           yyy

I thought that I could use an implicit el variable like #{rowIndex} but this is not working.

A solution I found is to create a binding for the data table and use the binding like:

<h:dataTable var="item" value="#{controller.items}" binding="#{controller.dataTable}">
    <h:column>#{controller.dataTable.rowIndex}</h:column>
    <h:column>value</h:column>
</h:dataTable>

but this solution is complex and doesn't work well when I have many nested dataTables in a page.

Any ideas on how to solve this in a better way?

Best Solution

This solution was posted by Jamie Williams on CodeRanch. He says it works with Tomahawk. I'm using primefaces, which also supports it.

<t:dataTable rowIndexVar="row" value="#{someBean.value}">  
    <h:column>  
        <h:outputText value="#{row + 1}"/>  
    </h:column>  
</t:dataTable>
Related Question