Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separator in a Wicket DropDownChoice

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)
);
like image 217
Janne Avatar asked Jul 06 '11 07:07

Janne


2 Answers

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       |
+----------------+
like image 122
Janne Avatar answered Oct 11 '22 01:10

Janne


    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>");
            }
        }
    });
like image 34
tetsuo Avatar answered Oct 10 '22 23:10

tetsuo