Domanda

I am attempting to change the color of values in a list based on whether the ID in the field "Team__c" on the carrier object matches any ID in the list of users. My controller is returning the color one time and this color applies to all records (See debug log). All values in the list are blue, some should be red. Thank you in advance.

Visualforce code:::

<apex:pageblocktable value="{!carriers}" var="c">
    <apex:column headervalue="Carrier">
        <font color="{!color2}">
            <apex:outputtext value="{!c.name}"/>
        </font>
    </apex:column>

Controller:::

public string getcolor2() {
    list<carrier__c> carriers = [select team__c from carrier__c where team__c != NULL];

    list<user> users= new list<user>();
        users = [select id from user where userrole.name = 'Executive'];
            set<string> uid = new set<string>();
            for(user us: users){
                uid.add(us.id);
            } 

    string color = 'red'; 

    for(carrier__c car: carriers){
        system.debug('*****List of carriers: ' + carriers);
        system.debug('*****List of users: ' + uid);
        system.debug('*****Current carrier= '+car);
        if(uid.contains(car.team__c) ){
            color='blue';
            system.debug('***** Set color to:'+color);   
            }
    }
    system.debug('***** Returning color: ' + color);
    return color;
}

Debug Log::::

 *****List of carriers: (Carrier__c:{Team__c=005U0000001D3E5IAK,      Id=a0HJ0000003bl8nMAA}, Carrier__c:{Team__c=005J0000001EEIHIA4, Id=a0HJ0000003bitnMAA}, Carrier__c:{Team__c=005U0000001BHRKIA4, Id=a0HJ0000003eD64MAE})

 *****List of users: {005U0000001D3E5IAK}
 *****Current carrier= Carrier__c:{Team__c=005U0000001D3E5IAK, Id=a0HJ0000003bl8nMAA}
 ***** Set color to:blue

*****List of users: {005U0000001D3E5IAK}
*****Current carrier= Carrier__c:{Team__c=005J0000001EEIHIA4, Id=a0HJ0000003bitnMAA}

*****List of users: {005U0000001D3E5IAK}
*****Current carrier= Carrier__c:{Team__c=005U0000001BHRKIA4, Id=a0HJ0000003eD64MAE}

***** Returning color: blue
È stato utile?

Soluzione

You've only got one variable for color in your controller, so you can't expect to get more than one color in your VisualForce page. You'll want to create a class that stores both a carrier__c object and a color, and then create a list to store an instance of your object for each carrier__c.

public class colorAndObject {
    public string color{get; set;}
    public carrier__c carrier{get;set;}

    public colorAndObject(string carColor, carrier__c carrierName) {
        color = carColor;
        carrier = carrierName;
    }
}

list<colorAndObject> carriersWithColors = new list<colorAndObject>();

In your VisualForce page, instead of using {!carriers} for your table value, use your carriersWithColors list.

<apex:pageblocktable value="{!carriersWithColors }" var="c">
    <apex:column headervalue="Carrier">
        <font color="{!c.color}">
            <apex:outputtext value="{!c.carrier.name}" />
        </font>
    </apex:column>

That should be enough to get you started. I'll let you fill in the gaps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top