Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have PageBlockTable where i want to put different colors for cells based on some condition

<apex:pageBlockTable value="{!data}" var="rec" width="90%">
    <apex:column>
        <apex:facet name="header">Values</apex:facet>           
        <div style="text-align:center;font-weight:bold; background-color:{!IF(rec.age ==20,'red', 'blue')}">
            <apex:outputText value="{!rec.age}"/>
        </div> 
     </apex:column>
</apex:pageBlockTable>

The condition i have to check some thing like

if(age==20)
   background-color:red;
else if(age>20 && age<40)
   background-color:green;
else if(age>40 && age<55)
   background-color:blue;
else
   background-color:pink;

how can i achieve this.?

Please help

share|improve this question

1 Answer 1

up vote 6 down vote accepted

The CASE function handles this better than the IF function:

background-color:{!CASE(rec.age <= 20, 'red', rec.age > 20 && rec.age <= 40, 'green', rec.age > 40 && rec.age <= 55, 'blue', 'pink')}

Take care to cover all the possible age values: your pseudo code mapped 40 to pink.

share|improve this answer
    
thanks for reply.I will check this –  NullReferenceException Apr 24 at 18:40
    
@NullReferenceException But, when possible, calculate the value in the controller and express it as a variable. Expressions take up "more space" than simple variables, so if you have many rows of data, you could reach your view state size limit sooner. –  sfdcfox Apr 25 at 2:57
    
@sfdcfox Yes i have many rows.can you please tell me how to do it. –  NullReferenceException Apr 25 at 3:21
2  
In the wrapper class: public string getColor() { if(age == 20) return 'red'; if(age < 40) return 'green'; if(age < 55) return 'blue'; return 'pink'; }; in the page: background-color: {!rec.color} –  sfdcfox Apr 25 at 3:26
    
@sfdcfox do i need to write seperate class for the same.?I have 3 columns Age , Age1, Age2 now i am writing style for each column for these 3.It would be great if it is possible to return the colors for 3 column.How can i create wrpper class for the perticular list and send the cell values to it. –  NullReferenceException Apr 25 at 3:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.