Visualforce Row Counter for Iteration Components
April 2nd, 2010
I have been working on a Visualforce page that displays a list of items from a collection and I want to display the current row number next to each item. I found this post that describes a solution but I think there may be a bug in one of the components so here is proposed work around. I want to display a collection like this:
![]()
It seems that there may be a bug in the way method works with the dataTable component. It works correctly with the following components.
Repeat Component
1 2 3 4 5 6 | <apex:variable value="{!1}" var="rowNum"/>
<apex:repeat value="{!myCollection}" var="item">
<apex:outputText value="{!FLOOR(rowNum)}"/> - <apex:outputField value="{!item.Name}"/><br/>
<apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:repeat> |
1 2 3 4 5 6 | <apex:variable value="{!1}" var="rowNum"/>
<apex:dataList value="{!myCollection}" var="item">
<apex:outputText value="{!FLOOR(rowNum)}"/> - <apex:outputField value="{!item.Name}"/>
<apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:dataList> |
![]()
DataTable Component – Does not work??
1 2 3 4 5 6 7 8 9 10 11 | <apex:variable value="{!1}" var="rowNum"/>
<apex:dataTable value="{!myCollection}" var="item">
<apex:column>
<apex:outputText value="{!FLOOR(rowNum)}"/>
</apex:column>
<apex:column >
<apex:outputField value="{!item.Name}"/>
</apex:column>
<apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:dataTable> |
![]()
So the solution that I came up with is to wrap my collection in an wrapper class implemented as an inner class in the controller.
Controller fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private void myMethod() {
Integer counter = 0;
for ((SomeObject__c item : [Select Name from SomeObject__c]) {
counter = counter + 1;
// add the wrapper to the collection
myCollection.add(new DataTableWrapper(item, counter));
}
}
// inner class
class DataTableWrapper {
public Integer counter { get; set; }
public SomeObject__c item { get; set;}
public DataTableWrapper(SomeObject__c item, Integer counter) {
this.item = item;
this.counter = counter;
}
} |
Visualforce fragment
1 2 3 4 5 6 7 8 | <apex:dataTable value="{!myCollection}" var="wrapper" columns="2">
<apex:column>
<apex:outputText value="{!wrapper.counter}."/>
</apex:column>
<apex:column>
<apex:outputField value="{!wrapper.item.Name}"/>
</apex:column>
</apex:dataTable> |
Categories: Apex, Code Sample, Salesforce, Visualforce













Hey there!
I also encountered this behavior and this is my way to work around this:
Use unary numbering like this, to avoid an extra controller:
{!lineItem.Name}
Maybe you like my solution. For my needs, it “just works”(™)
Greetings,
Fabian
Okay, when you’re moderating these Posts, maybe you can insert the following:
<apex:variable var=”positioncount” value=”|”/>
<apex:repeat value=”{!LineItems}” var=”lineItem” id=”table”>
<tr><td><apex:outputText value=”{!LEN(positioncount)}”><apex:variable var=”positioncount” value=”{!positioncount+’|'}”/>
</td><td”>{!lineItem.Name}</td></tr>
</apex:repeat>
Thx Jeff..this definitely saves my time. UR AMAZING
can’t you just move the inside the apex:column tag?
oops meant move the rownum+1 inside the apex:column tag
The dataList component has a ‘type’ attribute, which will display an ordered list when set to “1″ (other values: “a”, “A”, “i”, or “I” & “disc”, “square”, or “circle”) -
E.g.
Hi there,
In case noone else has pointed it out, the reason you rowNum didn’t increment is because it is not inside a column. dataTable ignored it because it was not within your apex:column tag.
For your example, this would be:
Hope this helps.
John