Reputation: 1513
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.
Upvotes: 0
Views: 199
Reputation: 1513
After some more digging, I found two different references to a bug that was supposedly resolved in a previous version. I'm not saying that the bug is still there, maybe I still missed something, but the workaround for that bug worked in my case too.
Seems that there was some issue with the class used as model: if it didn't implement equals() and hashcode(), its instances would not be correctly compared to each other by the ChoiceRenderer in background. So, I added both hashcode() and equals() to the Sede and Sala classes, simply out of desperation, and it worked fine.
Links to the references:
Wicket - DropDownChoice + ChoiceRenderer - preselecting does not work
Wicket DropDownChoice NOT working correctly with PropertyModels
EDIT: martin_g suggestion worked fine as well, so I marked it as correct answer. I'll just leave my previous solution as suggestion.
Upvotes: 0
Reputation: 17503
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
.
Upvotes: 2