Massimo,

Thanks for that, I was about to reply saying that JQuery can't do what I
want it to, then thought I'd test my statement before making it. Here's the
code for anyone who's interested:

Its all in the controller, as I pretty much use only one single
multi-purpose view for all my pages :-)

####

    #Fields to be visible for all departments
    commonFields = [
        'Details',
        'Suggested_Action'
    ]
    form = SQLFORM.factory(db[TableName])

    #Create dict of department-specific fields, this is where you make
changes when departments change the fields they require....
    #Note: must all exist in table def in model, exclude common fields in
list above.
    DepartmentFields = dict()
    DepartmentFields['IT'] = ['Contract_Number', 'Details']
    DepartmentFields['Projects'] = ['Work_Order_Number',
'Sales_Order_Number', 'Client']

    #Create javascript/JQuery string for department select's onChange
event...
    deptchange = "$(\"TR[id^=no_table]\").hide();" +\
        "var selectedDept = $('#To_Department :selected').text();" +\
        "var fieldsToShow = [];" +\
        "switch(selectedDept){"
    for dept, fields in DepartmentFields.items():
        fields.extend(commonFields )
        deptchange += "case '%s': fieldsToShow=%s;break;" % (dept, fields )
    deptchange += "default: fieldsToShow=[];};for (f in
fieldsToShow){$('#no_table_' + fieldsToShow[f] + '__row').show();};"

    #Create the department select html control and insert at the top of the
form (place this wrong and you will get very strange results when hiding
parts of the form)
    DepartmentsRows = db(db.department.id >0 ).select()
    Departments = [DepartmentsRows[i]['department_name'] for i in
range(len(DepartmentsRows))]
    form[0].insert(0, TR(TD('To Department: '),TD(
                SELECT( Departments , _name='To_Department',
_id='To_Department', _onChange=deptchange ),
            )))

I agree it's not ideal storing part of the logic in python, and part in
JQuery, but at least this way the dict representing the variable data can be
kept in python (we do this for the DAL so why not...).

I would still like to know the answer to me first question though: can I
create INPUT/SELECT/TEXTAREA bits directly from the table definition field
without going via SQLFORM? Anyone?

-------------------


On Mon, Jun 21, 2010 at 5:35 PM, mdipierro <[email protected]> wrote:

> This is what I would do
>
> - make a single for that contains all fields you need
> - use jQuery in the view so that depending on the selected department
> some fields are hidden
>
>
>
> On Jun 21, 3:38 am, Andrew Buchan <[email protected]> wrote:
> > Hello,
> >
> > I am trying to make a form for inserting a new record representing an
> > internal complaint against a given department. Depending on which
> department
> > is selected, different fields must be completed.
> > The way I'm planning on doing this is with a simple form with a select
> box
> > for the department, a DIV, and a submit button. On changing the
> department
> > selection, the DIV will be refreshed to display only those fields
> applicable
> > to that department (all of which correspond to a field in the DAL).
> >
> > 1. Where I'm having trouble is in generating the HTML fields based on the
> > names of a field in the DAL. I have previously always just used SQLForm
> and
> > sized it down to suit by passing a list of fields, but think that in this
> > situation, and many others, it would be useful to build a form from
> fields
> > obtained by passing the name of a field in the DAL and getting the
> > validators brought through automatically.
> >
> > 2. Also, if I manage to do this, will there be an issue with the fact
> that
> > some of the fields in the form are nested in a DIV, will the fields still
> > auto-validate? From recollection placing a whole form within a DIV
> doesn't
> > work so didn't want to do that...
> >
> > Cheers..
>

Reply via email to