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: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}"/>

The condition i have to check some thing like

else if(age>20 && age<40)
else if(age>40 && age<55)

how can i achieve this.?

Please help

1 Answer 1


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.

PS Ignore this answer - as crop1645 points out this is not how CASE works. I think the OP must have got their answer from the comment thread so I won't delete this as that would also delete the comments.

  • @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, 2014 at 2:57
  • @sfdcfox Yes i have many rows.can you please tell me how to do it. Apr 25, 2014 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, 2014 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. Apr 25, 2014 at 3:32
  • @NullReferenceException No, they all go in the same class; just add one function per each field that has dynamic coloring.
    – sfdcfox
    Apr 25, 2014 at 3:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .