I am trying to make 4 dependent menus.
When the user chooses an item from the first menu, the second menu will show dependent data and when the user chooses item from the second one , the third one will show dependent data and so on.
The user will see items on the first menu only and the other ones will be blank. If he chooses an item on the first menu the second one will show data but the third and the fourth will remain blank, and so on. The user must eventually choose entries from all the 4 menus.
<h:selectOneMenu id="first" value="#{nodes.selectState"}>
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity"}>
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion"}>
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation"}>
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
nodes Java Class
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A")){
CityList.add(new SelectItem("B"));
}
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B")){
RegionList.add(new SelectItem("C"));
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C")){
StationList.add(new SelectItem("D"));
}
}
it's only working at the first 2 menus the other 2 menus get null values
Try this, it may help you
By using the --Select City-- , --Select Region--, --Select Station-- to avoid the null pointer Exception.
Put the bean in the view scope and get rid of any business logic in getter methods.
The bean must be placed in the view scope so that all previous selections and new available items are remembered, otherwise things will fail if e.g.
rendered
attribute depends on a condition which was only set in a previous request, or if JSF needs to validate the selected item against list of available items.The getter methods should not contain any business logic as they will also be invoked during a.o. validations phase. You should use
<f:ajax listener>
to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child dropdowns. You can use<f:ajax render>
to update the contents of child dropdowns.Thus, so:
with
See also:
There is a typo error in your code. For third menu you have given id name as "first" instead of "third". May be its because of that problem.
You are facing this issue because you have twice
id="first"
. Fix this and it should work.