在Handsontable中创建onEdit回调 [英] Create onEdit callback in handsontable

查看:197
本文介绍了在Handsontable中创建onEdit回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 handsontable 库,当我发现

I'm using handsontable library and I got quite amazed when I found out there isn't an onEdit callback.

我试图通过使用一个用户在同一问题中提供的脚本来创建该脚本,但似乎适用于旧版本,并且不再起作用.

I tried to create it by using the script provided by one user in that same issue but it seems its for an old version and it doesn't work anymore.

我试图弄清楚如何添加一个钩子,但是从动手操作的角度来看,它的文档相当匮乏. 有人知道如何为handsontable创建这样的回调吗?

I tried to figure out how to add a hook, but the documentation on it is quite poor from handsontable. Does anybody know how to create such a callback for handsontable?

推荐答案

仅捕获双击是不够的,因为用户可以使用 Enter F1 键.

Capturing only double clicks is not enough, since a user may further enter edit-mode using the Enter or F1 keys.

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

One solution to the lack of onEdit callback is to register a custom editor. This way it fits nicely into all the edit-save-exit lifecycle (for example the the Esc key closes the editor and loses all changes). Here's a very simplified editor which extends the built-in and default 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 solution, however, is not generic, since if there is more than one editor used, it will have to get replaced too. It should work in simple cases, though. The complete example can be found in this fiddle.

这篇关于在Handsontable中创建onEdit回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