You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Scott Sauyet <li...@sauyet.com> on 2007/10/16 18:38:33 UTC
Getting the ListItems from a ListView
There may be a much better way of doing what I'm trying to do, and if
so, I'd love to hear about it. But what I thought would be simple seems
to be eluding me here.
I have a form with a dynamic number of dropdowns. I'm populating them
using a ListView, and the display is working fine. The trouble is that
in my onSubmit(), I now need to get at the selections made for each, and
I don't know how to get back the ListItems; the only methods which
really seem to expose these are populateItem() and renderItem(), which
run before onSubmit(). Is there a way to get at these? Or can you
suggest another way to structure this so that I have the access needed?
A little more explanation and some sample code is below.
Thanks,
-- Scott Sauyet
The user is purchasing tickets in a section of some venue. That section
will have different buyer types, each with its own price, description,
and maximum allowed tickets. The user should be able to choose, for
instance, two adult tickets, one senior ticket, and three child tickets.
But the buyer types available are data driven; I can't simply
hard-code separate dropdowns for each.
public static class SectionForm extends Form {
private PriceSection section;
public SectionForm(String id, PriceSection section) {
super(id);
this.section = section;
add(new Label("section-description", section.toString()));
List<BuyerType> buyerTypes = section.getBuyerTypes();
add(new ListView("buyer-type", buyerTypes) {
public List<String> quantities;
public void populateItem(ListItem listItem) {
BuyerType buyerType = (BuyerType)
listItem.getModelObject();
listItem.add(new Label("description",
buyerType.getDescription()));
listItem.add(new Label("price",
CURRENCY.format(buyerType.getPrice())));
quantities = allowedQuantities(buyerType);
listItem.add(new DropDownChoice("quantity",
new Model(quantities.get(0)), quantities));
}
});
}
public void onSubmit() {
System.out.println("Chose section " + section);
ListView types = (ListView) get("buyer-type");
System.out.println(((List) types.getModelObject())
.get(0).getClass());
/// ????
for (Object obj: types.?????()) { // What do I do here?
/// ????
ListItem item = (ListItem) obj;
BuyerType type = (BuyerType) item.getModelObject();
String choice = ((DropDownChoice) item.get("quantity"))
.getModelValue();
System.out.println("\t" + type.getDescription()
+ ": " + choice + " @ "
+ CURRENCY.format(type.getPrice()));
}
}
// ...
}
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Getting the ListItems from a ListView
Posted by Scott Sauyet <li...@sauyet.com>.
Scott Sauyet wrote:
> There may be a much better way of doing what I'm trying to do, and if
> so, I'd love to hear about it. But what I thought would be simple seems
> to be eluding me here.
I knew there had to be a better way. I found one.
> I have a form with a dynamic number of dropdowns. I'm populating them
> using a ListView, and the display is working fine. The trouble is that
> in my onSubmit(), I now need to get at the selections made for each, and
> I don't know how to get back the ListItems; the only methods which
> really seem to expose these are populateItem() and renderItem(), which
> run before onSubmit(). Is there a way to get at these? Or can you
> suggest another way to structure this so that I have the access needed?
I wrapped the objects in the list that serves as the ListView model in a
class that had a property that I could use for the model of the
drop-down list. It was pretty straightforward once I stopped trying to
get the ListItems back.
Someday I'll have interesting questions to ask... :-(
-- Scott
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org