Let's say I want to create a country/state picker and use it to manage the pulldown menus in a form. Ideally, I'd like to bind the values of these menus to the location Object I'm trying to manage.
<h:selectOneMenu id="countryMenu" value=#{controller.location.country}>
<f:ajax listener="{controller.locationPicker.updateStates}"
execute="stateMenu cityInput"/>
<h:selectItems value=#{controller.locationPicker.countries}/>
</h:selectOneMenu>
<h:selectOneMenu id="stateMenu" value=#{controller.location.state}>
<f:ajax listener="{controller.locationPicker.updateCities}" execute="cityInput"/>
<h:selectItems value=#{controller.locationPicker.states}/>
</h:selectOneMenu>
<h:inputText id="cityInput" value=#{controller.location.city}
rendered=#{not empty controller.location.state"/>
public class LocationPicker {
.
.
public void updateStates(AjaxBehaviorEvent event) {
List<String> selectedCountries = getSelectionsFromEvent(event);
states = getStatesFromCountries(selectedCountries);
cities = getCitiesFromStates(states);
}
}
The problem is that the only way to get the location.country
or location.state
values within the locationPicker
, is to use the AjaxBehaviorEvent
to get the value. This doesn't seem like the right approach, though, considering I have to cache all of these values within the locationPicker
because there are other form elements which depend on these values and each AjaxBehaviorEvent
only has the value for the component it's fire on.
It would be SO MUCH EASIER to just bind the menu values/selectItems to one object (the locationPicker
). Then, when submitting the form, copy the locationPicker values to the location object. I'm trying to avoid that though!
Anyone have any recommendations on how to make this work?