You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Claudia Hirt <hi...@gmx.de> on 2018/10/26 11:54:41 UTC

Question about IChoiceRenderer

Hi all, 

i have a question about IChoiceRenderer used for Selectboxes. 

There is a method String getIdValue(Tobject, int index) to get the unique id value of an option object. 
The javadoc says the following: 

"This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index." 

I wonder if it's a good idea to have the object and the index as parameters in this method. If the id value should be kind of a primary key of the object, it should not depend on the index, which has nothing to do with the object. 
I cannot use this method in my case to check if an object which is not in the choices list has the same id value than the selected one in my selectbox. I tried to do this with wicket-extensions Select, which does not have a choice renderer by default and I only have the object in isSelectedmethod of Select, but not the index. 

Best regards, 
Claudia

Re: Question about IChoiceRenderer

Posted by Martin Terra <ma...@koodaripalvelut.com>.
I would definitely recommend leveraging the class hierarcy
of DropDownChoice/AbstractSingleSelectChoice in case you are using a single
select, and if it is multi select then respective ListMultipleChoice.

This way you can leverage the existing implementation of isSelected etc.

**
Martin

ti 30. lokak. 2018 klo 20.54 Claudia Hirt (hirt-claudia@gmx.de) kirjoitti:

> Thanks for the tip Martin Terra!
>
> And thank you for the example code Martin Grigorov! Good to know I'm on
> the right track.
>
> I still have one proplem left, I know it really is a minor thing. I
> developed a framework component for a group selectbox which takes a
> HashMap<String, T> containing the optgroups and their corresponding
> options. Working with generic option objects I use a ChoiceRenderer to
> give the user the possiblity to map the id- and display-value from the
> object. This works fine so far, until you get to
> isSelected(IModel<?> model)
> In AbstractChoice you can set an object as selected which does not have
> to be the same object as the one in the choices. It compares the objects
> id using the given choice renderer.
> But Select only does an object equals compare. And there's no way to use
> the choice renderer in isSelected correctly because there is no index in
> the method signature.
> As I said this is really a minor thing, but the inconsistency with
> AbstractChoice annoys me ;)
> How would you suggest to solve this?
>
> Best regards,
> Claudia Hirt
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Question about IChoiceRenderer

Posted by Claudia Hirt <hi...@gmx.de>.
Thanks for the tip Martin Terra!

And thank you for the example code Martin Grigorov! Good to know I'm on 
the right track.

I still have one proplem left, I know it really is a minor thing. I 
developed a framework component for a group selectbox which takes a 
HashMap<String, T> containing the optgroups and their corresponding 
options. Working with generic option objects I use a ChoiceRenderer to 
give the user the possiblity to map the id- and display-value from the 
object. This works fine so far, until you get to
isSelected(IModel<?> model)
In AbstractChoice you can set an object as selected which does not have 
to be the same object as the one in the choices. It compares the objects 
id using the given choice renderer.
But Select only does an object equals compare. And there's no way to use 
the choice renderer in isSelected correctly because there is no index in 
the method signature.
As I said this is really a minor thing, but the inconsistency with 
AbstractChoice annoys me ;)
How would you suggest to solve this?

Best regards,
Claudia Hirt

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


Re: Question about IChoiceRenderer

Posted by Martin Terra <ma...@koodaripalvelut.com>.
Also, if you need optrgoups and styling while needing to keep it simple,
you can implement IStyledChoiceRenderer
<http://apache-wicket.1842946.n4.nabble.com/FormTester-and-Select-component-td1844682.html>
and
extend DropDownChoice with some subtle changes.

**
Martin

ti 30. lokak. 2018 klo 17.04 Martin Grigorov (mgrigorov@apache.org)
kirjoitti:

