简体   繁体   中英

Handsontable afterCreateRow fires multiple times when pasting rows

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM