ajax need to return 4 values

75 views
Skip to first unread message

lucas

unread,
Nov 14, 2015, 7:50:56 AM11/14/15
to web2py-users
hello one and all,

how do i run an ajax function under an onchange of a select where the return 4 values will be going to 4 separate other inputs of type text, select, or textarea?

thank you in advance, lucas

Paolo Valleri

unread,
Nov 14, 2015, 8:24:06 AM11/14/15
to web2py-users
There isn't a solution out-of-the-box in web2py.
I'd suggest returning a json with the 4 values, and then apply the result to the input fields

Paolo

Anthony

unread,
Nov 14, 2015, 12:40:22 PM11/14/15
to web2py-users
The third argument to ajax() can be the id of a target element, but it can also be ":eval" or a Javascript function. If you use ":eval", you can return any Javascript code, and it will be eval'ed -- so just generate the code necessary to update the four inputs (e.g., "$('#input1').val(value1); $('#input2').val(value2); ...").

Alternatively, you can specify a JS function that takes JSON and return the four values as JSON. Then let the function update the inputs.

Anthony

lucas

unread,
Nov 15, 2015, 8:54:24 AM11/15/15
to web2py-users
ok, i went with qQuery.getJSON.  here is how i did it and it works great.  JSON trading is cool.  just for sharing:

under the default controller's function, i have a section where i implement the javascript/jQuery code via an onchange of a select input.  the select input is meant to prepopulate with some options and if the user chooses an option then it grabs the values via the jQuery.getJSON from a separate function (below) under default:

            #bos...template
            tTGroup = db.task_group
            tTTemplate = db.task_template
            jq_template = """jQuery.getJSON('%s/ajaxTemplate', { template_id: jQuery('#task_template').val() }).done(
function(t) {
jQuery('#task_task').val(t.task);
jQuery('#task_task_group_id').val(t.task_group_id);
jQuery('#task_recurring').val(t.recurring);
jQuery('#task_time_spent_required').prop('checked', t.time_spent_required);
jQuery('#task_directive_notes').html(t.directive_notes);
});
""" % ('/cs/default')
            qTemplate = db((tTTemplate.id>0) & (tTTemplate.task_group_id==tTGroup.id)).select(orderby=tTTemplate.task)
            s = SELECT(OPTION("", _value=""), _id='task_template', _class='generic-widget', _name='template_ID', _onchange=XML(jq_template))
            for t in qTemplate:
                s.append(OPTION("%s (%s)" % (t.task_template.task, t.task_group.task_group), _value="%i" % t.task_template.id))
            frm[0].insert(1, TR(TD(LABEL("Task Template:", _id='task_template__label'), _class='w2p_fl'), TD(s, _class='w2p_fw'), TD("Choosing will overwrite certain fields.", _class='w2p_fc'), _id='task_template__row'))
            #eos...template

under controller default:

def ajaxTemplate():
    id = int(request.vars.template_id)
    return response.json(db(tTTemplate.id==id).select().first())

Reply all
Reply to author
Forward
0 new messages