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