We help IT Professionals succeed at work.

Kintone JSedit

tyler Moore
tyler Moore asked
on
I am using Kintone  JSedit (Java Script)? I am working on a basic application but my native language is not Java so I'm really at a disadvantage here and resources are thin.

I need to calculate a total using data entered in my form.  

Tyler
Comment
Watch Question

tyler MooreIT services

Author

Commented:
So basically to add more context, in Kintone I can embed JavaScript into it to complete functions. So what I've done is create this GUI, where you can input numbers into the Unit Price and Amount(qty). My desire is that through my JS it will automatically calculate the Total and display it in the box below. This is a over simplified example of what I intend to use it for but still learning JS as that's not my normal go to language.

2020-01-30-11_04_00-Test_APP---10---.png
Here is my code:
(function() {
    "use strict";
    var NUMBER = 'mynumber'; //field code of number field
    var ZEROFILL = 'zerofilled_number'; //field code of text field to set result into
    var NUMOFDIGITS = 8 //number of digits for the zero-filled number

    function zeroFill(value, length) {
        if (value.length >= length) { return value; }
        return (new Array(length).join('0') + value).slice(-length);
    }
 
    var events = ["app.record.create.change." + NUMBER, 'app.record.edit.change.' + NUMBER];
 
    kintone.events.on(events, function(event) {
        var record = event.record;
        var changes = event.changes.field;
        var length = NUMOFDIGITS;
        record[ZEROFILL].value = zeroFill(changes.value, length);
        return event;
    });
    // Events triggered while the record is in edit mode
    kintone.events.on(['app.record.edit.show', 'app.record.create.show', 'app.record.index.edit.show'], function (event) {
        // Restricting the input of the "Total" field
        event.record['Total'].disabled = true;

        return event;
    });
})();

Open in new window

Hi,

 for calculation you should no rely on Javascript you better use server side code like PHP.

Javascript can be bypassed easily using the browser console (right-click inspect)
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:

Did the above answer your question?  


While it is true a calculation is typically not to be relied on it is needed for the user.


For a ecommerce site / shopping cart you do in fact calculate totals for the user with javascript as that will give a quick visual answer without refreshing the page. In theory you could use an ajax request but more than likely that is not needed.


However, on the back end when you are going to send a payment amount to the processor, you only send the item and quantity. Then your back end code looks up the price and does any calculation including any final shipping if that is needed. 


In short, do you still need help with the JS calculation?  

tyler MooreIT services

Author

Commented:
I figured it out on my own. I'm not familiar as I want to be with the whole thing but ultimately it'll work out.