简体   繁体   English

粘贴行时,Handsontable afterCreateRow会触发多次

[英]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. Handsontable会生成两个新行,以便为粘贴的数据腾出空间。

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. 我的问题:afterCreateRow事件触发两次,每次触发的次数为1。我希望触发一次,触发次数为2,因为Handsontable事先知道必须创建两行。

Here is a jsfiddle that demonstrates the phenomenon. 这是一个演示该现象的jsfiddle

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. 我只想为每个用户操作调用一次submitBatch 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: 您可以使用window.setTimeout()将提交延迟到某个时间段过去,而没有新的afterCreateRow()调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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