Friday, January 23, 2015

How to use apex:variable in Visualforce Page?

How to use apex:variable in Visualforce Page?


A local variable that can be used as a replacement for a specified expression within the body of the component. Use < apex:variable > to reduce repetitive and verbose expressions within a page.
Note: < apex:variable > does not support reassignment inside of an iteration component, such as < apex:dataTable > or < apex:repeat >. The result of doing so, e.g., incrementing the < apex:variable > as a counter, is unsupported and undefined.
 

This tag supports following attributes:


Attribute
Description
id
An identifier that allows the component to be referenced by other components in the page.
rendered
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
value
The expression that can be represented by the variable within the body of the variable component.
var
The name of the variable that can be used to represent the value expression within the body of the variable component.

Code Example:

<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:page>


Class:

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;
    }
}





That's it. 

More about VF Tags

http://sfdcsrini.blogspot.com/2014/06/visualforce-form-tags-with-examples.html


0 comments:

Post a Comment

 
| ,