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 of the car as rowStyleClass="#{car.year le 1975 ? 'colored' : null}"
. Arithmetic, logical, or relational operators of the JSF Expression Language can be used to define the condition.
Note
For more on the...