在 handsontable 中创建 onEdit 回调

Create onEdit callback in handsontable

我正在使用 handsontable library and I got quite amazed when I found out there isn't an onEdit callback

我尝试使用 that same issue 中一位用户提供的脚本来创建它,但它似乎适用于旧版本并且不再有效。

我试图弄清楚如何添加一个挂钩,但是关于它的文档从 handsontable 来看非常差。 有人知道如何为 handsontable 创建这样的回调吗?

仅捕获双击是不够的,因为用户可以使用 EnterF1 键进一步进入编辑模式。

缺少 onEdit 回调的一个解决方案是注册自定义编辑器。这样它就可以很好地适应所有编辑-保存-退出生命周期(例如 Esc 键关闭编辑器并丢失所有更改)。这是一个非常简化的编辑器,它扩展了内置和默认的 TextEditor:

var LoggingEditor = Handsontable.editors.TextEditor.prototype.extend();

LoggingEditor.prototype.getValue = function() {
    console.log('User finished editing the cell, the value will be set to: '
                + this.TEXTAREA.value);
    return this.TEXTAREA.value;
};

LoggingEditor.prototype.setValue = function(newValue){
    console.log('User started editing the cell, value shown in cell is: '
                + newValue);
    this.TEXTAREA.value = newValue;
};

但是,此解决方案并不通用,因为如果使用了多个编辑器,则也必须更换它。不过,它应该适用于简单的情况。完整的示例可以在 this fiddle 中找到。