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
BrokenBirdBrokenBird 

passing value to apex

Ok I have read a lot about this, but still can't figure it out, I must be dumber than I thought.

 

Anyway I need to pass a value to my Controller from my page in a datatable:

 

 

<apex:column styleClass="{!ScheduleStatusClass}"> <apex:facet name="header"><b>Schedule Status</b></apex:facet> <apex:outputField value="{!aProject.Schedule_Status__c}" /> </apex:column>

 


  

 

You see the ScheduleStatusClass is a method that would return a style based on the content  of {!aProject.Schedule_Status__c, but I can't find a way to get the {!aProject.Schedule_Status__c} back to my controller for my {!ScheduleStatusClass} method to see. I have been at it for 3 hours and this is driving me insane, please help!!! :smileysad:

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

Here is one, http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=3850&query.id=90162#M3850 , you can also do an advanced search for wrapper and that should return a lot of info. There is also this, http://wiki.developerforce.com/index.php/Wrapper_Class

 

If you want to maintain your method function to control the styles this is what you would need to do. I only threw this together in notepad so there may be some syntax type errors but it should give you the right idea.

 

public class wrapperClassController {

//Our collection of the class/wrapper objects cContact
public List<cContact> contactList {get; set;}

//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts(){
if(contactList == null){
contactList = new List<cContact>();

for(Contact c : [select Id, Name, Email, Phone from Contact limit 10]){
String style = getStyle(c);
contactList.add(new cContact(c,style));
}
}
return contactList;
}

public String style getStyle(Contact con){
String style = 'green';
if(con.Email.contains('.gov')){
style = 'red';
}

return style;
}

public class cContact{
public Contact con {get; set;}
public String style {get; set;}

public cContact(Contact c, String style){
this.con = c;
this.style = style;
}
}
}


<apex:page controller="wrapperClassController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="c" id="table">
<apex:column value="{!c.con.Name}" />
<apex:column headerValue="Email">
<apex:outputField value="{!c.con.Email}" styleClass="c.style"/>
</apex:column>
<apex:column value="{!c.con.Phone}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Message Edited by TehNrd on 04-07-2009 10:52 AM

All Answers

TehNrdTehNrd
Please post the Apex code portion. Can't be of much help if you can only see half of what is going on.
gtuerkgtuerk

set the Schedule Status value in a page action method that is called out of your apex page.  lifecycle of the page is

 

controller constructor

page action

controller binding

 

by the time you need the attribute, the method has already fired.

 

worth a shot, eh?

BrokenBirdBrokenBird

private String currentScheduleStatus { get; set; } public String getScheduleStatusClass() { if (currentScheduleStatus == 'In-Progress - On Schedule' || currentScheduleStatus == 'In-Progress - Ahead of Schedule') { return 'highlightGreen'; } else { return 'highlightRed'; } }

 

BrokenBirdBrokenBird
Actually the value changes for each row of the datatable, so this won't work. I wish this language would just support custom functions with parameters... Would be so much easier to call {!ScheduleStatusClass({!aProject.Schedule_Status__c})}

Message Edited by BrokenBird on 04-06-2009 02:54 PM
TehNrdTehNrd

If all you want to do is change the color of text based on the value you can do something like this:

 

 

<apex:outputField value="{!aProject.Schedule_Status__c}" styleClass="{!if(OR(aProject.Schedule_Status__c='In-Progress - On Schedule','In-Progress - Ahead of Schedule'),'highlighGreen', 'highlightRed')}"/>

 

 

 

Message Edited by TehNrd on 04-06-2009 03:06 PM
BrokenBirdBrokenBird

Yes I understand using if in the style, but I use this method because the real thing is quite more complex than the snippet of code shown in my post. This is why I am using a method.

 

Thanks

jwetzlerjwetzler
TehNrd has posted many examples on the forums (Jason can you point to one?) of using a wrapper object for your iterating components.  That way your wrapper object can have a styleClass property that returns the correct style based on your field value.
TehNrdTehNrd

Here is one, http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=3850&query.id=90162#M3850 , you can also do an advanced search for wrapper and that should return a lot of info. There is also this, http://wiki.developerforce.com/index.php/Wrapper_Class

 

If you want to maintain your method function to control the styles this is what you would need to do. I only threw this together in notepad so there may be some syntax type errors but it should give you the right idea.

 

public class wrapperClassController {

//Our collection of the class/wrapper objects cContact
public List<cContact> contactList {get; set;}

//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts(){
if(contactList == null){
contactList = new List<cContact>();

for(Contact c : [select Id, Name, Email, Phone from Contact limit 10]){
String style = getStyle(c);
contactList.add(new cContact(c,style));
}
}
return contactList;
}

public String style getStyle(Contact con){
String style = 'green';
if(con.Email.contains('.gov')){
style = 'red';
}

return style;
}

public class cContact{
public Contact con {get; set;}
public String style {get; set;}

public cContact(Contact c, String style){
this.con = c;
this.style = style;
}
}
}


<apex:page controller="wrapperClassController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="c" id="table">
<apex:column value="{!c.con.Name}" />
<apex:column headerValue="Email">
<apex:outputField value="{!c.con.Email}" styleClass="c.style"/>
</apex:column>
<apex:column value="{!c.con.Phone}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Message Edited by TehNrd on 04-07-2009 10:52 AM
This was selected as the best answer
BrokenBirdBrokenBird
Now this is fantastic!! I can't thank you enough!