Calculations with Radio buttons

This sample code constructs a calculation formula composed of Number fields and a Radio button field.

Sample Image

calculationswithradiobutton.gif

Sample Code

// Change calculation method by radio button choice

(function() {
    "use strict";

    var X = 'x_field'; //field code of number field used for first part of the equation
    var CAL_METHOD = 'CalMethod'; // field code of radio button field
    var Y = 'y_field'; //field code of number field used for second part of the equation
    var RESULT = 'calc_result'; //field code of number field to set the calculation result

    kintone.events.on(["app.record.create.show","app.record.edit.show"], function(showevent){
        showevent.record[RESULT]['disabled'] = true;
        return showevent;
    })

    kintone.events.on(["app.record.create.submit", "app.record.edit.submit"], function(submitevent) {
        var record = submitevent.record;
        var xValue = Number(record[X].value), yValue = Number(record[Y].value);

        if (record[CAL_METHOD].value === 'plus') {
            record[RESULT].value = xValue + yValue;
        } else if (record[CAL_METHOD].value === 'minus') {
            record[RESULT].value = xValue - yValue;
        } else if (record[CAL_METHOD].value === 'multiplied by') {
            record[RESULT].value = xValue * yValue;
        } else {
            record[RESULT].value = xValue / yValue;
        }
        return submitevent;
    });
})();

Notes

Make sure to match the Field code of your fields, with the Field code written in the code.

 

Refer to the Tutorial page, on how to add scripts to your app.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.