Re: Dynamic Select and SelectOptions
Excellent, works perfect. But the only change is instead of getModelObjectAsString() use getDefaultModelObjectAsString(). Very good idea, but the only problem is if I have 7 option groups with each option group with 5-6 options HTML will be really long. Thanks, Raghu tbt wrote: > > > homar70 wrote: >> >> Hi, >> >> I have trouble of finding out how to make dynamic optgroups and options >> from the select and selectoptions in wicket-extensions. The same issue is >> described in this post >> http://www.nabble.com/Select-and-SelectOptions-td11684707.html#a11695243 >> >> Any ideas? >> >> -Spratle >> > > Hi > > You could use the following classes in wicket-extensions to create an > optgroup class. I have created a static dropdown with optgroups and the > example is given below. A repeater like ListView or RepeatingView can be > used to make it dynamic > > > public class OptGroup extends SelectOption > { > String label; > > public OptGroup(String id, String label) > { > super(id); > this.label = label; > } > > protected void onComponentTag(final ComponentTag tag) > { > checkComponentTag(tag, "optgroup"); > Select select = (Select)findParent(Select.class); > if (select == null) > { > throw new WicketRuntimeException( > "OptGroup component [" + > getPath() + > "] cannot find its > parent Select. All OptGroup components must be a > child of or below in the hierarchy of a Select component."); > } > > > tag.put("label", label); > } > } > > > public class CustomSelectOption extends SelectOption > { > public CustomSelectOption(String id,String displayValue) > { > super(id,new Model(displayValue)); > } > > protected void onComponentTagBody(final MarkupStream markupStream, final > ComponentTag openTag) > { > replaceComponentTagBody(markupStream, openTag, > getModelObjectAsString()); > } > } > > > ... > > OptGroup swedishOptGroup = new OptGroup("optGroup1","Swedish Cars"); > SelectOption option1 = new > CustomSelectOption("option1","Volvo"); > swedishOptGroup.add(option1); > SelectOption option2 = new CustomSelectOption("option2","Saab"); > swedishOptGroup.add(option2); > select.add(swedishOptGroup); > > OptGroup germanOptGroup = new OptGroup("optGroup2","German > Cars"); > SelectOption option3 = new > CustomSelectOption("option3","Mercedes"); > germanOptGroup.add(option3); > SelectOption option4 = new CustomSelectOption("option4","Audi"); > germanOptGroup.add(option4); > select.add(germanOptGroup); > > .. > > > > > > > > > > > > > -- View this message in context: http://www.nabble.com/Dynamic-Select-and-SelectOptions-tp21076798p24929879.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Dynamic Select and SelectOptions
homar70 wrote: > > Hi, > > I have trouble of finding out how to make dynamic optgroups and options > from the select and selectoptions in wicket-extensions. The same issue is > described in this post > http://www.nabble.com/Select-and-SelectOptions-td11684707.html#a11695243 > > Any ideas? > > -Spratle > Hi You could use the following classes in wicket-extensions to create an optgroup class. I have created a static dropdown with optgroups and the example is given below. A repeater like ListView or RepeatingView can be used to make it dynamic public class OptGroup extends SelectOption { String label; public OptGroup(String id, String label) { super(id); this.label = label; } protected void onComponentTag(final ComponentTag tag) { checkComponentTag(tag, "optgroup"); Select select = (Select)findParent(Select.class); if (select == null) { throw new WicketRuntimeException( "OptGroup component [" + getPath() + "] cannot find its parent Select. All OptGroup components must be a child of or below in the hierarchy of a Select component."); } tag.put("label", label); } } public class CustomSelectOption extends SelectOption { public CustomSelectOption(String id,String displayValue) { super(id,new Model(displayValue)); } protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { replaceComponentTagBody(markupStream, openTag, getModelObjectAsString()); } } ... OptGroup swedishOptGroup = new OptGroup("optGroup1","Swedish Cars"); SelectOption option1 = new CustomSelectOption("option1","Volvo"); swedishOptGroup.add(option1); SelectOption option2 = new CustomSelectOption("option2","Saab"); swedishOptGroup.add(option2); select.add(swedishOptGroup); OptGroup germanOptGroup = new OptGroup("optGroup2","German Cars"); SelectOption option3 = new CustomSelectOption("option3","Mercedes"); germanOptGroup.add(option3); SelectOption option4 = new CustomSelectOption("option4","Audi"); germanOptGroup.add(option4); select.add(germanOptGroup); .. -- View this message in context: http://www.nabble.com/Dynamic-Select-and-SelectOptions-tp21076798p21155450.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Dynamic Select and SelectOptions
Hi, I just had the problem alike, where if my list of selectoptions was empty, optgroup header was shown without any elements below, needed to hide this optgroup tag: Instead of developing OptGroup class, I used WebMarkupContainer and SelectOptions components, together they worked fine for me, here is the java code: Select mySelect = new Select("mySelect"); WebMarkupContainer optgroup1WMC = new WebMarkupContainer("optgroup1"); SelectOptions group1 = new SelectOptions("group1", someListOfElements1, someRenderer1()); optgroup1WMC.add(group1); optgroup1WMC.setVisible(someBoolVariable1); mySelect.add(optgroup1WMC); mySelect.add(group1); WebMarkupContainer optgroup2WMC = new WebMarkupContainer("optgroup2"); SelectOptions group2 = new SelectOptions("group2", someListOfElements2, someRenderer2()); optgroup2WMC.add(group2); optgroup2WMC.setVisible(someBoolVariable2); mySelect.add(optgroup2WMC); mySelect.add(group2); add(mySelect); and HTML code: Hope it helps someone in future :) -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamic-Select-and-SelectOptions-tp1872483p4508111.html Sent from the Users forum mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Dynamic Select and SelectOptions
Hi, good solution, but how does it work using a ListView ?? I tried: Select select = new Select("selectId"); select.add(new ListView("selGrpId", list) { OptGroup optGrp = null; @Override protected void populateItem(ListItem item) { MyBean bean = (DachGroupSelectBoxBean) item.getModelObject(); if (grpSelBean.getType() == NONE) item.add(new CustomSelectOption("option", bean.getName())); else if (grpSelBean.getType() == PARENT) item.add(optGrp = new OptGroup("optGroup", bean.getName())); else if (grpSelBean.getType() == CHILD) optGrp.add(new CustomSelectOption("grpOption", bean.getName())); } } ); add(select); Markup: DummyText Error message: WicketMessage: Unable to find component with id 'optGroup' in [MarkupContainer [Component id = 0]]. This means that you declared wicket:id=optGroup in your markup, but that you either did not add the component to your page at all, or that the hierarchy does not match. What I'm doing wrong? Some hints? Thanks! Ralph -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamic-Select-and-SelectOptions-tp1872483p3309097.html Sent from the Users forum mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Dynamic Select and SelectOptions
The solution is to nest the ListViews: Markup: Code: Select select = new Select(selectId); select.add(new ListView("optGroups", transList) { @Override protected void populateItem(ListItem item) { MyBean myBean = (MyBean) item.getModelObject(); item.add(new OptGroup("optGroup", myBean.getName(), MyBean.getList())); } }); add(select); class OptGroup extends SelectOption { String label; List list; public OptGroup(String id, String label, List list) { super(id); this.label = label; this.list = list; init(); } @SuppressWarnings("unchecked") private void init() { this.add(new ListView("options", list) { @Override protected void populateItem(ListItem item) { MyBean2 myBean2 = (MyBean2) item.getModelObject(); item.add(new CustomSelectOption("option", myBean2.getName())); } }); } protected void onComponentTag(final ComponentTag tag) { checkComponentTag(tag, "optgroup"); Select select = (Select) findParent(Select.class); if (select == null) { throw new WicketRuntimeException( "OptGroup component [" + getPath() + "] cannot find its parent Select. All OptGroup components must be a child of or below in the hierarchy of a Select component."); } tag.put("label", label); } } public class CustomSelectOption extends SelectOption { @SuppressWarnings("unchecked") public CustomSelectOption(String id, String displayValue) { super(id, new Model(displayValue)); } protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { replaceComponentTagBody(markupStream, openTag, getDefaultModelObjectAsString()); } } -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamic-Select-and-SelectOptions-tp1872483p3310381.html Sent from the Users forum mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Dynamic Select and SelectOptions
It's working fine with IE, safari, Chrome, Opera but not with Firefox (FF) The optgroup closing tag is set wrong. The options are not enclosed in the optgroup tags. Firebug shows following rendering Audi Opel Thank's for hints! ralph -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamic-Select-and-SelectOptions-tp1872483p3310785.html Sent from the Users forum mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org