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
momorganmomorgan 

Dynamic rowClasses?

 Hello all. I've got a pageblockTable that is generated from a list, and I'd like to be able to style rows on-the-fly. The sensible thing to do seemed to be to build up a string of class names while iterating through the list (in Apex), then to spit out that string like so:

 

 

<apex:pageblockTable value="{!master}" var="m" rowClasses="{!masterRowClasses}">

 

Alas, this doesn't work. There's nothing wrong with the string that's generated by the Apex, as it'll work fine when pasted in place of the formula reference. The doco isn't explicit on the subject, so I'm left wondering if this can be done.

 

Anyone ever done this or something similar? If not, anyone care to have a go?

Thanks! :)

 

Edwin VijayEdwin Vijay

Are you able to achieve this when you hardcode the values like..

 

<apex:pageblockTable value="{!master}" var="m" rowClasses="class1,class2">

If yes, then paste your bit of code.. Also, view the HTML source that is generated, that would give you more insight on the actual value returned by the APEX class 

 

 

 

momorganmomorgan

Yes, it works hardcoded. But, as the contents of the list can change as per what's in the database. Therefore I'm trying to write this out dynamically, to follow the contents of the list.

 

As I mentioned, the doco is foggy as to whether this can be done -- you'd assume it could, but you know what they say about assumption...

Edwin VijayEdwin Vijay

Can you post your code here? I can try to identify the possible error..

 

And yes, the document does'nt cover this topic, But when you view the actual generated HTML you can atleast come to a conclusion if it can be done or not..

momorganmomorgan

The actual code I'm working on is a pretty daunting sight, so just as an example I've cobbled together some code based on this example elsewhere on the boards that should roughly illustrate the idea. That previous thread covers the intended functionality of most of the code, and I've just added in the formatting stuff. It ain't the prettiest thing ever, but still.

 

Here's the Apex:

 

public class PersonExtension{
public string sRowClasses;

public string getRowClasses(){
return sRowClasses;
}

private final Contact person;

public PersonExtension(ApexPages.StandardController stdController){
this.person= (Contact)stdController.getRecord();
}
public class People {
contact c;
public People(Contact cin) { c = cin; }
public Contact getContact(){ return c;}
public String getAgeType() {
if (c.Age__c == null ){
sRowClasses = sRowClasses + 'cUnknown,'
return 'unknown';
}else if (c.age__c < 21){
sRowClasses = sRowClasses + 'cMinor,'
return 'minor';

}else{
sRowClasses = sRowClasses + 'cAdult,'
return 'adult';
}
}
}
List<People> peeps = new List<People>{};
public List<People> getPersonList() {
peeps.clear();
for (contact cc: [Select p.Id, p.Name, p.Age__c from Contact p order by lastname limit 10] ) {
peeps.add( new People (cc) );
}
return peeps;
}

public String getAnyString(){
return 'Anything';
}
}

 

 And here's the VisualForce:

 

 

<apex:page standardController="Contact" extensions="PersonExtension">
<style>
.cUnknown{
background:#cccccc;
}
.cMinor{
background:#ff9999;
}
.cAdult{
background:#99ff99;
}
</style>
<apex:form>
<apex:pageblock title="My Persons Infos">
<apex:pageblockList value="{!PersonList}" var="p" rowclasses="{!RowClasses}">
<apex:column value="{!p.Contact.Name}"/>
<apex:column value="{!p.Contact.Age__c}"/>
<apex:column value="{!p.ageType}" headerValue="Adultness" />
<apex:column value="{!anystring}" headerValue="my String" />
</apex:pageblockList>
</apex:pageblock>
</apex:form>
</apex:page>

 

So the idea is that the styling can't be hard-coded, as the contents of the list depends on what's in the database.

 

 

Message Edited by momorgan on 06-30-2009 08:25 AM
momorganmomorgan
Anyone?
ryanhcaryanhca

Did you ever find a solution to this? I'm trying to do the same thing and I tried the exact same approach as you did before I found your post...

 

The only thing I can think of is to put the class name for the row into a hidden variable in the row and have javascript go through the table and render it accordingly...

Keith654Keith654
I just hit the same problem in Spring 10...
momorganmomorgan

Yes, I'm not surprised. It's another of those irksome things that lacks the severity of a "bug", but doesn't have the gravitas required for a successful "idea". Lots of things fall through the cracks this way, sadly.

 

My hackaround was to abandon pageblockTable and go back to classical HTML...