> Hi,
>
> DropDownChoice component should be used when you have a list of objects
> which you want to render in an HTML select element without fancy things
> like option grouping and styling.
> If you need to do grouping and/or styling then you need to use Select and
> add SelectOption for each of your objects.
> See
>
> https://github.com/apache/wicket/blob/master/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SelectPage.java
> for an example.
> The grouping is only in the HTML in this example (
>
> https://github.com/apache/wicket/blob/master/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SelectPage.html#L23
> )
> but you can use WebMarkupContainer on the optgroup elements to set its
> attributes dynamically.
>
> On Tue, Oct 30, 2018 at 4:49 PM Claudia Hirt <hi...@gmx.de> wrote:
>
> > Hi,
> > I use select to create a selectbox containing optgroups which is not
> > possible with abstractchoice as far as i know. And i want to also use
> > choice renderer for this “groupselectbox component” to have the same
> > behaviour as in abstract choice.
> > Maybe I’m wrong with the whole concept. What is your recommendation for
> > how to create a group selectbox?
> >
> > Best regards,
> > Claudia
> >
> > > Am 30.10.2018 um 12:02 schrieb Martin Grigorov <mg...@apache.org>:
> > >
> > > Hi Claudia,
> > >
> > > I do not quite understand your question/suggestion.
> > >
> > > IChoiceRenderer is used by AbstractChoice and its specializations.
> > > "T object" is your type, so you should decide how to construct a stable
> > id
> > > out of it. In case you cannot get such id then you may use the passed
> > > index, but in this case you have to make sure that the same index is
> > always
> > > used for this object instance (as the javadoc explains).
> > >
> > > Wicket-Extensions' Select doesn't use IChoiceRenderer at all, so I
> don't
> > > see what is the relation to the first part of your question.
> > >
> > >> On Fri, Oct 26, 2018 at 2:54 PM Claudia Hirt <hi...@gmx.de>
> > wrote:
> > >>
> > >> Hi all,
> > >>
> > >> i have a question about IChoiceRenderer used for Selectboxes.
> > >>
> > >> There is a method String getIdValue(Tobject, int index) to get the
> > unique
> > >> id value of an option object.
> > >> The javadoc says the following:
> > >>
> > >> "This method is called to get the id value of an object (used as the
> > value
> > >> attribute of a choice element) The id can be extracted from the object
> > like
> > >> a primary key, or if the list is stable you could just return a
> > toString of
> > >> the index."
> > >>
> > >> I wonder if it's a good idea to have the object and the index as
> > >> parameters in this method. If the id value should be kind of a primary
> > key
> > >> of the object, it should not depend on the index, which has nothing to
> > do
> > >> with the object.
> > >> I cannot use this method in my case to check if an object which is not
> > in
> > >> the choices list has the same id value than the selected one in my
> > >> selectbox. I tried to do this with wicket-extensions Select, which
> does
> > not
> > >> have a choice renderer by default and I only have the object in
> > >> isSelectedmethod of Select, but not the index.
> > >>
> > >> Best regards,
> > >> Claudia
> > >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>

Re: Question about IChoiceRenderer

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

DropDownChoice component should be used when you have a list of objects
which you want to render in an HTML select element without fancy things
like option grouping and styling.
If you need to do grouping and/or styling then you need to use Select and
add SelectOption for each of your objects.
See
https://github.com/apache/wicket/blob/master/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SelectPage.java
for an example.
The grouping is only in the HTML in this example (
https://github.com/apache/wicket/blob/master/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SelectPage.html#L23)
but you can use WebMarkupContainer on the optgroup elements to set its
attributes dynamically.

On Tue, Oct 30, 2018 at 4:49 PM Claudia Hirt <hi...@gmx.de> wrote:

> Hi,
> I use select to create a selectbox containing optgroups which is not
> possible with abstractchoice as far as i know. And i want to also use
> choice renderer for this “groupselectbox component” to have the same
> behaviour as in abstract choice.
> Maybe I’m wrong with the whole concept. What is your recommendation for
> how to create a group selectbox?
>
> Best regards,
> Claudia
>
> > Am 30.10.2018 um 12:02 schrieb Martin Grigorov <mg...@apache.org>:
> >
> > Hi Claudia,
> >
> > I do not quite understand your question/suggestion.
> >
> > IChoiceRenderer is used by AbstractChoice and its specializations.
> > "T object" is your type, so you should decide how to construct a stable
> id
> > out of it. In case you cannot get such id then you may use the passed
> > index, but in this case you have to make sure that the same index is
> always
> > used for this object instance (as the javadoc explains).
> >
> > Wicket-Extensions' Select doesn't use IChoiceRenderer at all, so I don't
> > see what is the relation to the first part of your question.
> >
> >> On Fri, Oct 26, 2018 at 2:54 PM Claudia Hirt <hi...@gmx.de>
> wrote:
> >>
> >> Hi all,
> >>
> >> i have a question about IChoiceRenderer used for Selectboxes.
> >>
> >> There is a method String getIdValue(Tobject, int index) to get the
> unique
> >> id value of an option object.
> >> The javadoc says the following:
> >>
> >> "This method is called to get the id value of an object (used as the
> value
> >> attribute of a choice element) The id can be extracted from the object
> like
> >> a primary key, or if the list is stable you could just return a
> toString of
> >> the index."
> >>
> >> I wonder if it's a good idea to have the object and the index as
> >> parameters in this method. If the id value should be kind of a primary
> key
> >> of the object, it should not depend on the index, which has nothing to
> do
> >> with the object.
> >> I cannot use this method in my case to check if an object which is not
> in
> >> the choices list has the same id value than the selected one in my
> >> selectbox. I tried to do this with wicket-extensions Select, which does
> not
> >> have a choice renderer by default and I only have the object in
> >> isSelectedmethod of Select, but not the index.
> >>
> >> Best regards,
> >> Claudia
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Question about IChoiceRenderer

