List control in new form - on change

Hi, I've got a form with a list control in it, and I want to update the total of the lines (in the main form) when an entry is added or changed in the list control. I've tried a few things from here but nothing seems to work in the New form.

fd.rendered(function() {

	// disable the field
	fd.field('Total').disabled = true;
	fd.field('Order_x0020_Number').disabled = true;
	
	fd.field('Order_x0020_Number').value = getOrdVar();

	//refreshing the  total
	var myVar = setInterval(calcTotal, 2000);

	fd.control('SPDataTable1').$on('change', function() {   
        fd.control('SPDataTable1').widget.bind("dataBound",function(){
            calcTotal();
        })
    });
}); 

The calcTotal function updates the Total in the main form.

Could you show me where I'm going wrong?

NB the child form is in non-dialog view

Hello

Currently, change event only works for dialog mode. Inline editing is not yet supported.

As an option, you can either change the settings to open child items in the dialog or use the below code which will refresh total value periodically and on form submit.

Please do the following changes in the code:

  • Replace {SPDataTable0} with the List or Library Control internal Name.
  • Change Value in the code (in places like value[i].Value and value[i].Value.replace(/$/g,'')) with the InternalName of the column in the related items.
 function calcTotal() {

var value = fd.control('{SPDataTable0}').widget.dataItems();
var total = 0;
if(value){
    for (var i = 0; i < value.length; i++){
        if(value[i].Value)
          total += parseInt(value[i].Value.replace(/[^0-9.]/g, ""));
    }
}
fd.field('Total').value = total;
}
var myVar = 0;

fd.spRendered(function() {

// disable the field
fd.field('Total').disabled = true;

//refreshing the  total
var myVar = setInterval(calcTotal, 2000);

}); 

fd.spBeforeSave(function(spForm) {
    var myVar = setInterval(calcTotal, 2000);
    calcTotal();
    clearInterval(myVar);
});
1 Like

Does this work with calculated columns as well?

My setup is

Parent (SP List)

Child (SP List)

Code

//Form Calculation - total from items

function calcTotal() {

var value = fd.control('SPDataTable1').widget.dataItems();
var total = 0;
if(value){
for (var i = 0; i < value.length; i++){
if(value[i].Total)
total += parseInt(value[i].Total.replace(/[^0-9.]/g, ""));
}
}
fd.field('Total_Amount').value = total;
}
var myVar = 0;

fd.spRendered(function() {

// disable the field
fd.field('Total_Amount').disabled = true;

//refreshing the total
var myVar = setInterval(calcTotal, 2000);

});

fd.spBeforeSave(function(spForm) {
var myVar = setInterval(calcTotal, 2000);
calcTotal();
clearInterval(myVar);
});

Hello @cameron_stewart,

Welcome to Plumsail Community!

Yes, the code works for calculated columns as well, but you might need to update it.

Could you please share the screenshot of the form opened in the browser. I need to see the format of the calculated column value in List or Libary control to help you with the code.

Thanks @mnikitina - is this enough?

and child list. I made them numbers instead of currency

Hello @cameron_stewart,

Please see the code below.

As your column contains not whole numbers, I've replaced parseFloat function with Number function.

Please make sure that you are using the internal field names in the code.

function calcTotal() {

var value = fd.control('SPDataTable1').widget.dataItems();
var total = 0;
if(value){
    for (var i = 0; i < value.length; i++){
        if(value[i].Total)
          total += Number(value[i].Total.replace(/[^0-9.]/g, ""));
    }
}
fd.field('Total_Amount').value = total;
}
var myVar = 0;

fd.spRendered(function() {

// disable the field
fd.field('Total_Amount').disabled = true;

//refreshing the  total
var myVar = setInterval(calcTotal, 2000);

}); 

fd.spBeforeSave(function(spForm) {
    var myVar = setInterval(calcTotal, 2000);
    calcTotal();
    clearInterval(myVar);
});

Doesn't seem to do anything :frowning:

@cameron_stewart,

Are you getting any errors in the browser console (F12)? Could you please share the screenshot.