SQLFORM widgets and drop-down filtering

97 views
Skip to first unread message

Dave S

unread,
Apr 8, 2018, 3:41:29 AM4/8/18
to web2py-users
I have a table that follows the following structure:

db.define_table('collection',
               
Field('IssueYr', 'integer',
                     requires
= [IS_NOT_EMPTY(), IS_INT_IN_RANGE(1965,2018, "Not a valid year of issue")],
                     widget
= lambda f, v: SQLFORM.widgets.integer.widget(f, v, _autofocus=True)),
               
Field('State', 'string', default=None, represent=lambda state: state or '',
                      requires
=IS_EMPTY_OR(IS_IN_SET(State_list))),
               
Field('Other', 'string', default=None, represent=lambda Other: Other or '',
                      requires
=IS_EMPTY_OR(IS_IN_SET(other_list)))
)


And I use use SQLFORM() to fill out the fields..  "State" is defined for certain issueYrs, for other years a javascript trigger skips to the "Other" field.  This works well, and the State_list dropdown works as expected.  But it turns out that the State_list can be subdivided with 4 or 5 corresponding to a given year.  I do additional validation on the basis of this (to catch typo'd years).  It would be nice to take advantage of that subdivision in the dropdown, only offering the, um, slice of states for the year that has been entered.  For the validation, I have a dictionary where the keys are years and the values are the matching states.  Is there a widget that I can pass the value-list to for use in the dropdown, using the javascript trigger to handle the key lookup?

I looked at the autocomplete widget, and it's tied to a database field, so that's not very convenient for this use-case.  I'm not totally against having essentially static tables when appropriate, but it seems like overkill for this.

/dps


Leonel Câmara

unread,
Apr 9, 2018, 10:17:42 AM4/9/18
to web2py-users
I would just add some javascript to filter the state options when the year changes. There's no widget specifically for this, you could make a custom one, but it would always have to include javascript to do basically the same thing, so it's not worth it to make a custom widget if you will only use it here.

Dave S

unread,
Apr 9, 2018, 3:43:12 PM4/9/18
to web...@googlegroups.com


On Monday, April 9, 2018 at 7:17:42 AM UTC-7, Leonel Câmara wrote:
I would just add some javascript to filter the state options when the year changes. There's no widget specifically for this, you could make a custom one, but it would always have to include javascript to do basically the same thing, so it's not worth it to make a custom widget if you will only use it here.


Okay, that seems reasonable.

Thanks for the comments.

/dps
 
Reply all
Reply to author
Forward
0 new messages