Posted by Claudia Hirt <hi...@gmx.de>.
Hi,
I use select to create a selectbox containing optgroups which is not possible with abstractchoice as far as i know. And i want to also use choice renderer for this “groupselectbox component” to have the same behaviour as in abstract choice. 
Maybe I’m wrong with the whole concept. What is your recommendation for how to create a group selectbox?

Best regards,
Claudia 

> Am 30.10.2018 um 12:02 schrieb Martin Grigorov <mg...@apache.org>:
> 
> Hi Claudia,
> 
> I do not quite understand your question/suggestion.
> 
> IChoiceRenderer is used by AbstractChoice and its specializations.
> "T object" is your type, so you should decide how to construct a stable id
> out of it. In case you cannot get such id then you may use the passed
> index, but in this case you have to make sure that the same index is always
> used for this object instance (as the javadoc explains).
> 
> Wicket-Extensions' Select doesn't use IChoiceRenderer at all, so I don't
> see what is the relation to the first part of your question.
> 
>> On Fri, Oct 26, 2018 at 2:54 PM Claudia Hirt <hi...@gmx.de> wrote:
>> 
>> Hi all,
>> 
>> i have a question about IChoiceRenderer used for Selectboxes.
>> 
>> There is a method String getIdValue(Tobject, int index) to get the unique
>> id value of an option object.
>> The javadoc says the following:
>> 
>> "This method is called to get the id value of an object (used as the value
>> attribute of a choice element) The id can be extracted from the object like
>> a primary key, or if the list is stable you could just return a toString of
>> the index."
>> 
>> I wonder if it's a good idea to have the object and the index as
>> parameters in this method. If the id value should be kind of a primary key
>> of the object, it should not depend on the index, which has nothing to do
>> with the object.
>> I cannot use this method in my case to check if an object which is not in
>> the choices list has the same id value than the selected one in my
>> selectbox. I tried to do this with wicket-extensions Select, which does not
>> have a choice renderer by default and I only have the object in
>> isSelectedmethod of Select, but not the index.
>> 
>> Best regards,
>> Claudia
>> 


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


Re: Question about IChoiceRenderer

Posted by Martin Grigorov <mg...@apache.org>.
Hi Claudia,

I do not quite understand your question/suggestion.

IChoiceRenderer is used by AbstractChoice and its specializations.
"T object" is your type, so you should decide how to construct a stable id
out of it. In case you cannot get such id then you may use the passed
index, but in this case you have to make sure that the same index is always
used for this object instance (as the javadoc explains).

Wicket-Extensions' Select doesn't use IChoiceRenderer at all, so I don't
see what is the relation to the first part of your question.

On Fri, Oct 26, 2018 at 2:54 PM Claudia Hirt <hi...@gmx.de> wrote:

> Hi all,
>
> i have a question about IChoiceRenderer used for Selectboxes.
>
> There is a method String getIdValue(Tobject, int index) to get the unique
> id value of an option object.
> The javadoc says the following:
>
> "This method is called to get the id value of an object (used as the value
> attribute of a choice element) The id can be extracted from the object like
> a primary key, or if the list is stable you could just return a toString of
> the index."
>
> I wonder if it's a good idea to have the object and the index as
> parameters in this method. If the id value should be kind of a primary key
> of the object, it should not depend on the index, which has nothing to do
> with the object.
> I cannot use this method in my case to check if an object which is not in
> the choices list has the same id value than the selected one in my
> selectbox. I tried to do this with wicket-extensions Select, which does not
> have a choice renderer by default and I only have the object in
> isSelectedmethod of Select, but not the index.
>
> Best regards,
> Claudia
>