This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSF and the fly likes Setting the Style Dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Setting the Style Dynamically" Watch "Setting the Style Dynamically" New topic
Author

Setting the Style Dynamically

Fezekile Futhwa
Greenhorn

Joined: Aug 03, 2011
Posts: 5
I am completely new to JSF.

I am rendering a dataTable with several columns, a few columns must display different styles based on column data.

E.g.
<p:column headerText="Monday" style="width:100px" styleClass="">
<hutputText value="#{rowVar.monday}" />
</p:column>
<p:column headerText="Tuesday" style="width:100px" styleClass="">
<hutputText value="#{rowVar.tuesday}" />
</p:column>
<p:column headerText="Wednesday" style="width:100px" styleClass="">
<hutputText value="#{rowVar.wednesday}" />
</p:column>

I want to set the styleClass property to different styles based on the value of #{rowVar.*}. Like:
if (#{rowVar.*} == "A")
bold
else if (#{rowVar.*} == "B")
italic
else
normal

I have difined the styles at the top of the page. How do I evalute the condition in the styleClass property?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

Welcome to the Javaranch, Fezekile!

You can use EL expressions to retrieve style information for column definitions from a backing bean property. That's assuming that you really wanted to define the style for the whole column. If you're looking to style individual cells based on their contents, that's a different matter.


Customer surveys are for companies who didn't pay proper attention to begin with.
Fezekile Futhwa
Greenhorn

Joined: Aug 03, 2011
Posts: 5
Thanks.

Unfortunately the styles are set at cell level! So far it works fine for two or less, but am trying to figure out how to do it for more than two cells.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

One way to do it is to wrap the cell in a 1x1 panelGrid. I'll forgive you if you find this overkill and use a div element as a wrapper, though. You can use EL as a div style expression thanks to a loophole in how EL works on a JSF View.

However, to control appearances on a cell-by-cell basis, you're going to have to have a property in the tablemodel row to return the style of that cell, which means that your actual tablemodel row class may need to be implemented as decorator for the underlying data instead of using the underlying data directly.
Fezekile Futhwa
Greenhorn

Joined: Aug 03, 2011
Posts: 5
Mmm, I hear you. I had expected this to be a simple thing!!

But it seems like I may have to rethink my implementation then..... thanks though.

ps: As an afterthought, is there any particular reason why JSF does not lend itself to being parametised? This would have been so simple, if only I could call a JavaBean with a parameter(s)!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

Fezekile Futhwa wrote:

ps: As an afterthought, is there any particular reason why JSF does not lend itself to being parametised? This would have been so simple, if only I could call a JavaBean with a parameter(s)!


Because JSF isn't a "programming" environment, it's a Model/View/Controller environment and Views are supposed to be display definitions, not executable code. And, for that matter, you can't "call a JavaBean" anyway - you invoke a method defined for that bean - if you want to be pedantic about it.

JSF2 is a little more amenable to View parameterization than JSF1 was, but people often abuse the privilege.
Fezekile Futhwa
Greenhorn

Joined: Aug 03, 2011
Posts: 5
Okay......I had assumed more options for the conditional operator than is available.

I have been liking JSF very much so far......!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting the Style Dynamically