Using JSF, I'm trying to make a kind of complex situation (Class names has been refactored, and example is minimalist) :
DB
relation :a guy
hasa car
,a car
hasa type
,a type
hascars
The selectOneMenu
for CarType
need to operate 2 roles :
- show the type of the car at loading
- change the cars propositions when it changes
When I load my dataTable
, for each row, I want to show the value of the car if the guy has one (null
is a valid choice), and in the selectOneMenu
for CarType
if want to show the type of the car of the guy if the guy has one, but I can't use guy.car.carType
because I don't want to write over when changes its value, this menu needs also to serve as a research menu : I choose a Type, I can see its cars
I can't figure out which binding to use to set its value at beginning
1. Classes
class Guy{
Car car;
}
class Car{
CarType type;
}
class CarType{
String type;
}
2. View
<p:dataTable value="#{Bean.guys}" var="guy">
<p:column>
<p:selectOneMenu value="#{Bean.selectedType}>
<f:selectItem value="#{null}" />
<f:selectItems value="#{Beans.types}">
<p:ajax event="change" listener="#{bean.changeType}" />
</p:selectOneMenu>
</p:column>
<p:column>
<p:selectOneMenu value="#{Bean.selectedCar}>
<f:selectItem value="#{null}" />
<f:selectItems value="#{Beans.cars(guy)}" />
<p:ajax event="change" listener="#{bean.changeCar(guy)}" />
</p:selectOneMenu>
</p:column>
</p:dataTable>
3. Bean
class Bean{
public List<Guy> guys; // filled from DB
public List<Car> cars; // filled from DB
public List<CarType> types; // filled from DB
private Car selectedCar; // getter setter ok
private CarType selectedType; // getter setter ok
public List<Car> cars(Guy g){
selectedCar = g.getCar();
cars = selectedCar.getCarType().getCars(); // all cars of this type
}
public void changeType(){
cars = selectedType.getCars();
}
public void changeCar(Guy g){
g.setCar(selectedCar);
}
}