Here is an example of what I'm trying to do...
Don't forget to install the plugins....

*IN CONTROLLER*

def index():
    form = SQLFORM(db.product)
    if form.accepts(request.vars, session):
        session.flash = 'submitted %s' % form.vars
        redirect(URL('index'))
    return dict(form=form,
                categories=SQLTABLE(db().select(db.category.ALL)),
                colors=SQLTABLE(db(db.color.id > 0)(db.color.category == 
db.category.id
                                ).select(db.color.id, db.category.name, 
db.color.name)))

*IN MODEL*

from plugin_lazy_options_widget import lazy_options_widget
from plugin_suggest_widget import suggest_widget

db = DAL('sqlite:memory:')
db.define_table('category', Field('name'))
db.define_table('color', Field('category', db.category), Field('name'))
db.define_table('hue', Field('color', db.color), Field('name'))
db.define_table('product',
    Field('category', db.category, comment='<- type "A" or "B"'),
    Field('color', db.color,
          requires=IS_EMPTY_OR(IS_IN_DB(db(db.color.id > 0), 'color.id', 
'color.name', zero='---')),
          comment='<- select category first'),
    Field('hue', db.hue,
          requires=IS_EMPTY_OR(IS_IN_DB(db(db.hue.id > 0), 'hue.id', 
'hue.name', zero='---')),
          comment='<- select color first'),)

db.category.bulk_insert([{'name':'A'}, {'name':'B'}])

for category in db(db.category.id > 0).select():
    _id = category.id
    if category.name == 'A':
        db.color.bulk_insert([{'category': _id, 'name':'red'}, {'category': 
_id, 'name':'blue'}])
    elif category.name == 'B':
        db.color.bulk_insert([{'category': _id, 'name':'green'}])
for color in db(db.color.id > 0).select():
    _id = color.id
    if color.name == 'red':
        db.hue.bulk_insert([{'color': _id, 'name' : 'shiny'},{'color': _id, 
'name': 'dull'}])
    elif color.name == 'blue':
        db.hue.bulk_insert([{'color': _id, 'name' : 'glossy'},{'color': 
_id, 'name': 'dark'}])
    elif color.name == 'green':
        db.hue.bulk_insert([{'color': _id, 'name' : 'opaque'},{'color': 
_id, 'name': 'light'}])

db.product.category.widget = suggest_widget(db.category.name, 
id_field=db.category.id,
                                          limitby=(0, 10), min_length=1)

################################ The core 
######################################
# The lazy_options_widget receives js events
# called "product_category__selected" and "product_category__unselected"
# which will be triggered by the above suggest_widget.]
# You can also pass user_signature and hmac_key arguments for authorization 
in ajax
db.product.color.widget = lazy_options_widget(
                  'product_category__selected', 
'product_category__unselected',
                  lambda category_id: (db.color.category == category_id),
                  request.vars.category,
                  orderby=db.color.id,
                  # If you want to process ajax requests at the time of the 
object construction (not at the form rendered),
                  # specify your target field in the following:
                  field=db.product.color,
                  )
################################################################################
db.product.hue.widget = lazy_options_widget(
                  'product_color__selected', 'product_color__unselected',
                  lambda color_id: (db.hue.color == color_id),
                  request.vars.color,
                  orderby=db.hue.id,
                  # If you want to process ajax requests at the time of the 
object construction (not at the form rendered),
                  # specify your target field in the following:
                  field=db.product.hue,
                  )




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to