What is the best way to update dropdown list based on previous selection?

25 views
Skip to first unread message

Jordan Myers

unread,
Aug 31, 2015, 9:00:41 PM8/31/15
to web2py-users
Hello everyone,

I'm developing a rather complex form where the selection options need to depend on what the user selects in a previous field.
As a simplistic example, let's say there are two models, Owner and Thing, where an Owner can have multiple Things. I would like to have a form where you first pick an Owner, and then pick a Thing that the Owner has (and only Things the owner has). Ideally this would be done without using multiple forms (as I have many such complicated fields). There can be many owners so it is not feasible to have a hidden dropdown for each owner.

Here is sort-of what I envision:
db.define_table("person", Field('name'))
db.define_table("thing", Field('owner', 'reference person'))
db.define_table("thing_selection", Field('owner', 'reference person'), 
                Field('thing', 'reference thing', requires=IS_IN_DB(db(thisform.thing.owner == thisform.person.id), 'thing.id'))
                )

where thisform is what the user is submitting as a SQLFORM (or crud.create)

I imagine the solution will likely require AJAX. Is there a good resource or practice or things to keep in mind for editing a dropdown form with ajax, and how might I do it? Or if you have any other suggestions they are welcome. Thanks!
Reply all
Reply to author
Forward
0 new messages