2
votes

My scenario: I have a table with four rows. The first three rows are filled with data and the fourth row is blank. I highlight the entire first three rows, copy, then paste into the fourth row. Handsontable generates two new rows to make room for the pasted data.

My problem: The afterCreateRow event fires twice, and each time the amount is 1. I would expect it to fire once with an amount of 2, since Handsontable knows in advance that two rows must be created.

Here is a jsfiddle that demonstrates the phenomenon.

Here is what I'm trying to do:

function afterCreateRow(row, amount) {
    for (var i = row; i < row + amount; i++) {
        model.initializeNewRow(i);
        model.calculator.cascadeRow(i);
        model.calculator.calculateRow(i);
    }
    model.calculator.updateTotals();
    model.history.submitBatch();
}

I only want to call submitBatch once for each user action. How can I achieve this?

1
Could you provide a fiddle? This is the intended behavior but we can help you if you show us some code. - ZekeDroid
I added some example code. Is this enough? - Aaron
I don't see it. Where's the handsontable code? - ZekeDroid
Sorry. Now I added a jsfiddle. - Aaron

1 Answers

0
votes

You could use window.setTimeout() to delay the submit until some time period has passed with no new afterCreateRow() calls:

function afterCreateRow(row, amount) {
    for (var i = row; i < row + amount; i++) {
        model.initializeNewRow(i);
        model.calculator.cascadeRow(i);
        model.calculator.calculateRow(i);
    }
    model.calculator.updateTotals();
    delayed(model.history.submitBatch, 100);
}

var g_id = null;
function delayed(fn, timeoutms){
    if(g_id) window.clearTimeout(g_id);

    g_id = window.setTimeout(fn, timeoutms);
}