Thanks Mike and everyone who responded. I did manage to find a working solution by creating a OptGroup component by extending WebMarkupContainer and using fragments... But was then was disheartened as it appears that you cannot nest optgroups inside one another in html - To represent the multiple levels within my list hierarchy. If there is such a way to nest optgroups within one another, then I would very much appreciate the info but I suspect you can't from what I've read. My final solution was just to use a select component and replace the labels using hyphens before the text to represent the current level: Item1---Item1-1---Item1-2---Item1-3Item2---Item2-1---Item2-2------Item2-2-1------Item2-2-2---Item2-3Item3 Thanks Gary --- On Tue, 9/1/09, Michael O'Cleirigh <[email protected]> wrote:
From: Michael O'Cleirigh <[email protected]> Subject: Re: Dynamic Optgroups in Wicket To: [email protected] Date: Tuesday, September 1, 2009, 12:03 PM Hi Gary, > Hi,I am kind of new to Wicket and am having a difficult time finding some > decent info on creating optgroups within selects.I basically have a > structured list in my DB which in turn has children - perfect solution would > be to use dynamically generated optgroups with their children which will of > course be option tags.All the examples containing optgroups seem to have the > optgroups fixed in markup with no wicket:ids.Any thoughts would be most > welcomed.Gary > I created a custom implementation of SelectOptions and SelectOption (in wicket-extensions) that I use to render the optgroups. What I do is use a wrapping object that contains the business object and the name of the optgroup and if it is the first or last wrapping object with that particular optgroup name. Then I override the SelectOptions.newOption(...) to wire in my custom CustomSelectOption. Note that newOption() is called in order for each option in the list so tracking the last optiongroup name can be used to determine if its changed or not. i.e. if the Custom.SlectOption.startOptionGroup or the CustomSelectOption.endOptionGroup parameter should be true or false. This is the only change in the selectOption: �...@override protected void onRender(MarkupStream markupStream) { /* * Write out the option group wrappings if this is the first or last option in a particular group. */ if (startOptionGroup) { getResponse().write("<optgroup label=\"" + optionGroupName + "\">" + optionGroupName + "\n"); } super.onRender(markupStream); if (endOptionGroup) { getResponse().write("</optgroup>\n"); } } The setting of the start and end flags are handled in SelectOptions.newOption(...) and then the above code is used to render the options into optiongroups. Let me know if you need more details, Regards, Mike --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
