wicket中的动态<optgroup>支持

Mat*_*ett 5 java forms wicket optgroup

我正在寻找<select>使用wicket在我的页面中呈现标签,但是将选项分组<optgroup>,这在Wicket DropDownChoice中的Separator上进行了讨论,但在解决方案中<optgroup>假设<optgroup>标签是静态的,我想要同时使用数据库中的选项和组.

Mat*_*ett 2

好吧,目前我的解决方案是这样的:

   interface Thing {
       String getCategory();
   }
Run Code Online (Sandbox Code Playgroud)

进而:

            List<Thing> thingList = service.getThings();
    DropDownChoice<Thing> dropDownChoice = new DropDownChoice<Thing>("select",
            thingList) {
        private static final long serialVersionUID = 1L;
        private Thing last;

        private boolean isLast(int index) {
            return index - 1 == getChoices().size();
        }

        private boolean isFirst(int index) {
            return index == 0;
        }

        private boolean isNewGroup(Thing current) {
            return last == null
                    || !current.getCategory().equals(last.getCategory());
        }

        private String getGroupLabel(Thing current) {
            return current.getCategory();
        }

        @Override
        protected void appendOptionHtml(AppendingStringBuffer buffer,
                Thing choice, int index, String selected) {
            if (isNewGroup(choice)) {
                if (!isFirst(index)) {
                    buffer.append("</optgroup>");
                }
                buffer.append("<optgroup label='");
                buffer.append(Strings.escapeMarkup(getGroupLabel(choice)));
                buffer.append("'>");
            }
            super.appendOptionHtml(buffer, choice, index, selected);
            if (isLast(index)) {
                buffer.append("</optgroup>");
            }
            last = choice;

        }
    };
Run Code Online (Sandbox Code Playgroud)

这要求thingList已经根据类别进行排序。