Dynamically set value of one drop down list based on value of another drop down list

251 views
Skip to first unread message

weheh

unread,
Jul 10, 2010, 2:12:40 PM7/10/10
to web2py-users
This is an old issue that I'm finally getting back to. I've researched
this forum heavily for indications about how to do this and have
concluded that perhaps there is some new functionality in web2py that
will accomplish this for me automatically.

The issue is I want a form with two drop downs where the value of the
2nd drop down is contingent on the value of the first.

#models
db.define_table('animal',Field('name'))
db.define_table('pet',Field('name'),Field('animal',db.animal,requires=IS_IN_DB(db,'animal.id','%
(name)s'))

# animal is preloaded with 2 types: "dog" and "cat"
# pet is preloaded with a few pets:
# rex, dog
# bowser, dog
# luna, cat
# felix, cat

#controller
form=SQLFORM.factory(db.animal,db.pet)
...

When the form is displayed, I want it so that when the animal type is
set to dog, the pet names will only display "rex" and "bowser". If the
user selects the animal type to "cat", then the pet list dynamically
changes to "luna" and "felix".

What is the latest and greatest way to accomplish this? Should I be
going to jQuery (my default choice)?

mr.freeze

unread,
Jul 10, 2010, 2:36:15 PM7/10/10
to web2py-users

weheh

unread,
Jul 10, 2010, 3:01:04 PM7/10/10
to web2py-users
Hi Nathan -- yes I studied it pretty carefully. Can't say I understand
the whole thing, but I think it's more complicated than I need,
frankly. I've been trying to figure out whether I can do this with a
mixture of jQuery and a custom widget. Right now, I'm thinking the
jQuery is all I really need.

weheh

unread,
Jul 10, 2010, 3:06:35 PM7/10/10
to web2py-users
It seems like this is such a common occurrence and that so many people
have asked about this already, that there isn't some kind of native
helper function within web2py that does this dynamically. Seems like
this capability should be specified as an argument(s) to a requires
statement:

db.animal.requires=IS_IN_DB(db,'animal.id','%(name)s',onselect=True)

where onselect=True causes it to dynamically update according to value
selected from animal table.

acc...@inbox.com

unread,
Jul 10, 2010, 4:12:01 PM7/10/10
to web2py-users

weheh

unread,
Jul 10, 2010, 5:57:52 PM7/10/10
to web2py-users
Acculon, that is helpful, though not exactly what I need. Anyway, I'm
making some progress.

Does anyone know exactly how to replace the contents of a SELECT? In
other words, I have a bunch of options that I want to build up then
pass back via AJAX to the select list. But I can't seem to get it to
work exactly right. I'm having to encapsulate the entire select list
by a DIV and then replace the entire contents of the DIV, select list
and all options.

On Jul 10, 4:12 pm, "accu...@inbox.com" <accu...@inbox.com> wrote:
> This may be what you are looking for:
>  http://groups.google.com/group/web2py/msg/1c4897120e4f124b
>  http://groups.google.com/group/web2py/browse_thread/thread/4a6149ddcb...
Reply all
Reply to author
Forward
0 new messages