I had basically the same problem. After several days of searching for a short solution, I find it works best for maximum flexibility, uses repeaters, containers, and AttributeModifier , something like:
<select wicket:id="select"> <wicket:container wicket:id="repeatingView"> <optgroup wicket:id="optGroup"> <wicket:container wicket:id="selectOptions"> <option wicket:id="option"></option> </wicket:container> </optgroup> </wicket:container> </select>
In Java code, "select" is Select; "repeatingView" is a RepeatingView. Nested inside a RepeatingView contains a WebMarkupContainer called .newChildId (). Nested inside is another WebMarkupContainer that represents the "optGroup". Inside this second WMC is an AttributeModifier, which adds a dynamic label to optgroup and SelectOptions, which handles "selectOptions" and "option". Sort of:
Select select = new Select("select"); add(select); RepeatingView rv = new RepeatingView("repeatingView"); select.add(rv); for(String groupName : groupNames){ WebMarkupContainer overOptGroup = new WebMarkupContainer(rv.newChildId()); rv.add(overGroup); WebMarkupContainer optGroup = new WebMarkupContainer("optGroup"); overOptGroup.add(optGroup); optGroup.add( new AttributeModifier("label",true,new Model<String>(groupName)) ); optGroup.add( new SelectOptions<MyBean>( "selectOptions",listOfBeanOptionsForThisGroup,new MyBeanRenderer() ) ); }
(it is assumed that the strings are passed directly as group names and that the parameters refer to beans of type MyBean listed in the variable listOfBeanOptionsForThisGroup)
I believe that it should not be difficult to reorganize this solution into something that uses much less nesting, if anyone has any suggestions, I will edit them in response and deal with them. Using ListView instead of RepeatingView should also reduce code size.
source share