How to increment apex:variable?

In order to increment apex:variable inside the apex:pageBlockTable, we have to include it inside apex:column in apex:pageBlockTable.

 <apex:page controller="sample">
    <apex:pageBlock >
    <apex:variable var="i" value="{!0}"/>
        <apex:pageBlockTable value="{!acnt}" var="acc">
            <apex:column >

                {!i}
                <apex:variable var="i" value="{!i+1}"/>
            </apex:column>
            <apex:column value="{!acc.Name}" rendered="{! IF((mod(i,2)) == 0, true, false)}" style="background:pink;"/>
            <apex:column rendered="{! IF((mod(i,2)) == 0, true, false)}" style="background:pink;" value="{!acc.Rating}"/>                                
            <apex:column value="{!acc.Name}" rendered="{! IF((mod(i,2)) == 1, true, false)}" style="background:yellow;"/>
            <apex:column rendered="{! IF((mod(i,2)) == 1, true, false)}" style="background:yellow;" value="{!acc.Rating}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


In case of apex:repeat, we can just write as below

<apex:variable var="i" value="{!0}"/>
<apex:repeat>
      <apex:variable var="i" value="{!i+1}"/>
</apex:repeat>

7 comments:

  1. Thank you! Very clear code, I appreciate the clarity around Page Block increments.

    ReplyDelete
  2. good example dear anyone can easily learn through this

    ReplyDelete
  3. This is undefined behavior. Salesforce says not to do it in their official documentation: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_variable.htm

    ReplyDelete
  4. This is undefined behavior. Salesforce says not to do it in their official documentation: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_variable.htm

    ReplyDelete
  5. can any one help this format.
    Create a VF page and write a controller, for vf page use tag to create a counter( default value set to 1 for apex variable) for each iteration using apex repeat tag(to increment counter).


    (1)jhonny
    (2)james
    (3)sonyl
    (4)jhonn

    ReplyDelete
  6. can any one help this format.
    Create a VF page and write a controller, for vf page use tag to create a counter( default value set to 1 for apex variable) for each iteration using apex repeat tag(to increment counter).


    (1)jhonny
    (2)james
    (3)sonyl
    (4)jhonn

    ReplyDelete
    Replies
    1. Mahesh, Have you got the solution?

      Delete