plugin_lazy_options widget

79 views
Skip to first unread message

Jesse Ferguson

unread,
Aug 7, 2014, 7:25:54 PM8/7/14
to web...@googlegroups.com
Can you use the plugin_lazy_options_widget on itself?  For example,

db.service_order.so_company.widget = suggest_widget(db.company.company_name, id_field=db.company.id, limitby=(0, 10), min_length=1)

"""Below Works Just fine"""

db.service_order.so_location.widget = lazy_options_widget(
                  'service_order_so_company__selected', 'service_order_so_company__unselected',
                  lambda so_company_id: (db.g_location.company == so_company_id),
                  request.vars.so_company,
                  orderby=db.g_location.id,
                  field=db.service_order.so_location,
                  )
"""This Widget is not working, It should determine its select options from the above widgets field"""

db.service_order.so_lease.widget = lazy_options_widget(
                  'service_order_so_location__selected', 'service_order_so_location__unselected',
                  lambda so_location_id: (db.lease.lease_location == so_location_id),
                  request.vars.so_lease,
                  orderby=db.lease.id,
                  field=db.service_order.so_lease,
                  )

I believe the problem is the first two arguments to lazy_options_widget are not present on itself, only present on suggest_widget fields.... How can I get this work? I tried adding some jquery to add the fields to the lazy_options_widget, but my java skills are horrible...

Jesse Ferguson

unread,
Aug 7, 2014, 7:45:45 PM8/7/14
to
The first two args for the widget seem to reference the fields HTML class "suggest_selected" in the source of the plugin they are referred to as on_key and off_key. There is also a trigger option however I cant figure out what that does...

Jesse Ferguson

unread,
Aug 13, 2014, 12:51:08 AM8/13/14
to web...@googlegroups.com
I've tried everything, I'm sure its just something easy I'm missing or its possibly just some needed javascript to trigger the ajax call. Anyone out there wanna take a look, maybe give me a hint?  http://dev.s-cubism.com/plugin_lazy_options_widget

Jesse Ferguson

unread,
Aug 13, 2014, 1:17:09 AM8/13/14
to web...@googlegroups.com
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,
                  )




Jesse Ferguson

unread,
Aug 14, 2014, 5:33:04 PM8/14/14
to web...@googlegroups.com
OK so I could never figure out how to use this widget on itself, I ruptured a few brain cells in the process and just decided to learn jquery. In case anyone is ever in my position here is my working solution....

1)scrap the second lazy_options_widget and just use the default web2py select box.

2) create a controller that will return a db lookup in json ex.

def lookup():
    response.view = 'generic.json'
    query = request.vars.query
    data = db(db.tablenamehere.fieldnamehere == query).select() or "none"
    return data.as_dict()


This will take an variable (the id of the selected item) and return the options you will plug into your select box

3)Add some JS to the view

 <script>
var selectList = $("#IDOFYOURFIELD");

$('#TABLE_FIELD__display').change(function(){
    value = $("#TABLE_FIELD__hidden").val();
    $.getJSON("/app/defualt/lookup/",{ query: value }, function(response){
        var responseList="";
        $.each(response, function(index, item) {
            responseList +="<option value=" + item.id + ">" + item.name + "</option>";
        });
    leaseList.html(responseList);
    });
});

</script>

4) Pray it works!
Reply all
Reply to author
Forward
0 new messages