You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by carlson weber filho - Master CIM Informática <cw...@mastercim.com.br> on 2009/07/27 21:21:32 UTC

DropDownChoice with ID and Value

Newbie question:
I have a list of 3 options, let's supose it's 1=abc, 2=def, 3=ghi

I will present to my user the select field with the choices abc, def and 
ghi. I want that my model updates with 1, 2 and 3. I did not find a way 
to achieve this (must me a simple thing).


best regards,

carlson

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


Re: DropDownChoice with ID and Value

Posted by carlson weber filho - Master CIM Informática <cw...@mastercim.com.br>.
Will try it, thanks a lot
Scott Swank escreveu:
> Look at ChoiceRenderer.  It's pretty easy to write one that takes a
> map in it's constructor if your needs are that simple.
>
> Scott
>
> 2009/7/27 carlson weber filho - Master CIM Informática
> <cw...@mastercim.com.br>:
>   
>> Newbie question:
>> I have a list of 3 options, let's supose it's 1=abc, 2=def, 3=ghi
>>
>> I will present to my user the select field with the choices abc, def and
>> ghi. I want that my model updates with 1, 2 and 3. I did not find a way to
>> achieve this (must me a simple thing).
>>
>>
>> best regards,
>>
>> carlson
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>     
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>   


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


Re: DropDownChoice with ID and Value

Posted by Scott Swank <sc...@gmail.com>.
Look at ChoiceRenderer.  It's pretty easy to write one that takes a
map in it's constructor if your needs are that simple.

Scott

2009/7/27 carlson weber filho - Master CIM Informática
<cw...@mastercim.com.br>:
> Newbie question:
> I have a list of 3 options, let's supose it's 1=abc, 2=def, 3=ghi
>
> I will present to my user the select field with the choices abc, def and
> ghi. I want that my model updates with 1, 2 and 3. I did not find a way to
> achieve this (must me a simple thing).
>
>
> best regards,
>
> carlson
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: DropDownChoice with ID and Value

Posted by Mathias Nilsson <wi...@gmail.com>.
Yes, you are right.

Didn't read the question close enough. 
-- 
View this message in context: http://www.nabble.com/DropDownChoice-with-ID-and-Value-tp24686742p24703752.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: DropDownChoice with ID and Value

Posted by John Krasnay <jo...@krasnay.ca>.
The thing that makes your example awkward (IMHO, of course) is that it
leaves out how the value that the user selects makes its way back into
the app's domain object. The OP said he wants "my model updates with 1,
2, and 3." From that I understand he has some object with an int
property...

class Widget {
    private int type;
    //...
}

Widget myWidget;

To complete your example, you would have to do the following when the
component was initialized:

- create a variable of type Choice to hold the selection
- loop through the list of choices, finding the one that matches
  myWidget.type and storing it in the variable
- pass a PropertyModel to the DDC pointing to the choice variable

Then, when the form is submitted, the Choice variable would contain a
(possibly different) Choice object, corresponding to the one the user
selected. You would then have to set myWidget.type from the id property
of your choice variable.

In my example, this is all done automatically, since the PropertyModel
points directly to the property to be updated. But this is only possible
if you give DDC a list of objects of the same type as the property.

jk


On Mon, Jul 27, 2009 at 05:13:05PM -0700, Mathias Nilsson wrote:
> 
> Wasn't this more awkward?
> 
> -- 
> View this message in context: http://www.nabble.com/DropDownChoice-with-ID-and-Value-tp24686742p24690478.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
> 

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


Re: DropDownChoice with ID and Value

Posted by Mathias Nilsson <wi...@gmail.com>.
Wasn't this more awkward?

-- 
View this message in context: http://www.nabble.com/DropDownChoice-with-ID-and-Value-tp24686742p24690478.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: DropDownChoice with ID and Value

Posted by John Krasnay <jo...@krasnay.ca>.
This is an awkward way to do it, and still doesn't set the selected
value back into his model.

The golden rule of DropDownChoice is: objects in the list must be of the
same type as the model object. If you want your model to be updated with
an int, you have to give DDC a list of Integer. The second rule is that
you use a ChoiceRenderer to convert this value (the int) to a display
value:

class MyDomainClass {
  private int selection;
}

MyDomainClass myobj;

add(new DropDownChoice("ddc", 
    new PropertyModel(myobj, "selection"),
    Arrays.asList(1, 2, 3),
    new ChoiceRenderer() {
        public Object getDisplayValue(Object value) {

            // value is one of the objects from the list,
            // the one that the user selected
            Integer i = (Integer) value;

            // This function maps 1 to "abc" and so on...
            return getName(i);
        })
    );

jk


On Mon, Jul 27, 2009 at 12:36:39PM -0700, Mathias Nilsson wrote:
> 
> class Choice{
>   private int id; 
>   private Stringvalue;
> 
>   // getters and setters
> }
> 
> List<Choice> choices = new LinkedList<Choice>();
> Choice c = new Choice();
> c.setId( 1 );
> c.setValue( "abc" );
> 
> choices.add( c );
> 
> // add the other values
> 
> 
> // The choice with id 1 will be selected.
> DropDownChoice drop = new DropDownChoice( "drop" , new Model( c ) , choices,
> new ChoiceRenderer( "value" , "id" ) );
> 
> -- 
> View this message in context: http://www.nabble.com/DropDownChoice-with-ID-and-Value-tp24686742p24686931.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
> 

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


Re: DropDownChoice with ID and Value

Posted by Mathias Nilsson <wi...@gmail.com>.
class Choice{
  private int id; 
  private Stringvalue;

  // getters and setters
}

List<Choice> choices = new LinkedList<Choice>();
Choice c = new Choice();
c.setId( 1 );
c.setValue( "abc" );

choices.add( c );

// add the other values


// The choice with id 1 will be selected.
DropDownChoice drop = new DropDownChoice( "drop" , new Model( c ) , choices,
new ChoiceRenderer( "value" , "id" ) );

-- 
View this message in context: http://www.nabble.com/DropDownChoice-with-ID-and-Value-tp24686742p24686931.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