I have an update SQLFORM for quotation table, i want to make 
all_items_total and quote_amount not writable by user because 
all_items_total should be the sum of all the items belong to this 
quotation, not enter by user. The quote_amount should be all_items_total 
minus discount, so when the user changes the discount value on the SQLFORM, 
jQuery should detect the change and make a callback to return the 
quote_amount, finally update the field with specific id. All those ajax and 
callback worked fine when i had db.quotation.all_items_total.writable=True 
& db.quotation.quote_amount.writable=True. However, when I finally changed 
them to False, first I couldn't pick up the value of all_items_total which 
is highlighted in red below inside the ajax function, but 
the change_discount_callback was called, obviously error was raised because 
all_items_total passed from ajax to callback function was empty. Then i 
tried to add another ajax callback to simply change those 2 field's 
writable to True (temporary), it went through but same issue happened. 
Surely if all the above worked, i still had to update the quote_amount on 
the form display with value returned from callback. I have been stuck for 
1.5 days, any help is much appreciated. Thanks!

db.define_table('quotation',
                Field('customer', 'reference customer'),
                Field('all_items_total', 'double', default=0),
                Field('discount', 'double', default=0, label='Discount %'),
                Field('quote_amount', 'double', default=0),
                auth.signature)

db.define_table('quote_item',
                Field('quotation', 'reference quotation',  writable=False, 
label='Quote Id'),
                Field('product'),
                Field('quantity', 'integer', default=1),
                auth.signature)

def edit_quote():
    db.quotation.all_items_total.writable = False
    db.quotation.quote_amount.writable = False
    quote_id = request.args(0)
    quote_row = db.quotation(quote_id)
    form=SQLFORM(db.quotation, quote_row, labels={'id':'Quote 
Id'}).process()
    return locals()

<script>
jQuery(document).ready(function(){

function do_ajax_change_discount(t, discount, all_items_total){
    jQuery.ajax({method:'post', url:'{{=URL('change_discount_callback')}}',
                 data:{'discount':discount, 
'all_items_total':all_items_total},
                 success: function(data){
                     result = JSON.parse(data)
                    
 jQuery('#quotation_quote_amount').val(result.quote_amount);
                });
}
jQuery('#quotation_discount').change(function(){
    var discount = jQuery('#quotation_discount').val();
    var all_items_total = jQuery('#quotation_all_items_total').val();
    do_ajax_change_discount(this, discount, all_items_total);
    });
});
</script>

def change_discount_callback():
    vars=request.post_vars
    if vars:
        discount = float(vars.discount)
        all_items_total = float(vars.all_items_total)
        quote_amount = all_items_total * (100 - discount)/100
    else:
        print "request.post_vars is None" 
    return simplejson.dumps(dict(quote_amount=quote_amount))

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