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
<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>
DataList Component
<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??
<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
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
<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>
</p>