Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF outputtext conditional display/styling. Differently depending on condition

I`m using JSF and I have an output text per example "qwerty" that I want to display / format differently if some conditions will be met. So per example if:

  • cond1 is true then I want to display qwerty
  • cond1 is false then I want to display qwerty

and so on.

Is there a way?

like image 250
CyberGriZzly Avatar asked Mar 13 '13 21:03

CyberGriZzly


3 Answers

You can use a ternary operator in order to choose the style you want to apply depending on the condition:

<h:outputText value="qwerty" 
    style="#{backingBean.cond1 ? 'text-decoration:line-through;':'fontstyle:italic;'}">

And it would be even better if you use css classes in order of inline styles. Good luck!

Using the 'condition' inline in the EL is also possible, see using greater than logical expression in rendered attribute. Read also the discussion about using inline in the xhtml or in the javabean

like image 143
Xtreme Biker Avatar answered Oct 31 '22 17:10

Xtreme Biker


Neat way is to create CSS classes

<h:outputText value="qwerty" 
     styleClass="#{backingBean.cond1 ? 'classA' : 'classB'}" /> 
like image 26
Piotr Gwiazda Avatar answered Oct 31 '22 19:10

Piotr Gwiazda


In similar situations, I use following

<h:outputText value="yourValueFromYourBeanOrWhatever" styleClass="anArbitraryName#{managedBean.condition}"/>

And in my CSS file I define the classes:

.anArbitraryNametrue{}

and

.anArbitraryNamefalse{}
like image 6
Laabidi Raissi Avatar answered Oct 31 '22 18:10

Laabidi Raissi