Calculation on vTiger field before save handler on

2019-08-22 02:57发布

问题:

I need to understand how to do real time calculation on Edit.js

By searching and looking around i came up with this code in Edit.js of the Contact Module.

calculate_amount: function (){
var units = $("input[name='cf_852']");
var value = $("input[name='cf_854']");
$(units, value).on('keyup', function(){
if (units.val() != '' && value.val() != ''){
var currentamount = units.val() * value.val();
$("input[name='cf_856']").val(currentamount);
}
});
}

Have i done something wrong? Because it doesn't work..

Thanks for all the help!

回答1:

I should write your keyup function like this:

I tested, it's OK

calculateAmount: function (){  
  var units = $("input[name='cf_1512']");
  var value = $("input[name='cf_1514']");
  $(document).on('keyup',"input[name='cf_1512'], input[name='cf_1514']", function(){
    if (units.val() != '' && value.val() != ''){
      var currentamount = units.val() * value.val();
      $("input[name='cf_1518']").val(currentamount);
    }
  })
},


回答2:

You must call you function into the function registerBasicEvents. If the registerBasicEvents function is not available in Edit.js of Contacts module so add it.

registerBasicEvents: function (container) {
  this._super(container);
  this.calculate_amount();
}


回答3:

You should pass id's of element instead reference of element for Keyup function. Please find below code snippet and modify your code in vTiger. As I checked all syntax and function written correctly in your script. Just pass the comma separated id's and execute the code. Thanks!

var units = $('#Contacts_editView_fieldName_cf_1512');
var value = $('#Contacts_editView_fieldName_cf_1514');
  
  $('#Contacts_editView_fieldName_cf_1512, #Contacts_editView_fieldName_cf_1514').on('keyup', function(){
 
        if (units.val() != '' && value.val() != ''){
           var currentamount = parseFloat(units.val()) * parseFloat(value.val());
           $("#Contacts_editView_fieldName_cf_1516").val(currentamount);
        }
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
Input1: <input type="text" id="Contacts_editView_fieldName_cf_1512"/><br/>
Input2: <input type="text" id="Contacts_editView_fieldName_cf_1514" /><br/>
Result: <input type="text" id="Contacts_editView_fieldName_cf_1516"/>