0

I want to color a row of a datatable if the country column's text is 'UK'. Here is my XHTML file:

 <h:dataTable value="#{personBean.persons}" var="p" 
    rowStyleClass="#{p.country eq 'UK' ? 'colored' : null}">
            
       <h:column >
            <f:facet name="header">Country</f:facet>
             "#{p.country}"
      </h:column>
 </h:dataTable>

Here is my CSS file:

.colored {
   background-color: #cceeff;
   color: #FFFFFF;
}

I can't achieve that a row is colored.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
v8rs
  • 197
  • 4
  • 17
  • Its working for me. Check in the chrome console whether CSS file is linked properly to your page and clear the catch then try again. also remove the double quote from `"#{p.country}"` in your case. – Ahmed Raaj Jan 03 '18 at 06:48

1 Answers1

0

Native JSF h:dataTable will not be evaluate rowStyleClass on per row basis.

It will happen in Primefaces p:dataTable.

One alternative you can do is you have build a String of comma separated style classes at your ManagedBean and supply it to rowStyleClass attribute.

For Example:
ManagedBean:

String rowStyles;
public void getRowStyles(){
    for(Person p: persons){
      rowStyles+=p.getCountry().equals("UK") ? "colored," : ",";
    }

    return rowStyles;
}

XHTML:

<h:dataTable rowStyleClass="#{personBean.rowStyles}" ...>
Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92