Is there some obvious way to add a separator to the list of options in a Wicket DropDownChoice? In my case I'm populating the selection with two types of domain objects from my datasource. I guess I could go and manually add some kind of dummy domain object to the choice list but it feels pretty ugly.
Example:
+---------+-+
| Apple |▼|
| Orange +-+
| ------- |
| Carrot |
| Cucumber|
+---------+
Current code (without any separator) looks something like:
EntityModel model = getModel();
List<? extends Produce> foods = foodService.getAllProduce();
// getAllProduce() returns first all fruits, then all vegetables
add(new DropDownChoice<Produce>(
"produceSelect", new PropertyModel<Produce>(model, "favProduce"), foods)
);
I ended up solving this using the Select
and SelectOptions
components from wicket-extensions as mentioned by martin-g.
SelectOptions<Produce> fruitOptions = new SelectOptions<Produce>(
"fruits",
fruitCollection,
new FruitRenderer());
SelectOptions<Produce> vegetableOptions = new SelectOptions<Produce>(
"vegetables",
vegetableCollection,
new VegetableRenderer());
Select select = new Select("produceSelect",
new PropertyModel<Produce>(model, "favProduce"));
select.add(fruitOptions);
select.add(vegetableOptions);
The corresponding HTML looks something like this:
<select wicket:id="produceSelect" id="produceSelect">
<optgroup label="Fruits">
<wicket:container wicket:id="fruits">
<option wicket:id="option">Apple</option>
</wicket:container>
</optgroup>
<optgroup label="Vegetables">
<wicket:container wicket:id="vegetables">
<option wicket:id="option">Carrot</option>
</wicket:container>
</optgroup>
</select>
This produces a bit different but better end result as the optgroup
labels are bolded and cannot be selected:
+----------------+-+
| **Fruits** |▼|
| Apple +-+
| Orange |
| **Vegetables** |
| Carrot |
| Cucumber |
+----------------+
add(new DropDownChoice<String>("choice", Arrays.asList("Apple","Orange","Carrot","Cucumber")) {
@Override
protected void appendOptionHtml(AppendingStringBuffer buffer, String choice, int index, String selected) {
super.appendOptionHtml(buffer, choice, index, selected);
if ("Orange".equals(choice)) {
buffer.append("<optgroup label='----------'></optgroup>");
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With