Thursday, 25 May 2017

How to use apex:variable in Visualforce Page?

1. I am displaying table of the accounts data on the page using '<apex:pageBlockTable>' .
2. Displaying accounts with rating not equals to NULL and iterate on page.
3. In table there is three column , first is showing increment of variable, second is for account name and third is showing rating.
4. Background color provided in to the columns and it will display according to 'Mod' condition in the rendered attribute.
5. In rendered attribute , Mod conditions are given and it will check variable value mod by 2--
    if mod(i,2)==0, it will show green background color.
    if mod(i,2)==1, it will show violet background color.


Visualforce Page:

<apex:page controller="VariableCtr" showHeader="false" sidebar="false">
    <apex:pageBlock >
    <apex:variable var="i" value="{!0}"/>
    <apex:pageBlockTable value="{!showAccount}" 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:green;"/>
            <apex:column rendered="{! IF((mod(i,2)) == 0, true, false)}" style="background:green;" value="{!acc.Rating}"/> 
                                            
            <apex:column value="{!acc.Name}" rendered="{! IF((mod(i,2)) == 1, true, false)}" style="background:violet;"/>
            <apex:column rendered="{! IF((mod(i,2)) == 1, true, false)}" style="background:violet;" value="{!acc.Rating}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

In case of apex:repeat, you can replace apex:repeat by apex:pageBlockTable --
    <apex:page controller="VariableCtr" showHeader="false" sidebar="false">
    <style>
    table 
    {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }
    
    td, th {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
    }
    
    </style>
    
    <table>
      <tr>
        <th>Increment</th>
        <th>Account Name</th>
        
      </tr>
      <apex:variable var="i" value="{!0}"/>
      <apex:repeat value="{!showAccount}" var="acc">
          <tr>
            <td>
                 {!i}
                <apex:variable var="i" value="{!i+1}"/>
            </td>
            <td style="display:{! IF((mod(i,2)) == 0, "block", "none")};background:green;">{!acc.Name}</td>
            <td style="display:{! IF((mod(i,2)) == 1, "block", "none")};background:violet;">{!acc.Name}</td>
          </tr>
      </apex:repeat>
    </table>
</apex:page>

Apex Class:

public class VariableCtr
{
    public List<Account> getShowAccount()
    {
        List<Account> lstAcc=new List<Account>([select id,name,rating from Account where rating!=null]);
        return lstAcc;
    }
}




0 comments:

Post a Comment