Search code examples
wicketdropdownchoice

Wicket 7 - Select, SelectOptions and pre-set


Im using Select instead of DropDownChoice to use OPTGROUP.

Select<Role> roleInput = new Select<Role>("role", new PropertyModel<Role>(this,"selectedRole"));

The two list of Role are:

SelectOptions<Role> fedOptions = new SelectOptions<Role>("federazione",federationRoleList,new RoleRenderer());
SelectOptions<Role> eOptions = new SelectOptions<Role>("enti",eRoleList,new RoleRenderer());

Its working well when submitting and also applying a AjaxFormComponentUpdatingBehavior on roleInput, I have my PropertyModel dynamically modified.

Unfortunally I have a problem with pre-set. I tried to set selectedRole with a specific Role but the Select always start with the first element of the first list.

DropDownChoice works perfectly pre-setting the model but not Select.

I've tried with

roleInput.setModelObject(selectedRole);

but its not working.

I thinks the problem is with this component that has to manage two or more Repeaters instead of a single list.

Any clue?

Thanks

EDIT: Implementation of RoleRenderer

public class RoleRenderer implements IChoiceRenderer<Role>,Serializable{
private static final long serialVersionUID = 1L;

@Override
public Object getDisplayValue(Role object) {
    return object.getName();
}

@Override
public String getIdValue(Role object, int index) {
    return object.getId().toString();
}

@Override
public Role getObject(String id, IModel<? extends List<? extends Role>> choices) {
    return getObjectFromId(id);
}

public Role getObjectFromId(String id){
    return null;
};

}

NOTE: getObjectFromId require access to Manager so will be overrided outside.


Solution

  • Put a breakpoint at org.apache.wicket.extensions.markup.html.form.select.SelectOption#onComponentTag() and see what is returned by select.isSelected(this) for the SelectionOption that matches the default model (object). It might be that your #equals() implementation is not correct.