Conditional coloring in dataTable

The dataTable component provides conditional coloring on rows, which can be styled based on conditions.

How to do it...

A basic definition of a color-coded table that displays a list of cars follows:

<p:dataTable id="coloring" var="car" value="#{dataTableController.cars}"
rowStyleClass="#{car.year le 1975 ? 'colored' : null}">
<p:columnheaderText="Year">#{car.year}</p:column>
<p:columnheaderText="Name">#{car.name}</p:column>
</p:dataTable>

The colored style definition could be as simple as the following:

<style type="text/css">
.colored {
background-color: #FF0000;
color: #FFFFFF;
}
</style>

How it works...

With the rowStyleClass attribute, style class can be defined for each row according the manufacturing year ...

Get PrimeFaces Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.