I have a handsontable, when a cell is changed I want to recalculate the current row values.
I am doing this by using the afterChange hook, doing the calculations and then setting the new cell value using setDataAtCell.
The issue is I am getting stuck in an infinite loop because the setDataAtCell is triggering the afterChange it is currently using.
I have tried adding a false variable and setting this to true if it has been run to get around this but this refuses to work. Even if it did work then in my scenario of calculating the data it wouldn’t work for me because it would mean if the same data was changed a second time then the recalculation wouldn’t occur.
Any ideas on how to solve?
var hot = new Handsontable(container, {
data: data,
stretchH: "all",
rowHeaders: true,
colHeaders: true,
filters: true,
dropdownMenu: true,
formulas: true,
afterChange: function( changes, source ) {
if( arguments[1] != "loadData" ) {
if( !changes ) {
return;
}
changedRowStartingZero = changes[0][0];
changedRow = changedRowStartingZero;
console.log( '-------' );
var currentRow = changedRow;
var net = hot.getDataAtCell( currentRow, 7 );
var netTrade = hot.getDataAtCell( currentRow, 8 );
var deliveryCost = hot.getDataAtCell( currentRow, 10 );
var fees = hot.getDataAtCell( currentRow, 11 );
var totalCost = netTrade + deliveryCost + fees;
var netProfit = net - totalCost;
var percentProfit = netProfit / net ;
hot.setDataAtCell( currentRow, 12, totalCost );
hot.setDataAtCell( currentRow, 13, netProfit );
hot.setDataAtCell( currentRow, 14, percentProfit );
}
},