pythonjqueryweb2py

web2py: detect change in dropdown widget


How can we detect a change from a dropdown widget so that code can be executed?

Table loan references table services. Both have field called interest_rate. When I change loan.service via dropdown I'd like to reflect the corresponding interest_rate from services table to loan table.

How can this be achieved?

model

db.define_table('services',
                Field('service_name',requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'services.service_name')]),
                Field('service_type','reference service_types',requires=IS_IN_DB(db,db.service_types.id,
                                                                                '%(type_name)s',
                                                                                error_message='not in table',
                                                                                zero=None),
                                                                                ondelete='RESTRICT',
                                                                                ),
                Field('interest_rate','decimal(15,2)',requires=IS_DECIMAL_IN_RANGE(0,100)),
                Field('max_term','integer'),
                auth.signature,
                format='%(service_name)s',
    )

db.define_table('loan',
    Field('service','reference services',requires=IS_IN_DB(db,db.services.id,
                                            '%(service_name)s',
                                            error_message='not in table',
                                            zero=None),
                                            ondelete='RESTRICT',),
    Field('member_id','reference members',requires=IS_IN_DB(db,db.members.id,
                                            '%(member_name)s',
                                            error_message='not in table',
                                            zero=None),
                                            ondelete='RESTRICT',
                                            label='Member'),
    Field('amount','decimal(15,2)',requires=IS_DECIMAL_IN_RANGE(1000)),
    Field('interest_rate','decimal(6,2)',default=10),
    )   

Solution

  • The material process is to monitor onchange with the change() event, something like this:

    <script src="{{=URL('static', 'js/utils.js')}}"></script>
    <script>
    
    {{=ASSIGNJS(jslast_nos = last_nos)}}
    {{=ASSIGNJS(jsaction = action)}}
    jQuery(document).ready(function(){
        if (jsaction == "new") {
            jQuery("#AAP_doc_number").val(jslast_nos[jQuery("#AAP_pos_id").val()]);
        };
        jQuery("#AAP_pos_id").change(function(){
            pos_id = jQuery('#AAP_pos_id').val();
            jQuery("#AAP_doc_number").val(jslast_nos[pos_id]);
        });
        jQuery('#AAP_bags').change(function(){
            jQuery("#AAP_net_kg_qty").val(Number(jQuery('#AAP_bags').val() * 50));
            jQuery('#AAP_net_kg_qty').change();
        });
        jQuery('#AAP_net_kg_qty, #AAP_selling_price').change(function(){
            n = Number(jQuery('#AAP_net_kg_qty').val()) * Number(jQuery('#AAP_selling_price').val())
            jQuery('#AAP_amount').val(evenRound(Number(n),2).toFixed(2))
        });
    });
    </script>