You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Lon Varscsak <lo...@gmail.com> on 2016/05/05 00:28:49 UTC
DropDownChoice and Object/Property
Hey all,
Is there a way to have a DropDownChoice use a complex object (think ORM
class) as it’s list, but get/set the Model’s value as the ID instead of the
complex object?
So for:
new DropDownChoice<>(“shipMethod”, new PropertyModel<>(OrderPage.this
.getOrderController().order().shippingHeader(), “shipMethod"), new
ListModel<>(shipMethods), new ChoiceRenderer<>("description", "id"))
It puts in the ORM object’s toString into the “shipMethod” column and I
want it to put in the value for “id” from the renderer. Additionally, I
want it to get it’s initial value from the “id” value instead and convert
it to the ORM object (from shipMethods list).
Hopefully this makes sense. :P
-Lon (Wicket Newb, WebObjects Refugee)
Re: DropDownChoice and Object/Property
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
In that case you need something like:
IModel<String> model = new Model<>(OrderPage.this
.getOrderController().order().shippingHeader()getShipMethod().getId());
IModel<List<String>> models = new
ListModel<String>(shipMethods.stream().map(ShipMethod::getId).collect(Collectors.toList()));
IChoiceRenderer<String> renderer = new IChoiceRenderer<>() {
public String getIdValue(String id, int index) {return id;}
public String getDisplayValue(String id) {
for (ShipMethod sm : shipMethods) {
if (id.equals(sm.getId())) return sm.getDescription();
}
throw new RuntimeException("Cannot find ShipMethod with id: " + id);
}
}
... = new DropDownChoice<String>(“shipMethod”, model, models, renderer);
Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov
On Thu, May 5, 2016 at 8:49 PM, Lon Varscsak <lo...@gmail.com> wrote:
> haha, sorry…here’s another pass:
>
> So the objects that are in the list are called ShipMethod and it has a
> “description” which I want to use for display, and an “id” which is the
> thing I want stored in the Order object. But with the property model and
> ChoiceRenderer as I have it, it’s expecting to push a ShipMethod object
> into the Order object’s “shipMethod” attribute, but it’s not modeled that
> way. In the Order object I just want to store the “id” from ShipMethod in
> “shipMethod”.
>
> On Wed, May 4, 2016 at 11:45 PM, Martin Grigorov <mg...@apache.org>
> wrote:
>
> > Hi,
> >
> >
> > On Thu, May 5, 2016 at 2:28 AM, Lon Varscsak <lo...@gmail.com>
> > wrote:
> >
> > > Hey all,
> > >
> > > Is there a way to have a DropDownChoice use a complex object (think ORM
> > > class) as it’s list, but get/set the Model’s value as the ID instead of
> > the
> > > complex object?
> > >
> > > So for:
> > >
> > > new DropDownChoice<>(“shipMethod”, new PropertyModel<>(OrderPage.this
> > > .getOrderController().order().shippingHeader(), “shipMethod"), new
> > > ListModel<>(shipMethods), new ChoiceRenderer<>("description", "id"))
> > >
> > > It puts in the ORM object’s toString into the “shipMethod” column and I
> > > want it to put in the value for “id” from the renderer. Additionally,
> I
> > > want it to get it’s initial value from the “id” value instead and
> convert
> > > it to the ORM object (from shipMethods list).
> > >
> > > Hopefully this makes sense. :P
> > >
> >
> > Not much :p
> > You missed to show us ShipMethod class.
> > Does it have "description" and "id" properties ?
> >
> >
> > >
> > > -Lon (Wicket Newb, WebObjects Refugee)
> > >
> >
>
Re: DropDownChoice and Object/Property
Posted by Lon Varscsak <lo...@gmail.com>.
haha, sorry…here’s another pass:
So the objects that are in the list are called ShipMethod and it has a
“description” which I want to use for display, and an “id” which is the
thing I want stored in the Order object. But with the property model and
ChoiceRenderer as I have it, it’s expecting to push a ShipMethod object
into the Order object’s “shipMethod” attribute, but it’s not modeled that
way. In the Order object I just want to store the “id” from ShipMethod in
“shipMethod”.
On Wed, May 4, 2016 at 11:45 PM, Martin Grigorov <mg...@apache.org>
wrote:
> Hi,
>
>
> On Thu, May 5, 2016 at 2:28 AM, Lon Varscsak <lo...@gmail.com>
> wrote:
>
> > Hey all,
> >
> > Is there a way to have a DropDownChoice use a complex object (think ORM
> > class) as it’s list, but get/set the Model’s value as the ID instead of
> the
> > complex object?
> >
> > So for:
> >
> > new DropDownChoice<>(“shipMethod”, new PropertyModel<>(OrderPage.this
> > .getOrderController().order().shippingHeader(), “shipMethod"), new
> > ListModel<>(shipMethods), new ChoiceRenderer<>("description", "id"))
> >
> > It puts in the ORM object’s toString into the “shipMethod” column and I
> > want it to put in the value for “id” from the renderer. Additionally, I
> > want it to get it’s initial value from the “id” value instead and convert
> > it to the ORM object (from shipMethods list).
> >
> > Hopefully this makes sense. :P
> >
>
> Not much :p
> You missed to show us ShipMethod class.
> Does it have "description" and "id" properties ?
>
>
> >
> > -Lon (Wicket Newb, WebObjects Refugee)
> >
>
Re: DropDownChoice and Object/Property
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
On Thu, May 5, 2016 at 2:28 AM, Lon Varscsak <lo...@gmail.com> wrote:
> Hey all,
>
> Is there a way to have a DropDownChoice use a complex object (think ORM
> class) as it’s list, but get/set the Model’s value as the ID instead of the
> complex object?
>
> So for:
>
> new DropDownChoice<>(“shipMethod”, new PropertyModel<>(OrderPage.this
> .getOrderController().order().shippingHeader(), “shipMethod"), new
> ListModel<>(shipMethods), new ChoiceRenderer<>("description", "id"))
>
> It puts in the ORM object’s toString into the “shipMethod” column and I
> want it to put in the value for “id” from the renderer. Additionally, I
> want it to get it’s initial value from the “id” value instead and convert
> it to the ORM object (from shipMethods list).
>
> Hopefully this makes sense. :P
>
Not much :p
You missed to show us ShipMethod class.
Does it have "description" and "id" properties ?
>
> -Lon (Wicket Newb, WebObjects Refugee)
>