Re: How to make a Wiket DropDownChoice with complex a model return a simple String
Your welcome! I will try to publish this snippet somewhere in the guide. > Thanks Andrea, > > I was trying what you sent in your previous message and I was having > trouble figuring out how to get the value set on the target property model. > > Your code here, especially this line: > > targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, > choiceIndex)); > > was really helpful and I was able to get everything working. > > Thanks again. > Have you ever consider making this available to the Wicket community? I see > a lot of questions related to DropDownChoice, but not many answers. > > - Bruce > > > -Original Message- > From: Andrea Del Bene [mailto:an.delb...@gmail.com] > Sent: Wednesday, February 12, 2014 4:31 AM > To: users@wicket.apache.org > Subject: Re: How to make a Wiket DropDownChoice with complex a model return > a simple String > > Here is some code of a possible solution: > > > public class DropDownChoiceForString extends DropDownChoice { > > private IModel targetModel; > > public DropDownChoiceForString(String id, IModel model, > IModel targetModel, List choices, > IChoiceRenderer renderer) { > super(id, model, choices, renderer); > this.targetModel = targetModel; > } > > protected DropDownChoiceForString(String id, IModel model, > IModel targetModel) { > this(id, model, targetModel, Collections. emptyList(), null); > } > > @Override > protected void onInitialize() { > super.onInitialize(); > // load the initial choice. > setModelObject(convertChoiceIdToChoice(targetModel.getObject())); > } > > @Override > protected void onDetach() { > super.onDetach(); > > targetModel.detach(); > } > > @Override > protected void onModelChanged() { > super.onModelChanged(); > > T newSelection = getModelObject(); > > int choiceIndex = getChoices().indexOf(newSelection); > // update the string source with the selected value. > targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, > choiceIndex)); > } > } > > > >> Hi, >> >> I needed something similar in a couple of projects. One solution is to >> create a custom DropDownChoice which takes also the string model to >> update. Then you can override onModelChanged to update the string >> model with the new value that can be extracted with the ChoiceRenderer. >>> To make this easier to understand, let's say that I have a list of >>> state abbreviations for example AL, AK, AZ, AR etcetera (the actual >>> example has two letter abbreviations also, but much longer names). I >>> need a DropDownChoice that shows the abbreviations for selection >>> (e.g., AK) but will show the full name as a tooltip whenever the >>> mouse hovers over one of the choice (e.g., AK shows Alaska). >>> >>> >>> I have developed a solution that is based on the example presented here: >>> http://stackoverflow.com/questions/12234738/wicket-dropdownchoice-tit >>> les-too >>> >>> ltips-for-options >>> >>> that develops a subclass of DropDownChoice and overrides >>> appendOptionHtml to append a "title" to the buffer. My class takes a >>> StateDisplay object as its model, and the model has two fields, an >>> "id" that is displayed in the list, and a "fullName" which is added >>> as the title by the appendOptionHtml method. >>> >>> >>> This works fine for displaying thedropdown with the tooltip, but the >>> problem is that the model is a complex object( StateDisplay) with two >>> fields, but the underlying domain object needs a simple String with >>> the state ID. >>> >>> >>> Prior to implementing the tooltip, the DrowdownChoice was formed using: >>> >>> >>> Item.add(new DropDownChoice("state", new >>> PropertyModel((CompanyDTO) item.getDefaultModelObject(), >>> "state"), stateList))); >>> >>> >>> Note: "item "is there because this is used in the populateItem method >>> of a ListView. >>> >>> >>> Now the code is: >>> >>> >>> Model< StateDisplay > selectedState = new Model<>(); >>> >>> >>> ChoiceRenderer choiceRenderer = new >>> ChoiceRenderer("id", "id"); >>> >>>
RE: How to make a Wiket DropDownChoice with complex a model return a simple String
Thanks Andrea, I was trying what you sent in your previous message and I was having trouble figuring out how to get the value set on the target property model. Your code here, especially this line: targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, choiceIndex)); was really helpful and I was able to get everything working. Thanks again. Have you ever consider making this available to the Wicket community? I see a lot of questions related to DropDownChoice, but not many answers. - Bruce -Original Message- From: Andrea Del Bene [mailto:an.delb...@gmail.com] Sent: Wednesday, February 12, 2014 4:31 AM To: users@wicket.apache.org Subject: Re: How to make a Wiket DropDownChoice with complex a model return a simple String Here is some code of a possible solution: public class DropDownChoiceForString extends DropDownChoice { private IModel targetModel; public DropDownChoiceForString(String id, IModel model, IModel targetModel, List choices, IChoiceRenderer renderer) { super(id, model, choices, renderer); this.targetModel = targetModel; } protected DropDownChoiceForString(String id, IModel model, IModel targetModel) { this(id, model, targetModel, Collections. emptyList(), null); } @Override protected void onInitialize() { super.onInitialize(); // load the initial choice. setModelObject(convertChoiceIdToChoice(targetModel.getObject())); } @Override protected void onDetach() { super.onDetach(); targetModel.detach(); } @Override protected void onModelChanged() { super.onModelChanged(); T newSelection = getModelObject(); int choiceIndex = getChoices().indexOf(newSelection); // update the string source with the selected value. targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, choiceIndex)); } } > Hi, > > I needed something similar in a couple of projects. One solution is to > create a custom DropDownChoice which takes also the string model to > update. Then you can override onModelChanged to update the string > model with the new value that can be extracted with the ChoiceRenderer. >> To make this easier to understand, let's say that I have a list of >> state abbreviations for example AL, AK, AZ, AR etcetera (the actual >> example has two letter abbreviations also, but much longer names). I >> need a DropDownChoice that shows the abbreviations for selection >> (e.g., AK) but will show the full name as a tooltip whenever the >> mouse hovers over one of the choice (e.g., AK shows Alaska). >> >> >> I have developed a solution that is based on the example presented here: >> http://stackoverflow.com/questions/12234738/wicket-dropdownchoice-tit >> les-too >> >> ltips-for-options >> >> that develops a subclass of DropDownChoice and overrides >> appendOptionHtml to append a "title" to the buffer. My class takes a >> StateDisplay object as its model, and the model has two fields, an >> "id" that is displayed in the list, and a "fullName" which is added >> as the title by the appendOptionHtml method. >> >> >> This works fine for displaying thedropdown with the tooltip, but the >> problem is that the model is a complex object( StateDisplay) with two >> fields, but the underlying domain object needs a simple String with >> the state ID. >> >> >> Prior to implementing the tooltip, the DrowdownChoice was formed using: >> >> >> Item.add(new DropDownChoice("state", new >> PropertyModel((CompanyDTO) item.getDefaultModelObject(), >> "state"), stateList))); >> >> >> Note: "item "is there because this is used in the populateItem method >> of a ListView. >> >> >> Now the code is: >> >> >> Model< StateDisplay > selectedState = new Model<>(); >> >> >> ChoiceRenderer choiceRenderer = new >> ChoiceRenderer("id", "id"); >> >> >> Item.add(new StateDropDownChoice< StateDisplay >("state", >> selectedState, stateList, choiceRenderer)); >> >> >> After the selection is made the model (selectedState) is set to the >> chosen StateDisplay, but there is no easy way to the simple String >> value set on the CompanyDTO. >> >> >> How can I make the dropdown selection update the domain model's >> simple String. >> >> >> Thanks >> >> Bruce >> >> > - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: How to make a Wiket DropDownChoice with complex a model return a simple String
Here is some code of a possible solution: public class DropDownChoiceForString extends DropDownChoice { private IModel targetModel; public DropDownChoiceForString(String id, IModel model, IModel targetModel, List choices, IChoiceRenderer renderer) { super(id, model, choices, renderer); this.targetModel = targetModel; } protected DropDownChoiceForString(String id, IModel model, IModel targetModel) { this(id, model, targetModel, Collections. emptyList(), null); } @Override protected void onInitialize() { super.onInitialize(); // load the initial choice. setModelObject(convertChoiceIdToChoice(targetModel.getObject())); } @Override protected void onDetach() { super.onDetach(); targetModel.detach(); } @Override protected void onModelChanged() { super.onModelChanged(); T newSelection = getModelObject(); int choiceIndex = getChoices().indexOf(newSelection); // update the string source with the selected value. targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, choiceIndex)); } } > Hi, > > I needed something similar in a couple of projects. One solution is to > create a custom DropDownChoice which takes also the string model to > update. Then you can override onModelChanged to update the string > model with the new value that can be extracted with the ChoiceRenderer. >> To make this easier to understand, let's say that I have a list of state >> abbreviations for example AL, AK, AZ, AR etcetera (the actual example >> has >> two letter abbreviations also, but much longer names). I need a >> DropDownChoice that shows the abbreviations for selection (e.g., AK) but >> will show the full name as a tooltip whenever the mouse hovers over >> one of >> the choice (e.g., AK shows Alaska). >> >> >> I have developed a solution that is based on the example presented here: >> http://stackoverflow.com/questions/12234738/wicket-dropdownchoice-titles-too >> >> ltips-for-options >> >> that develops a subclass of DropDownChoice and overrides >> appendOptionHtml >> to append a "title" to the buffer. My class takes a StateDisplay >> object as >> its model, and the model has two fields, an "id" that is displayed in >> the >> list, and a "fullName" which is added as the title by the >> appendOptionHtml >> method. >> >> >> This works fine for displaying thedropdown with the tooltip, but the >> problem is that the model is a complex object( StateDisplay) with two >> fields, but the underlying domain object needs a simple String with the >> state ID. >> >> >> Prior to implementing the tooltip, the DrowdownChoice was formed using: >> >> >> Item.add(new DropDownChoice("state", new >> PropertyModel((CompanyDTO) item.getDefaultModelObject(), >> "state"), >> stateList))); >> >> >> Note: "item "is there because this is used in the populateItem method >> of a >> ListView. >> >> >> Now the code is: >> >> >> Model< StateDisplay > selectedState = new Model<>(); >> >> >> ChoiceRenderer choiceRenderer = new >> ChoiceRenderer("id", "id"); >> >> >> Item.add(new StateDropDownChoice< StateDisplay >("state", selectedState, >> stateList, choiceRenderer)); >> >> >> After the selection is made the model (selectedState) is set to the >> chosen >> StateDisplay, but there is no easy way to the simple String value set on >> the CompanyDTO. >> >> >> How can I make the dropdown selection update the domain model's simple >> String. >> >> >> Thanks >> >> Bruce >> >> > - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: How to make a Wiket DropDownChoice with complex a model return a simple String
Hi, I needed something similar in a couple of projects. One solution is to create a custom DropDownChoice which takes also the string model to update. Then you can override onModelChanged to update the string model with the new value that can be extracted with the ChoiceRenderer. To make this easier to understand, let's say that I have a list of state abbreviations for example AL, AK, AZ, AR etcetera (the actual example has two letter abbreviations also, but much longer names). I need a DropDownChoice that shows the abbreviations for selection (e.g., AK) but will show the full name as a tooltip whenever the mouse hovers over one of the choice (e.g., AK shows Alaska). I have developed a solution that is based on the example presented here: http://stackoverflow.com/questions/12234738/wicket-dropdownchoice-titles-too ltips-for-options that develops a subclass of DropDownChoice and overrides appendOptionHtml to append a "title" to the buffer. My class takes a StateDisplay object as its model, and the model has two fields, an "id" that is displayed in the list, and a "fullName" which is added as the title by the appendOptionHtml method. This works fine for displaying thedropdown with the tooltip, but the problem is that the model is a complex object( StateDisplay) with two fields, but the underlying domain object needs a simple String with the state ID. Prior to implementing the tooltip, the DrowdownChoice was formed using: Item.add(new DropDownChoice("state", new PropertyModel((CompanyDTO) item.getDefaultModelObject(), "state"), stateList))); Note: "item "is there because this is used in the populateItem method of a ListView. Now the code is: Model< StateDisplay > selectedState = new Model<>(); ChoiceRenderer choiceRenderer = new ChoiceRenderer("id", "id"); Item.add(new StateDropDownChoice< StateDisplay >("state", selectedState, stateList, choiceRenderer)); After the selection is made the model (selectedState) is set to the chosen StateDisplay, but there is no easy way to the simple String value set on the CompanyDTO. How can I make the dropdown selection update the domain model's simple String. Thanks Bruce - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org