How to implement multiple-selection drop-down lists in TG?

3 views
Skip to first unread message

midtoad

unread,
Mar 9, 2008, 7:32:59 PM3/9/08
to TurboGears
I want to allow a multiple selection form in my TG app. Let's say my
form lists types of fruit in a drop-down list. I want to allow users
to be able to select 'apple', 'banana' and 'pear', and the Submit
those results. Or similarly, I want to have a checkbox on every row
of a table, and be able to send the ID of each row that was
selected.

It seems like in either case, what ends up getting passed to
controllers.py is multiple instances of the same parameter. In the
case of the fruit, the controller function will get called like this
by the form:

save_data(fruit="apple", fruit="banana", fruit="pear")
and I'm sure that will NOT work!

Same thing would happen with the checkbox.

What solutions have you users found to get around this limitation?
If possible, I'd like to do it without using widgets, since I haven't
learned how to use them yet. Of course, if that's the only way, I may
have to learn sooner rather than later...

thanks
S

Jorge Godoy

unread,
Mar 9, 2008, 7:55:21 PM3/9/08
to turbo...@googlegroups.com
Em Sunday 09 March 2008 20:32:59 midtoad escreveu:
> It seems like in either case, what ends up getting passed to
> controllers.py is multiple instances of the same parameter. In the
> case of the fruit, the controller function will get called like this
> by the form:
>
> save_data(fruit="apple", fruit="banana", fruit="pear")
> and I'm sure that will NOT work!

Have you tested?

FormEncode will convert that to an array so it should be
save_data(fruit=['apple', 'banana', 'pear']).


--
Jorge Godoy <jgo...@gmail.com>

Christoph Zwerschke

unread,
Mar 9, 2008, 8:23:28 PM3/9/08
to turbo...@googlegroups.com
midtoad schrieb:

> I want to allow a multiple selection form in my TG app. Let's say my
> form lists types of fruit in a drop-down list. I want to allow users
> to be able to select 'apple', 'banana' and 'pear', and the Submit
> those results. Or similarly, I want to have a checkbox on every row
> of a table, and be able to send the ID of each row that was
> selected.

Run tg-admin toolbox and click on the Widget Browser.

Have a look at the CheckBox List and Multiple Select Field.

The trick is that the controller receives one parameter that is a list
of all selected values.

-- Christoph

Diez B. Roggisch

unread,
Mar 10, 2008, 4:50:09 AM3/10/08
to turbo...@googlegroups.com
Christoph Zwerschke schrieb:

Don't forget to use the ForEach(convertToList=True)-validator to ensure
that you get a list under all circumstances. Makes the programming easier.

Diez

midtoad

unread,
Mar 11, 2008, 10:43:41 AM3/11/08
to TurboGears


On Mar 9, 6:23 pm, Christoph Zwerschke <c...@online.de> wrote:
> The trick is that the controller receives one parameter that is a list
> of all selected values.

It turns out that isn't quite right. Yes, the controller does receive
a list of all selected values (contained as unicode strings) if you in
fact select multiple values in the drop-down list. But if you only
select one item in the list, the controller receives a string
containing that selection. This complicates the processing of the
returned values.

I ended having to do this in order to deal with both cases: single-
item selection and multiple-item selection: http://paste.turbogears.org/paste/2212

thanks again for pointing me in the right direction.

Christopher Arndt

unread,
Mar 11, 2008, 10:52:10 AM3/11/08
to turbo...@googlegroups.com
midtoad schrieb:

> On Mar 9, 6:23 pm, Christoph Zwerschke <c...@online.de> wrote:
>> The trick is that the controller receives one parameter that is a list
>> of all selected values.
>
> It turns out that isn't quite right. [...] if you only

> select one item in the list, the controller receives a string
> containing that selection.

Please read Diez' previous message again and listen to his advice.

Chris

midtoad

unread,
Mar 11, 2008, 11:55:02 PM3/11/08
to TurboGears


On Mar 11, 8:52 am, Christopher Arndt <chris.ar...@web.de> wrote:

> Please read Diez' previous message again and listen to his advice.

Yes, that was good advice. And a very handy validator! It definitely
is cleaner to use it than write code to check type of parameter.

I probably skipped over it the first time because his post was about
widgets, which I'm not using. I'd forgotten about that validator
suggestion, so thanks for bringing it to my suggestion.

Now back to development!

S
Reply all
Reply to author
Forward
0 new messages