Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Conditional coloring in dataTable


The dataTable component provides conditional coloring on rows, which can be styled based on conditions. The row styling utilizes the rowStyleClass attribute that has a condition as the EL expression.

In this recipe, we will demonstrate the conditional coloring on rows for countries with GDP (gross domestic product) less than $3,500,000.

How to do it…

A basic definition of a color-coded table that displays a list of countries with their GDPs is given here:

<p:dataTable value="#{dataTableColoringBean.countryGdpList}"
  var="countryGdp"
  rowStyleClass="#{countryGdp.gdp le 3500000 ? 'colored' : ''}">
  <p:column headerText="Name" sortBy="#{countryGdp.name}">
    #{countryGdp.name}
  </p:column>
  <p:column headerText="GDP (Millions of US $)">
    #{countryGdp.gdp}
  </p:column>
</p:dataTable>

The colored style definition used in rowStyleClass could be as simple as the following:

<style type="text/css">
  .colored {
    background...