Skip to content Skip to sidebar Skip to footer

Row Counter For Html Table Row

I have HTML table in JSF web application. I am generating rows dynamically using . I want a counter for each row. How can I get this? Any help? Ssimilar to rowKeyV

Solution 1:

As of Facelets 2.0, this is now possible using the varStatus.

<ui:repeat varStatus="status" var="user" value="#{collection}">
     #{status.index}
</ui:repeat>

Solution 2:

Since you are using richfaces, you can do it with its iteration tag (<a4j:repeat>), which is a bit more appropriate than using <c:forEach>, and which is like an extension to <ui:repeat>

<table><a4j:repeatvalue="#{bean.list}"var="item"rowKeyVar="idx"><tr><td><h:outputTextvalue="#{idx + 1}" /></td><td><h:outputTextvalue="#{item.someProperty}" /></td><td><h:outputTextvalue="#{item.otherProperty}" /></td></tr></a4j:repeat></table>

Solution 3:

You can't do it nicely with the ui:repeat, but you can do so with the h:dataTable. Bind the component to a UIData property in the backing bean. It has a getRowIndex() method which does exactly what you need:

<h:dataTablebinding="#{bean.table}"value="#{bean.list}"var="item"><h:column><h:outputTextvalue="#{bean.table.rowIndex + 1}" /></h:column><h:column><h:outputTextvalue="#{item.someProperty}" /></h:column><h:column><h:outputTextvalue="#{item.otherProperty}" /></h:column></h:dataTable>

Here I am adding 1 to UIData#getRowIndex() because it is zero-based. You may find the Using datatables article useful to learn more about datatables.

If you actually want a bit more control over the table layout (especially using colspans/rowspans, which are lacking in the real JSF h:dataTable tag), then an alternative is to use the JSTL c:forEach tag which has a varStatus attribute which gives you a handle to the loop status.

<table><c:forEachitems="#{bean.list}"var="item"varStatus="loop"><tr><td><h:outputTextvalue="#{loop.index + 1}" /></td><td><h:outputTextvalue="#{item.someProperty}" /></td><td><h:outputTextvalue="#{item.otherProperty}" /></td></tr></c:forEach></table>

Solution 4:

There is no counter for each row in ui:repeat. As BalusC said you can go for h:datatable. Another idea is indirectly you can add a additional index method in bean for each object in the list in serverside and get that in jsf and manipulate it.

Solution 5:

A bad (or good idea) is using JavaScript to do that trick.

<script>var numberOfIndex = 0;
</script><ui:repeatvalue="#{modelBean.listUser}"var="item"><tr><td><script>numberOfIndex -=-1;document.write(numberOfIndex);</script></td><td>${item.id}</td><td>${item.name}</td></tr></ui:repeat>

Post a Comment for "Row Counter For Html Table Row"