function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramesh KumarRamesh Kumar 

How to display <apex:repeat> row background with alternative colour ?

I am facing aProblem.I want to show a table having row with alternative background color.i am displaying data using <apex:repeat>.I cant use pageblocktable or any other table method .I have to complete my problem with apex repeat only .
Please anyone Suggest?
Best Answer chosen by Ramesh Kumar
Suneel#8Suneel#8
You might need to use a Wrapper class to acheive this as below.
VF Page code
<apex:page Controller="GetColor">
    <style type="text/css">
        .p { background-color: #eec; font-weight: bold; }
        .q { background-color: #eee; font-weight: bold; }        
    </style>
    <table>
    <th>Account Name</th>
    <apex:repeat value="{!lWrappers}" var="a">
        <tr>
        <td class="{!IF(a.color,'p','q')}" >
            {!a.name}
        </td>
        </tr>
    </apex:repeat>
    </table>
</apex:page>
Controller Code
public class GetColor {
    public GetColor() {
        color=true;
        lWrappers=new List<AccountWrapper>();
        for(Account a:[select name from Account]){
            lWrappers.add(new AccountWrapper(a.name,color));
            color=!color;
        }
    }
    public class AccountWrapper {
        public String name{get;set;}
        public boolean color{get;set;}
        public AccountWrapper(String n,boolean b){
            this.name=n;
            this.color=b;
        }
    }
    public List<AccountWrapper> lWrappers{get;set;}
    public boolean color{get;set;}
}

 

All Answers

Suneel#8Suneel#8
You might need to use a Wrapper class to acheive this as below.
VF Page code
<apex:page Controller="GetColor">
    <style type="text/css">
        .p { background-color: #eec; font-weight: bold; }
        .q { background-color: #eee; font-weight: bold; }        
    </style>
    <table>
    <th>Account Name</th>
    <apex:repeat value="{!lWrappers}" var="a">
        <tr>
        <td class="{!IF(a.color,'p','q')}" >
            {!a.name}
        </td>
        </tr>
    </apex:repeat>
    </table>
</apex:page>
Controller Code
public class GetColor {
    public GetColor() {
        color=true;
        lWrappers=new List<AccountWrapper>();
        for(Account a:[select name from Account]){
            lWrappers.add(new AccountWrapper(a.name,color));
            color=!color;
        }
    }
    public class AccountWrapper {
        public String name{get;set;}
        public boolean color{get;set;}
        public AccountWrapper(String n,boolean b){
            this.name=n;
            this.color=b;
        }
    }
    public List<AccountWrapper> lWrappers{get;set;}
    public boolean color{get;set;}
}

 
This was selected as the best answer
Ramesh KumarRamesh Kumar
Thanks Mr. suneel !