You are right. I planned to use validator for the "initial" state and then 
Ajax for "online changes", but it was too messy, so finally i'm using just 
Ajax.

Thanks for the info links but I've written my own solution. Here it is, 
just in case it helps anyone:

Moved db definition code to a model, and add this after grid definition:

    if 'edit' in request.args:
        # Ajax department_id -> teacher_id
        # 1.- Initial filter
        form += SCRIPT(''' jQuery(document).ready(function(){
               ajax('%s', ['department_id', 'teacher_id'], 'pupil_teacher_id
');
            }); ''' % URL('options_teacher'))
        # 2.- Ajax modification setted by DOM
        department_id = form.element('select #pupil_department_id')
        department_id['_onchange'] = "ajax('%s', ['department_id'], 
'pupil_teacher_id')" % URL('options_teacher')

Here the function that returns the filtered OPTIONs:

def options_teacher():
    T = db.teacher
    rs = db(T.department_id == request.vars.department_id).select(T.ALL, 
orderby=T.name)
    teachers = [OPTION(r.name, _value=r.id, _selected=bool(r.id == int(
request.vars.teacher_id or 0))) for r in rs]
    return CAT(OPTION(T('Choose One'), _value=''), *teachers)


Optional additional check can be introduce adding to grid definition:
onvalidation=lambda form: my_onvalidation(form)

definig previously:
    def my_onvalidation(form):
        if form.vars.department_id and form.vars.teacher_id \
        and db.teacher[form.vars.teacher_id].department_id != int(form.vars.
department_id):
            form.errors.teacher_id = T('Error')

Regards!


El sábado, 1 de agosto de 2015, 16:14:59 (UTC+2), Anthony escribió:
>
> If you think about it, this is not logically possible -- how can you set 
> values for the teacher dropdown that depend on the selected department 
> *before* the department has even been selected? Instead, you have to 
> handle this via Javascript -- once the user selects the department, then 
> you populate the list of teachers. There is no built-in support for that 
> functionality, but for some ideas, see 
> http://stackoverflow.com/questions/8146260/best-practice-for-populating-dropdown-based-on-other-dropdown-selection-in-web2p/8152910#8152910
> .
>
> Anthony
>
>

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