Search This Blog

30 September 2010

using CascadingSelection with tw.dynforms

Here I took some note on my usage of the CascadingSingleSelectField from tw.dynforms

It is not that well documented, so I took an extract of code.



class MailForm(TableForm):

...
    CascadingSingleSelectField(
        # this will be used to id the widget
        'region',

        label_text='Region',
        help_text = 'Please choose the region.

        # what controller to call onchange
        cascadeurl = '/choices/region',
        # this is the default js event, but can be overriden
        event = 'onchange',
        # id of other widgets if you need their value to compute your choices
        extra = ['gender', 'senior'],   
        size = 10,
        ),
    Spacer(),
    CascadingSingleSelectField('people',
        label_text='People',
        help_text = 'Please select a person.',
        cascadeurl = '/choices/people',
        ),
    Spacer(),
    StringField('mailto',
        label_text='To:',
        ),
...

in the controller let's add some methods

@expose('json')
def region(self, **kw):
    """ called by the region widget
    """
    # this is the value of the "gender" widget
    gender = kw.get("gender")
    # same for senior, they were defined in the extra attribute
    senior = kw.get("senior")
    # "value" is the value of our widget (region here)
    region = kw.get("value")

    # here we suppose that the _get_people method returns a list of tuples
    # (value, display). it will result in display
    people = self._get_people(gender, senior, region)

    # the keys of the resulting dict will be used to determine
    # what widget should be changed there could be many of those 
    return dict(people=people)

Further readings :

you might want to read this tw.dynforms tutorial

or the official documentation

No comments:

Post a Comment