我正在使用 handsontable图书馆,当我发现 there isn't an onEdit
callback 时我感到非常惊讶.
我尝试使用 that same issue 中一位用户提供的脚本来创建它但它似乎适用于旧版本并且不再有效。
我试图弄清楚如何添加一个钩子(Hook),但是关于它的文档从 handsontable 来看非常差。 有人知道如何为 handsontable 创建这样的回调吗?
最佳答案
仅捕获双击是不够的,因为用户可能会使用 Enter 或 F1 键进一步进入编辑模式。
缺少 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 中找到.
关于javascript - 在 handsontable 中创建 onEdit 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654330/