Is there something in Wicket to do two drop down choices so that the first selection of the first doprdownchoice changes all the options of the second one?
Asked
Active
Viewed 6,155 times
2 Answers
5
You should use the value of the first choice to determine the value of the second one.
I this example I chose to use a AjaxFormComponentUpdatingBehavior
that triggers the Ajax update and performs the value change. I'm providing the simple example of populating the second DropDownChoice
with the Federal States of the country that gets selected in the first one.
Note that I'm using wicket 1.4 in this example, shouldn't be too much different in newer versions though.
// 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);

msp
- 3,272
- 7
- 37
- 49
0
The first DropDownChoice
should update the Model
second DropDownChoice
is based on and add second DDC to AjaxRequestTarget
.
This constructor accepts the model and you can store a reference to it in order to be able to change it.

Alexey Malev
- 6,408
- 4
- 34
- 52
-
This one? DropDownChoice(String id,IModel extends List extends T>> choices) I dont get how it would work, cus for example, the first ddc have 1 , 2 and 3 like options. When i choose 1, in the second ddc i wanna see (for example) 4 , 5,6 options. When i select 2, i want to see 7, 8 ,9. Where will those options be initialized? Thank you :) – evilkorona Apr 29 '14 at 00:38
-
Well, you just wrote it, although in nature language... in method that handles value change, you should check - it current selected value is 1, set model of other DDC to contain 4 5 6, if 2 - to contain 7 8 9. – Alexey Malev Apr 29 '14 at 00:41
-
@evilkorona You're welcome. Do not forget to upvote helpful posts and select preferred, if your problem is resolved. – Alexey Malev Apr 29 '14 at 00:45
-
I could,t. The second DDC don't redraw :( – evilkorona Apr 29 '14 at 03:25
-
I dont know how to do it > – evilkorona Apr 29 '14 at 04:14
-
I think i found the answer, but i am really tired to read it now http://topriddy.wordpress.com/2012/01/29/intelligent-dropdown-choice-implementation-in-wicket/ – evilkorona Apr 29 '14 at 04:22