hierarchy - Make multiple dependent / cascading selectOneMenu dropdown lists in JSF -
i trying make 4 dependent menus.
when user chooses item first menu, second menu show dependent data , when user chooses item second 1 , third 1 show dependent data , on.
the user see items on first menu , other ones blank. if chooses item on first menu second 1 show data third , fourth remain blank, , on. user must choose entries 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 working @ first 2 menus other 2 menus null values
put bean in view scope , rid of business logic in getter methods.
the bean must placed in view scope previous selections , new available items remembered, otherwise things fail if e.g. rendered
attribute depends on condition set in previous request, or if jsf needs validate selected item against list of available items.
the getter methods should not contain business logic invoked during a.o. validations phase. should use <f:ajax listener>
perform business logic based on change. should in listener method explicitly clear out selected values of child dropdowns. can use <f:ajax render>
update contents of child dropdowns.
thus, so:
<h:selectonemenu id="state" value="#{nodes.selectedstate}"> <f:selectitem itemvalue="#{null}" itemlabel="-- select --" /> <f:selectitems value="#{nodes.availablestates}" /> <f:ajax listener="#{nodes.changestate}" render="city region station" /> </h:selectonemenu> <h:selectonemenu id="city" value="#{nodes.selectedcity}"> <f:selectitem itemvalue="#{null}" itemlabel="-- select --" /> <f:selectitems value="#{nodes.availablecities}" /> <f:ajax listener="#{nodes.changecity}" render="region station" /> </h:selectonemenu> <h:selectonemenu id="region" value="#{nodes.selectedregion}"> <f:selectitem itemvalue="#{null}" itemlabel="-- select --" /> <f:selectitems value="#{nodes.availableregions}" /> <f:ajax listener="#{nodes.changeregion}" render="station" /> </h:selectonemenu> <h:selectonemenu id="station" value="#{nodes.selectedstation}"> <f:selectitem itemvalue="#{null}" itemlabel="-- select --" /> <f:selectitems value="#{nodes.availablestations}" /> </h:selectonemenu>
with
@managedbean @viewscoped public class nodes { private string selectedstate; // getter+setter private string selectedcity; // getter+setter private string selectedregion; // getter+setter private string selectedstation; // getter+setter private list<selectitem> availablestates; // getter (no setter necessary!) private list<selectitem> availablecities; // getter (no setter necessary!) private list<selectitem> availableregions; // getter (no setter necessary!) private list<selectitem> availablestations; // getter (no setter necessary!) @ejb private someservice someservice; @postconstruct public void init() { availablestates = someservice.liststates(); } public void changestate(ajaxbehaviorevent event) { availablecities = someservice.listcities(selectedstate); selectedcity = selectedregion = selectedstation = null; availableregions = availablestations = null; } public void changecity(ajaxbehaviorevent event) { availableregions = someservice.listregions(selectedcity); selectedregion = selectedstation = null; availablestations = null; } public void changeregion(ajaxbehaviorevent event) { availablestations = someservice.liststations(selectedregion); selectedstation = null; } // generate necessary getters+setters here. should not change them. }
Comments
Post a Comment