Nikhil
Nikhil

Reputation: 2883

Multiple condition on rowStyleClass in dtata table PrimeFaces

I am using primefaces for the first time in my web project and colored my data table rows dynamically using rowStyleClass like
rowStyleClass="#{alar.severity eq 'Major' ? 'major' : null}"
But is there any way to check multiple conditions in the rowStyleClass? I want to check the severities Major, Minor, and Normal. How can I execute multiple conditions?

Upvotes: 2

Views: 7950

Answers (2)

user1983983
user1983983

Reputation: 4841

In think in your case and in general if you use the lower case severity as row class and you dont want to exclude any severities, the following should do the trick:

rowStyleClass="#{not empty alar.severity ? alar.severity.toLowerCase() : null}"

Another more extensible and more readable solution would be to define a bean-method which takes alar as parameter and returns the styleclass:

public String alarStyleClass(Alar alar) {
    if(alar.severity.equals("Major")) return "major";
    if(alar.severity.equals("Minor")) return "minor";
    if(alar.severity.equals("Normal")) return "normal";
    return null;
}

And for the rowStyleClass:

rowStyleClass="#{bean.alarStyleClass(alar)}"

The last possible solution that comes to my mind would be to do all the checks directly in the rowStyleClass-attribute:

rowStyleClass="#{alar.severity eq 'Major' ? 'major' : 
                 alar.severity eq 'Minor' ? 'minor' :
                 alar.severity eq 'Normal' ? 'normal' :
                 null}"

Upvotes: 4

Manuel
Manuel

Reputation: 4258

Just cascade the conditions:

rowStyleClass="#{alar.severity eq 'Major'  ? 'major'  : 
                (alar.severity eq 'Normal' ? 'normal' : 
                (alar.severity eq 'Minor'  ? 'minor' : null))}">

But it can get unclear fast, so don't cascade too often.

Upvotes: 4

Related Questions