>
> Well, that's pretty impressive... mate.
>
> ;)
>
> Thanks a lot. Will surely try it.
>

I have updated the recipe because it had some details not solved (i.e. the 
forms do not process the input and widgets without filter with default 
values). Here's the new version. I plan to post this in web2pyslices when 
they enable posting again (spam problems).

model:

db.define_table("department", Field("name"), format="%(name)s")
db.define_table("myrank", Field("name"), Field("department_id", "reference 
department"), format="%(name)s")
db.define_table("seafaring", Field("name"), Field("myrank_id", "reference 
myrank", label="Rank"), Field("department_id", "reference department", label
="Department"), format="%(name)s")

# dummy records creation
data = {"kitchen": ["cook", "chef", "assistant"], "deck": ["boatswain", 
"maintopman", "sailmaker"]}

if db(db.department).count() <= 0:
    for k, l in data.iteritems():
        i = db.department.insert(name=k)
        for v in l:
            db.myrank.insert(department_id=i, name=v)

def department_widget(field, value):
    widget = SQLFORM.widgets.options.widget(field, value)
    widget.attributes["_onchange"] = 
'updateMyRanks(jQuery("[name=department_id]").val());'
    return widget

def myrank_widget(field, value):
    _id="%s_%s" % (field._tablename, field.name)
    if value:
        department_id = db.myrank[value].department_id
        # filter the ranks when there's an actual value recorded
        field.requires = IS_IN_DB(db(db.myrank.department_id==department_id
), 'myrank.id', "%(name)s")
    elif not request.function == "myrank":
        # create an empty select if no rank was selected
        # unless there's an options request
        field.requires = IS_IN_SET([])
    script = SCRIPT("""
    function updateMyRanks(departmentId){
        // remove the ye olde options
        jQuery("[name=%(fieldname)s]").html("");
        // fish the new ones
        web2py_component("%(url)s/myrank/" + departmentId, "%(_id)s");
    }
    """ % dict(fieldname=field.name, _id=_id,
               url=URL(c="default", f="myrank", extension="load")))
    return DIV(script, SQLFORM.widgets.options.widget(field, value))

db.seafaring.department_id.widget = department_widget
db.seafaring.myrank_id.widget = myrank_widget


controller (default.py)

def index():
    # the seafaring men census form
    form = SQLFORM(db.seafaring)
    if form.process().accepted:
        response.flash = T("Okay!")
    return dict(form=form)

def edit():
    form = SQLFORM(db.seafaring, 1)
    if form.process().accepted:
        response.flash = T("Okay too")
    return dict(form=form)

def myrank():
    """ This function returns the rank options for dynamic widget update"""
    # write the field custom requisites
    db.seafaring.myrank_id.requires = IS_IN_DB(db(db.myrank.department_id==
request.args(1)), 'myrank.id', "%(name)s")
    # create a dummy form to extract the widget
    form = SQLFORM(db.seafaring)
    # here a bit of black magic to return the correct data when loaded as 
component
    text =  "".join([t.xml() for t in form.custom.widget.myrank_id.elements(
"option")])
    return text


view (default/index.html)

{{left_sidebar_enabled,right_sidebar_enabled=False,('message' in globals
())}}
{{extend 'layout.html'}}
{{=BEAUTIFY(response._vars)}}




-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to