You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by homar70 <at...@gmail.com> on 2008/12/18 18:03:43 UTC

Dynamic Select and SelectOptions

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
-- 
View this message in context: http://www.nabble.com/Dynamic-Select-and-SelectOptions-tp21076798p21076798.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Dynamic Select and SelectOptions

Posted by Raghu Kasturi <au...@gmail.com>.
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);
> 
> ..................................................
> 
> 
> <select wicket:id="select">
> <optgroup wicket:id="optGroup1">
> <option wicket:id="option1"></option>
> <option wicket:id="option2"></option>
> </optgroup>
> <optgroup wicket:id="optGroup2">
> <option wicket:id="option3"></option>
> <option wicket:id="option4"></option>
> </optgroup>
> </select>
> 

-- 
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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Dynamic Select and SelectOptions

Posted by tbt <na...@yahoo.com>.

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

..................................................


<select wicket:id="select">
<optgroup wicket:id="optGroup1">
<option wicket:id="option1"></option>
<option wicket:id="option2"></option>
</optgroup>
<optgroup wicket:id="optGroup2">
<option wicket:id="option3"></option>
<option wicket:id="option4"></option>
</optgroup>
</select>
-- 
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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org