About OPTGROUP, here is an example.

CHINA={
  'CAN':'Guangzhou',
  'SHA':'Shanghai',
  'PEK':'Beijing(Peking)',}
INTERNATIONAL = {
  'SYD':'Sydney',
  'BKK':'Bankok',
  }
AIRPORTS = dict(CHINA.items() + INTERNATIONAL.items())

db.define_table('booking',
    Field('departure', requires=IS_IN_SET(AIRPORTS)),
    ...,
    )

# so far, nothing is new. Then...

from gluon.sqlhtml import OptionsWidget
class GroupOptionsWidget(OptionsWidget):
    def __init__(self, groups):
        self.groups = groups
    def widget(self, field, value, **attributes):
        attr = OptionsWidget._attributes(field, {'value':value},
**attributes)
        opts = [OPTION('')] + [ OPTGROUP( # OPTGROUP(...) is available
since web2py 1.77.4
            _label=group, *[OPTION(v, _value=k) for (k, v) in
options.items()])
                for group, options in self.groups.items() ]
        return SELECT(*opts, **attr)

db.booking.departure.widget = GroupOptionsWidget({'China':CHINA,
'International':INTERNATIONAL}).widget

# That is it.

# I know it is not cool that we need to define our own
GroupOptionsWidget every time. But if there is enough interest, I
guess we can do more to enhance the already-too-complicated
IS_IN_SET(...), to support following pattern:

    Field('departure',
        IS_IN_SET( {'China':CHINA, 'International':INTERNATIONAL} ) )

I mean, a dict of dicts should automatically trigger a predefined
GroupOptionsWidget.

Regards,
Iceberg

On May16, 2:10pm, Jason Brower <encomp...@gmail.com> wrote:
> Oh fun.  But waht OPTGROUP, and block name/end stuff do? How do I use
> it?
> Best Regards,
> Jason
>
>
>
> On Sat, 2010-05-15 at 22:14 -0700, mdipierro wrote:
> > Please check it
>
> > changelog
> > - new template system allows {{block name}}{{end}}, thanks Thadeus
> > - fixed mime headers in emails, included PGP in emails, thanks Gyuris
> > - automatic database retry connect when pooling and lost connections
> > - OPTGROUP helper, thanks Iceberg
> > - web2py_ajax_trap captures all form submissions, thank you Skiros
> > - multicolumn checkwidget and arbitrary chars in multiple is_in_set,
> > thanks hy
> > - Québécois for welcome, thanks Chris
> > - crud.search(), thanks Mr Freeze
> > - DAL(...migrate,fake_migrate), thanks Thadeus

Reply via email to