This question comes up often. We should create an article about it on the Wiki.

Eelco

Johan Compagner wrote:

see the IChoice interface.
With this you can let the DropDownChoice now what is the display and what is the id where the DropDownChoice can identify what object is selected.


johan


Joe Toth wrote:

Thanks, I understood that, but my mind keeps having flashbacks of struts. I don't have to worry about the index now, but what about the displayName?

Is there an easy way to use a custom displayName instead of using the default toString()?
For instance my database object's toString() prints out all the values of the columns, when I just want to display a 'name' column in the dropdown. I know I can do approach #2, but that would entail me selecting the objects from the database, iterating through them to create new pseudo objects. I would like to just create a ChoiceList passing it an interface to render getDisplayValue.



On 4/28/05, *Christopher Turner* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:


Hi Joe,
I think the problem you are having is that you are still worrying
about the index value that is used when the form containing the
rendered select element is submitted. This is one of the areas
that you have to change your mindset when using Wicket - it takes
a while to get your head around it.
What you have to remember is that from a conceptual point of view
a drop down choice is just a list of objects that you can pick
from. The fact that HTML requires them to have a submit value and
a display value is kind of irrelevant. If you give Wicket the
list of possible objects it will manage its own indexing. When
the page is rendered, the list option that matches the current
object in your model will be selected. When the form is
submitted Wicket will do all of the index resolution for you and
then set the object corresponding to the dropdown option that was
selected back in your model.
Thus, in your example, after the form is submitted, the model
object that you supplied to the drop down list will have the
Integer object representing the year that the user picked - you
need do nothing more than use this value. When you start thinking
about dropdowns of database values, all you need to do is create
a list of objects, where each object represents one of the values
from the database. Once your form is submitted the model will
contain the object that represents the value that the user
selected. There are two approaches: 1) The objects you use could
be real business objects represented by a row in the database OR
2) The objects could be pseudo objects containing a primary key
and display value when you don't want to instantiate actual
business object instances in order to present the dropdown list.
Try to detach yourself from the underlying name=value parameter
mechanism of HTTP and think in terms of models and objects. This
is where Wicket saves you all of the work.
p.s. The DropDownChoice component has built-in support for a
"Please Select" option - so you don't need to build this
yourself. Have a look at the code/examples to see how it works.
regards,
Chris


        How do I create a dropdown list and pick what to use for an
        id and displayValue?
        I want to create a dropdown with the first item to be
        index=0, displayValue=Please Select then the rest to be the
        last 100 years.
        I know I'll have the same problem when I want to create
        dropdowns of database objects.
        Is there an easy solution?

        This is what I have so far, but the id is the index.

        ChoiceList years = new ChoiceList();
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        years.add("Please Select");
        for (int i = 0; i <= 100; i++) {
            Integer year = currentYear - i;
            years.add(year);
        }

        DropDownChoice yearDropDown = new DropDownChoice("year",
        model, years);



        Thanks





------------------------------------------------------- SF.Net email is sponsored by: Tell us your software development plans! Take this survey and enter to win a one-year sub to SourceForge.net Plus IDC's 2005 look-ahead and a copy of this survey Click here to start! http://www.idcswdc.com/cgi-bin/survey?id=105hix _______________________________________________ Wicket-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to