0

I am extending my first question to solve a more challenging problem. Considering the below db.py and default.py I'm trying to have 2 cascading drop-downs in a custom form where the second depends to what is selected in the first (see rem'ed text below in "db.define_table('C',").

db.py

db.define_table('A',
      Field('A1', 'string', required=True),
      Field('A2', 'string', required=True),
      format='%(A1)s)

db.define_table('B',
      Field('B1', db.A),
      Field('B2', 'string', required=True),
      format='%(B2)s)

db.define_table('C',
*# select drop down of all records in A.C1*
      Field('C1', db.A),
*# cascading drop down selecting all records of B.B2 WHERE B.B1=C.C1*
      Field('C2', db.B), 
      Field('C3', 'string', required=True))

default.py

def C():
    rows = db(db.C).select(orderby=~db.C.C1|~db.C.C2).render()
    return locals()

Any help out there on this?

Would be also nice to have the drop-down content of C.C1 and C.C2 ordered by the reder() text of C1 and C2.

Community
  • 1
  • 1
  • How are you generating form? `SQULFORM` or custom html form? If you are using `SQLFORM`, you can use conditional fields, read this [Conditional Fields](http://www.web2py.com/books/default/chapter/29/07/forms-and-validators#Conditional-fields). When you want to show field `C2`?, I didn't get your explanation from comments. – Gaurav Vichare Aug 25 '16 at 04:16
  • Or you can make ajax call after selecting value in `C1` dropdown and decide whether to show `C2` dropdown or not and which values to show. – Gaurav Vichare Aug 25 '16 at 04:25
  • For what I understand from the Conditional field link you sent is that a field is "shown" if another field has a certain value selected. What I'd like to do instead is to have both fields displayed but with the values of the second drop down that depend on what value is selected in the first. Maybe ajax is the way to go but no clue on how to do it. All this in a custom html view. – Joe Carpenter Aug 25 '16 at 04:33
  • This will be helpful for you http://stackoverflow.com/questions/8146260/best-practice-for-populating-dropdown-based-on-other-dropdown-selection-in-web2p – Gaurav Vichare Aug 25 '16 at 04:46

0 Answers0