I need to display different colors of options in Primefaces.
I have a selectOneMenu with dynamical items (List)
The solution is to use the 'advanced' way of displaying this in PrimeFaces 4.0 and newer.
You can combine f:selectItems tag with p:column tags for p:selectOneMenu (see the showcase), with an iteration var for the columns themselves, like you do in tables.
Then the ideal thing would be to set the styleClass to the entire column depending on the condition, but unfortunatelly it doesn't work. Luckily, adding some Javascript/jQuery code you can achieve your goal, check out this SSCCE:
XHTML Page
Bean.java
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
public class Car implements Serializable {
String name;
boolean sold;
public Car(String name, boolean sold) {
this.name = name;
this.sold = sold;
}
public String getName() {
return name;
}
public boolean isSold() {
return sold;
}
}
private List allCars = Arrays.asList(new Car("Audi", true), new Car(
"Mercedes", false));
public List getAllCars() {
return allCars;
}
private Car selectedCar;
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
public void send() {
System.out.println("Sent " + selectedCar.name);
}
}
You might also be interested in setting only the font color and not the background: