wicketdropdownchoicewicket-8

Wicket 1.8 DropDownChoice doesn't show the correct model object


I'm having an issue with DropDownChoice and its model. The HTML involved is a modal window with which the user can edit the settings of an object: the first DDC alters the list of the second when its model changes, and everything works fine. The problem is that both the DDCs do not show the saved value, but they show the first item in the list associated. Here's some code:

private DropDownChoice<Sala> salaDDC;
private DropDownChoice<Sede> sedeDDC;
private ArrayList<Sala> listaSale;

private Sala sala = null; //they both get correctly initialized afterwards
private Sede sede = null;
[...]
//first DDC, with the "sede" list. 
//Its model is based on class Sede, and its list of choices is sediList, which is constant

    form.addOrReplace(sedeDDC = new DropDownChoice<>("sedeDDC", Model.of(sede), sediList);
    sedeDDC.add(new AjaxFormComponentUpdatingBehavior("change") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            listaSale = listSalePerSede.get(sedeDDC.getModelObject().getId());
            if (null != listaSale) {
                if (listaSale.isEmpty()) {
                    listaSale = new ArrayList<>();
                }
            } else {
                listaSale = new ArrayList<>();
            }
            target.add(salaDDC);
        }
    });
    sedeDDC.setDefaultModelObject(sede);
    sedeDDC.setModelObject(sede);

//second DDC, representing the "sala" list 
//Its model is based on Sala class, and its list of choices changes if the 
//other DDC model object changes, hence the PropertyModel model

    form.addOrReplace(salaDDC = new DropDownChoice<>("salaDDC", Model.of(sala), new PropertyModel(this, "listaSale")));
    salaDDC.setDefaultModelObject(sala);
    salaDDC.setModelObject(sala);
    salaDDC.setOutputMarkupId(true);

So, suppose you have this situation:

"Sede" has a meaning like department, and "Sala" is like a room, so for each department, you can have a list of different rooms.

If the user tries to edit an object which settings are "sede1" and "sala2", the dialog window will load with the first DDC showing "sede1" (that is right, only by chance) and the second one showing "sala1" (because is the first of the list associated to "sede1"), and not "sala2". Similarly, if the object's settings are "sede2" and "sala5", the DDCs will show respectively "sede1" (first of the "sede" list) and "sala4" (first item of the list associated with "sede2"), while the model object is "sala5". So, for the time being, the user has to re-set the already saved values for these two fields when editing the related object, and that is not nice.

I've done some debugging, and from what I collected, both the model and the default model are always correct: the DDCs just don't show them, and I can't understand why. Let me know if more information is needed.


Solution

  • You use DropDownChoice without providing IChoiceRenderer, so Wicket uses new ChoiceRenderer(), i.e. without displayExpression and idExpression parameters.

    Try with new DropDownChoice(id, model, list, new ChoiceRenderer("name", "id")), where "name" would be the displayExpression and "id" the idExpression for Sede and Sala.