evi*_*ona 3 java wicket dropdownchoice
在Wicket中有什么东西可以做两个下拉选项,这样第一个doprdownchoice的第一个选择会改变第二个选项的所有选项吗?
您应该使用第一个选项的值来确定第二个选择的值.
在本例中,我选择使用AjaxFormComponentUpdatingBehavior触发Ajax更新并执行值更改的示例.我提供了一个简单的例子,DropDownChoice在第一个选择的国家的联邦州填充第二个.
请注意,我在这个例子中使用的是wicket 1.4,但在新版本中不应该有太大的不同.
// two countries
final String aut = "AUT";
final String ger = "GER";
// their states
final List<String> autStates = Arrays.asList(new String[] { "V", "T", "S", "W" });
final List<String> gerStates = Arrays.asList(new String[] { "NRW", "B", "BW" });
// mapping, you should really get this data from a service or have a constant
final Map<String, List<String>> countryToState = new HashMap<String, List<String>>(2);
countryToState.put(aut, autStates);
countryToState.put(ger, gerStates);
// the container to send back via ajax
final WebMarkupContainer cont = new WebMarkupContainer("cont");
cont.setOutputMarkupId(true);
add(cont);
final Model<String> stateModel = new Model<String>();
final DropDownChoice<String> countries = new DropDownChoice<String>("countries", new Model<String>(), new ArrayList<String>(countryToState.keySet()));
final DropDownChoice<String> states = new DropDownChoice<String>("states", stateModel, new LoadableDetachableModel<List<String>>() {
@Override
protected List<String> load() {
final String country = countries.getModelObject();
final List<String> list = countryToState.get(country);
return list != null ? list : new ArrayList<String>(0);
}
});
countries.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
// just add the container to see the results
target.addComponent(cont);
}
});
cont.add(countries);
cont.add(states);
Run Code Online (Sandbox Code Playgroud)