Create onEdit callback in handsontable

2019-07-26 17:24发布

问题:

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.

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?

回答1:

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

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.