Separator in a Wicket DropDownChoice

匆匆过客 提交于 2019-11-29 10:51:16

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>");
            }
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!