Re: How to pre-select a DropDownChoice
http://cwiki.apache.org/WICKET/dropdownchoice.html Hoover, William wrote: Can you add this to the wiki (http://cwiki.apache.org/WICKET/dropdownchoice-examples.html)? It seems like a commonly asked question :o) - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to pre-select a DropDownChoice
DropDownChoice has a lot of contructors. One of them, DropDownChoice(String id, IModel model, IModel choices, ...) The first model, could be named selectedModel. And represents the selected object, with a wicket model.
Re: How to pre-select a DropDownChoice
Funny, I answered the same question to my colleague this morning. Below the code I gave him. What happens is that the model is wrapped in another model. The wrapping model has a defaultModel that is used to return a value when the underlying model returns null. Below the model wrapper you find an example on how to use it. If you find something better, please let us know :) import org.apache.wicket.model.IChainingModel; import org.apache.wicket.model.AbstractWrapModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.IWrapModel; /** * * @author Erik van Oosten */ public class DefaultingModelWrapper implements IWrapModel { private IModel wrappedModel; private IModel defaultModel; public DefaultingModelWrapper(IModel wrappedModel, IModel defaultModel) { this.wrappedModel = wrappedModel; this.defaultModel = defaultModel; } public IModel getWrappedModel() { return wrappedModel; } public Object getObject() { Object value = wrappedModel.getObject(); if (value == null) { value = defaultModel.getObject(); } return value; } public void setObject(Object object) { wrappedModel.setObject(object); } public void detach() { wrappedModel.detach(); defaultModel.detach(); } } public class DefaultingCountryDropDown extends CountryDropDown { public DefaultingCountryDropDown(String id, IModel model) { super(id, new DefaultingModelWrapper(model, new Model(NL))); } public DefaultingCountryDropDown(String id) { super(id, new DefaultingModelWrapper(getModel(), new Model(NL))); } } Regards, Erik. Juan Gabriel Arias wrote: DropDownChoice has a lot of contructors. One of them, DropDownChoice(String id, IModel model, IModel choices, ...) The first model, could be named selectedModel. And represents the selected object, with a wicket model. -- Erik van Oosten http://day-to-day-stuff.blogspot.com/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to pre-select a DropDownChoice
Can you add this to the wiki (http://cwiki.apache.org/WICKET/dropdownchoice-examples.html)? It seems like a commonly asked question :o) -Original Message- From: Erik van Oosten [mailto:[EMAIL PROTECTED] Sent: Friday, March 14, 2008 9:35 AM To: users@wicket.apache.org Subject: Re: How to pre-select a DropDownChoice Funny, I answered the same question to my colleague this morning. Below the code I gave him. What happens is that the model is wrapped in another model. The wrapping model has a defaultModel that is used to return a value when the underlying model returns null. Below the model wrapper you find an example on how to use it. If you find something better, please let us know :) import org.apache.wicket.model.IChainingModel; import org.apache.wicket.model.AbstractWrapModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.IWrapModel; /** * * @author Erik van Oosten */ public class DefaultingModelWrapper implements IWrapModel { private IModel wrappedModel; private IModel defaultModel; public DefaultingModelWrapper(IModel wrappedModel, IModel defaultModel) { this.wrappedModel = wrappedModel; this.defaultModel = defaultModel; } public IModel getWrappedModel() { return wrappedModel; } public Object getObject() { Object value = wrappedModel.getObject(); if (value == null) { value = defaultModel.getObject(); } return value; } public void setObject(Object object) { wrappedModel.setObject(object); } public void detach() { wrappedModel.detach(); defaultModel.detach(); } } public class DefaultingCountryDropDown extends CountryDropDown { public DefaultingCountryDropDown(String id, IModel model) { super(id, new DefaultingModelWrapper(model, new Model(NL))); } public DefaultingCountryDropDown(String id) { super(id, new DefaultingModelWrapper(getModel(), new Model(NL))); } } Regards, Erik. Juan Gabriel Arias wrote: DropDownChoice has a lot of contructors. One of them, DropDownChoice(String id, IModel model, IModel choices, ...) The first model, could be named selectedModel. And represents the selected object, with a wicket model. -- Erik van Oosten http://day-to-day-stuff.blogspot.com/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to pre-select a DropDownChoice
Yeah, I figured that out last night. That's actually a great way of handling the component's state. Thanks for your input. -Original Message- From: Nick Heudecker [mailto:[EMAIL PROTECTED] Sent: Thursday, March 13, 2008 5:57 PM To: users@wicket.apache.org Subject: Re: How to pre-select a DropDownChoice Set the model of the DDC to the value you want to have selected. On Thu, Mar 13, 2008 at 7:53 PM, Michael Mehrle [EMAIL PROTECTED] wrote: I feel silly asking this, but I can't figure it out. I need to set my DropDownChoice component to the id I'm passing into my page via a pageparam. How do I do that? I don't see anything resembling setSelected(). Michael - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Nick Heudecker Professional Wicket Training Consulting http://www.systemmobile.com Eventful - Intelligent Event Management http://www.eventfulhq.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to pre-select a DropDownChoice
Set the model of the DDC to the value you want to have selected. On Thu, Mar 13, 2008 at 7:53 PM, Michael Mehrle [EMAIL PROTECTED] wrote: I feel silly asking this, but I can't figure it out. I need to set my DropDownChoice component to the id I'm passing into my page via a pageparam. How do I do that? I don't see anything resembling setSelected(). Michael - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Nick Heudecker Professional Wicket Training Consulting http://www.systemmobile.com Eventful - Intelligent Event Management http://www.eventfulhq.com