I haven't looked at this in detail, but if you want to add an extra field 
to a form, see 
http://web2py.com/books/default/chapter/29/07#Adding-extra-form-elements-to-SQLFORM
 (you 
can also use the DOM manipulation methods described here: 
http://web2py.com/books/default/chapter/29/05#Server-side-DOM-and-parsing).

If you need to load options in a select dynamically based on other 
selections in the form, that gets more complicated. For some solutions, see 
here<http://stackoverflow.com/questions/8146260/best-practice-for-populating-dropdown-based-on-other-dropdown-selection-in-web2p/8152910#8152910>
.

Anthony

On Thursday, December 6, 2012 4:02:37 AM UTC-5, software.ted wrote:
>
> Apologies for my ignorance but I want to add a field [list of items] to a 
> SQLFORM created from a table that does not have that field. So to add the 
> field I am using Field.Virtual as follows:
>
> db.staff_time_record.departments = Field.Virtual(db.department)
>
> But this does not seem to allow me to create a virtual field with a 
> reference of all departments in the table as a list. Then what I want to do 
> if this works or is possible, use on the change of the drop down list of 
> departments field on the form to list only the offices in the selected 
> department using jQuery in the view as follows:
>
> jQuery('#department').on(function(){
>      ajax('update_office',['departments'], 'staff_time_record_office_id');
>
> My worry here again even if I have not tested this yet is, when the page 
> loads first time, it will load all Offices cause of the db defaults. 
>
> Please correct me if am way out of line in the normal way of doing things 
> in web2py....I have added various components of application. 
>
> Model:
>
> <code>
> import datetime
> now = datetime.datetime.today()
> db.define_table("department", 
>     Field("name"), 
>     format="%(name)s"
> )
> db.define_table("office", 
>     Field("name"), 
>     Field("department_id", 
>     db.department, 
>     requires=IS_IN_DB(db, 'department.id', '%(name)s')), 
>     format="%(name)s"
> )
> db.define_table("staff_time_record", 
>     Field("time_in","datetime", default=now), 
>     Field("office_id", db.office, requires=IS_IN_DB(db,'office.id
> ','%(name)s'))
> )
>
> </code>
>
> Controller:
>
> <code>
> def index():
>     db.staff_time_record.departments = Field.Virtual(db.department)
>     form = SQLFORM(db.staff_time_record)
>     if form.process().accepted:
>         response.flash("Time Card for the offer recorded")
>     elif form.errors:
>         response.flash("There was an error recording the employee record")
>     return dict(form=form)
> def update_office():
>     return [OPTION(o.name, _value=o.id) for o in 
> db(db.office.department_id == request.vars.departments)]
> </code>
>
> The View:
>
> <code>
>
> {{extend 'layout.html'}}
> <h3>Please record time officer reported for work</h3>
> {{=form.custom.start}}
> <table>
> <tr>
>         <td>Officer Department:</td>
>         <td>{{=form.custom.widget.departments}}</td>
>     </tr>
>     <tr>
>         <td>Officer title:</td>
>         <td>{{=form.custom.widget.office_id}}</td>
>     </tr>
>     <tr>
>         <td>Time Reported: </td>
>     <td>{{=form.custom.widget.time_in}}</td>
>     </tr>
>     <tr>
>         <td></td>
>         <td>{{=form.custom.submit}}</td>
>     </tr>
> </table>
> {{=form.custom.end}}
>
> <script>
>     jQuery('#department').on(function(){
>     ajax('update_office',['departments'], 'staff_time_record_office_id');
>     }
> </script>
>
> </code>
>
>
> I think if I get this and understand whats really going on will really 
> want us to be part of web2py conf 2013!!!
>
> Ideas to clarify the gray areas above will be appreciated.
>
>
>
> -- 
>
> .......................................................................................
> Teddy Lubasi Nyambe
> Opensource Zambia
> Lusaka, ZAMBIA
>
> Cell: +260 97 7760473
> website: http://www.opensource.org.zm
>
> ~/
> Human Knowledge belongs to the world! - AntiTrust
>
> Man is a tool-using animal. Without tools he is nothing, with tools he is 
> all - Thomas Carlyle 1795-1881
>
> /~
>  

-- 



Reply via email to