Well, that's pretty impressive... mate.

;)

Thanks a lot. Will surely try it.

On Sunday, June 23, 2013 7:53:29 PM UTC+2, Alan Etkin wrote:
>
> Ahoy there. I have just bought this recipe from a seafaring man with one 
> leg that might solve this matter. I hope you don't find it fishy:
>
> model
>
> # Pretty close to your model aye?
> 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)
>     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
>
>
>
> This is the controller
>
> def index():
>     # the seafaring men census form
>     form = SQLFORM(db.seafaring)
>     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
>
> For the index.html shipmate census form you only need a view like
>
> {{extend "layout.html"}}
> {{=form}}
>
> Now, if you go a-rowing to <app>/default/index.html and select a 
> department, the options set should should change to those of the 
> appropriate department.
>

-- 

--- 
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