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
Max_gMax_g 

How to change color for output text.

I need to be able to conditionally change the color of output text based on the value of a fi\eld in a custom object.  How do I code this in my VF page?

 

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

Please refer below code, I had done something similar.

 

Visualforce code:

 

<apex:page controller="dynamicTextColor_Controller">
    <apex:pageblock title = "My Opportunities">
        <apex:pageBlockTable value="{!Opps}" var="o">
            <apex:column value="{!o.Name}" />
            <apex:column headerValue="Amount">
                <apex:outputText value="{!o.Amount}" style="color:{!IF(o.Amount < 100000, 'red', 'blue')};"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageblock>
</apex:page>

 

Apex Controller code :

 

public with sharing class dynamicTextColor_Controller
{
    private List<Opportunity> lstOpps;  // we will color records in this based on amount

    public dynamicTextColor_Controller()
    {}
    
    public List<Opportunity> getOpps()
    {
        lstOpps = [Select Name, Amount From Opportunity Where Amount != NULL Limit 20];
        return lstOpps;
    }    
    
}

 

Let me know if any questions!

 

All Answers

vishal@forcevishal@force

Hi,

 

Please refer below code, I had done something similar.

 

Visualforce code:

 

<apex:page controller="dynamicTextColor_Controller">
    <apex:pageblock title = "My Opportunities">
        <apex:pageBlockTable value="{!Opps}" var="o">
            <apex:column value="{!o.Name}" />
            <apex:column headerValue="Amount">
                <apex:outputText value="{!o.Amount}" style="color:{!IF(o.Amount < 100000, 'red', 'blue')};"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageblock>
</apex:page>

 

Apex Controller code :

 

public with sharing class dynamicTextColor_Controller
{
    private List<Opportunity> lstOpps;  // we will color records in this based on amount

    public dynamicTextColor_Controller()
    {}
    
    public List<Opportunity> getOpps()
    {
        lstOpps = [Select Name, Amount From Opportunity Where Amount != NULL Limit 20];
        return lstOpps;
    }    
    
}

 

Let me know if any questions!

 

This was selected as the best answer
Max_gMax_g

Thanks for the help.  Worked great.

 

neao18neao18

thanks  for the help....!!! it works..!!!

Deepak pal 9Deepak pal 9
what if i  have multiple conditon on the same field for formating  like the colour could be  red blue green or more  based on vlaue.