ajax callback onchange for checkbox boolean

72 views
Skip to first unread message

黄祥

unread,
Feb 1, 2016, 3:46:38 AM2/1/16
to web2py-users
just wondering about ajax callback onchange for checkbox boolean 
e.g.
modules
if current.request.vars.action == 'adjust_factory':
#factory = current.request.vars['factory_%s' % id]
factory = current.request.vars.factory
if factory == 'on':
factory = 'None'
session_order_product[id] = factory
elif factory == 'None':
factory = 'on'
session_order_product[id] = factory

views
{{if factory == 'on':}}
<form>
<input type="checkbox" name="{{='factory_%s' % id}}" value="{{=factory}}" checked
onchange="ajax('{{=URL(link_callback, vars = dict(id = id, factory = factory, 
action = 'adjust_factory') ) }}', 
['{{='factory_%s' % id}}'], ':eval' )" />
</form>
{{else:}}
<form>
<input type="checkbox" name="{{='factory_%s' % id}}" value="{{=factory}}"
onchange="ajax('{{=URL(link_callback, vars = dict(id = id, factory = factory, 
action = 'adjust_factory') ) }}', 
['{{='factory_%s' % id}}'], ':eval' )" />
</form>
{{pass}}

why the bold part of code in modules is not work (the commented one), and the uncommented part is work ?
any idea the reason for this?

thanks and best regards,
stifan

黄祥

unread,
Feb 6, 2016, 6:03:32 AM2/6/16
to web2py-users
why when using 2 checkbox, the latest change or click is applied, the previous one is ignore to store in the session value? 
let say, i have two unchecked checkbox name : bevel and gosok_halus, when i click for all checkbox, first : bevel and then second click : gosok_halus, the value that been updated in session is gosok_halus is on the session storage, yet the bevel is remain unchecked after i refresh the page.
already tried onclick instead of onchange but got the same result, tried to remove ':eval' in ajax callback but got the same result. 
any idea how to make ajax callback with more than one checkbox in web2py way?
e,g,
controllers
def callback_4(session_order):
counter = int(request.vars.counter)
if request.vars.action == 'adjust_bevel':
id = int(request.vars.id)
quantity = int(request.vars['quantity_%s' % counter] )
bevel = request.vars.bevel
gosok_halus = request.vars.gosok_halus
if bevel == 'on':
bevel = ''
session_order[counter] = id, quantity, bevel, gosok_halus
elif bevel == '':
bevel = 'on'
session_order[counter] = id, quantity, bevel, gosok_halus
if request.vars.action == 'adjust_gosok_halus':
id = int(request.vars.id)
quantity = int(request.vars['quantity_%s' % counter] )
bevel = request.vars.bevel
gosok_halus = request.vars.gosok_halus
if gosok_halus == 'on':
gosok_halus = ''
session_order[counter] = id, quantity, bevel, gosok_halus
elif gosok_halus == '':
gosok_halus = 'on'
session_order[counter] = id, quantity, bevel, gosok_halus

views
        <td>
            {{if bevel == 'on':}}
            <form>
                <input type="checkbox" name="{{='bevel_%s' % counter}}" value="{{=bevel}}" checked
                onchange="ajax('{{=URL(link_callback, vars = dict(counter = counter, id = id, 
                bevel = bevel, gosok_halus = gosok_halus, action = 'adjust_bevel') ) }}', 
                ['{{='quantity_%s' % counter}}'], ':eval' )" />
            </form>
            {{else:}}
            <form>
                <input type="checkbox" name="{{='bevel_%s' % counter}}" value="{{=bevel}}"
                onchange="ajax('{{=URL(link_callback, vars = dict(counter = counter, id = id, 
                bevel = bevel, gosok_halus = gosok_halus, action = 'adjust_bevel') ) }}', 
                ['{{='quantity_%s' % counter}}'], ':eval' )" />
            </form>
            {{pass}}
        </td>
        <td>
            {{if gosok_halus == 'on':}}
            <form>
                <input type="checkbox" name="{{='gosok_halus_%s' % counter}}" value="{{=gosok_halus}}" checked
                onchange="ajax('{{=URL(link_callback, vars = dict(counter = counter, id = id, 
                bevel = bevel, gosok_halus = gosok_halus, action = 'adjust_gosok_halus') ) }}', 
                ['{{='quantity_%s' % counter}}'], ':eval' )" />
            </form>
            {{else:}}
            <form>
                <input type="checkbox" name="{{='gosok_halus_%s' % counter}}" value="{{=gosok_halus}}"
                onchange="ajax('{{=URL(link_callback, vars = dict(counter = counter, id = id, 
                bevel = bevel, gosok_halus = gosok_halus, action = 'adjust_gosok_halus') ) }}', 
                ['{{='quantity_%s' % counter}}'], ':eval' )" />
            </form>
            {{pass}}
        </td>

黄祥

unread,
Feb 7, 2016, 4:08:53 AM2/7/16
to web2py-users
found the solution with redirect(current.request.env.http_web2py_component_location, client_side = True), never thought that more than one check box must reload the whole page to ensure the value is store in the session, it's so different with the form field text input type, doesn't matter how much form field text input type, it can triggered the ajax callback and store and retrieve it on the session
e.g.
def callback_4(session_order):
counter = int(current.request.vars.counter)
if current.request.vars.action == 'adjust_bevel':
quantity = int(current.request.vars['quantity_%s' % counter] )
bevel = current.request.vars.bevel
gosok_halus = current.request.vars.gosok_halus
if bevel == 'on':
bevel = ''
session_order[counter] = id, quantity, bevel, gosok_halus
redirect(current.request.env.http_web2py_component_location, client_side = True)
elif bevel == '':
bevel = 'on'
session_order[counter] = id, quantity, bevel, gosok_halus
redirect(current.request.env.http_web2py_component_location, client_side = True)
if current.request.vars.action == 'adjust_gosok_halus':
quantity = int(current.request.vars['quantity_%s' % counter] )
bevel = current.request.vars.bevel
gosok_halus = current.request.vars.gosok_halus
if gosok_halus == 'on':
gosok_halus = ''
session_order[counter] = id, quantity, bevel, gosok_halus
redirect(current.request.env.http_web2py_component_location, client_side = True)
elif gosok_halus == '':
gosok_halus = 'on'
session_order[counter] = id, quantity, bevel, gosok_halus
redirect(current.request.env.http_web2py_component_location, client_side = True)

best regards,
stifan
Reply all
Reply to author
Forward
0 new messages