Dynamic support for <optgroup> in gates

I'm looking to make the <select> on my page with a wicket, but group the parameters with <optgroup> , this was discussed in Separator in Wicket DropDownChoice , but the solutions there <optgroup> assume that the <optgroup> tags are static, I want to pull both parameters and groups from the database.

+4
source share
3 answers

Use two nested repeaters to repeat according to your groups and parameters:

 <select wicket:id="select"> <optgroup wicket:id="group"> <option wicket:id="option"></option> </optgroup> </select> 
+4
source

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.

+3
source

So, for now, my solution should have something like this:

  interface Thing { String getCategory(); } 

and then:

  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; } }; 

This requires that thingList already sorted by category.

+2
source

Source: https://habr.com/ru/post/1386528/


All Articles