Certain rows can be styled based on a condition using rowStyleClass. Table below highlights cars that are manufactured before 1990.
DocumentationId | Year | Brand | Color |
---|---|---|---|
792bcbf3 | 1981 | Jaguar | Green |
98314a32 | 2005 | Volkswagen | Maroon |
0c8618d8 | 1973 | Honda | Blue |
49e042c8 | 1977 | BMW | Maroon |
10b22136 | 1978 | Audi | Orange |
170e09c4 | 1989 | Audi | Blue |
7aeb6c15 | 2002 | Jaguar | Yellow |
198923bc | 2008 | Ford | Black |
fc08fa31 | 1995 | Audi | Brown |
0631e7f5 | 1969 | Jaguar | Red |
<style type="text/css"> .old { background-color: #fca752 !important; background-image: none !important; color: #000000 !important; } </style> <p:dataTable var="car" value="#{dtBasicView.cars}" rowStyleClass="#{car.year le 1990 ? 'old' : null}"> <p:column headerText="Id"> <h:outputText value="#{car.id}" /> </p:column> <p:column headerText="Year"> <h:outputText value="#{car.year}" /> </p:column> <p:column headerText="Brand"> <h:outputText value="#{car.brand}" /> </p:column> <p:column headerText="Color"> <h:outputText value="#{car.color}" /> </p:column> </p:dataTable>
@Named("dtBasicView") @ViewScoped public class BasicView implements Serializable { private List<Car> cars; @Inject private CarService service; @PostConstruct public void init() { cars = service.createCars(10); } public List<Car> getCars() { return cars; } public void setService(CarService service) { this.service = service; } }
@Named @ApplicationScoped public class CarService { private final static String[] colors; private final static String[] brands; static { colors = new String[10]; colors[0] = "Black"; colors[1] = "White"; colors[2] = "Green"; colors[3] = "Red"; colors[4] = "Blue"; colors[5] = "Orange"; colors[6] = "Silver"; colors[7] = "Yellow"; colors[8] = "Brown"; colors[9] = "Maroon"; brands = new String[10]; brands[0] = "BMW"; brands[1] = "Mercedes"; brands[2] = "Volvo"; brands[3] = "Audi"; brands[4] = "Renault"; brands[5] = "Fiat"; brands[6] = "Volkswagen"; brands[7] = "Honda"; brands[8] = "Jaguar"; brands[9] = "Ford"; } public List<Car> createCars(int size) { List<Car> list = new ArrayList<Car>(); for(int i = 0 ; i < size ; i++) { list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState())); } return list; } private String getRandomId() { return UUID.randomUUID().toString().substring(0, 8); } private int getRandomYear() { return (int) (Math.random() * 50 + 1960); } private String getRandomColor() { return colors[(int) (Math.random() * 10)]; } private String getRandomBrand() { return brands[(int) (Math.random() * 10)]; } private int getRandomPrice() { return (int) (Math.random() * 100000); } private boolean getRandomSoldState() { return (Math.random() > 0.5) ? true: false; } public List<String> getColors() { return Arrays.asList(colors); } public List<String> getBrands() { return Arrays.asList(brands); } }