VisualForce Counter Using <apex:Variable> - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

VisualForce Counter Using <apex:Variable>

One of the nice Visualforce tags, but not often used is the <apex:variable> tag. In this sample, we'll use this tag to create a counter to enumerate each iteration of the <apex:repeat> tag.

This is the Visualforce code:

<apex:page controller="repeaterCon">
    <apex:variable value="{!1}" var="rowNum"/>
    <apex:repeat value="{!collection}" var="row">
        ({!rowNum}){!row}<br/>
        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
    </apex:repeat>
    <apex:dataList value="{!collection}" var="row" type="1">
        {!row}
    </apex:dataList>
</apex:page>

And this is the Apex controller:

public class repeaterCon {
    public List<String> collection {
        get {
            if (collection == null) {
                collection = new List<String>();
                for (Account a : [SELECT ID, Name FROM Account LIMIT 10]) {
                    collection.add(a.Name);
                }
            }
            return collection;
        }
        private set;
    }
}

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez