创建Tabulator实例后如何设置回调?

How to set callback after the instance of Tabulator is created?

我正在使用 Tabulator 并想收听 table 编辑事件。我检查了回调 dataEdited 并发现所有示例在创建实例时都设置了回调函数,例如 new Tabulator('#samplediv',{dataEdited:function(){ ...}}).

但是,我需要在创建实例后在子函数中设置回调,例如:

const table=new Tabulator(......)

function SomeSub(){
// how to set dataEdited of table here?
}

我试过 table.dataEdited=function(){} 无效。

我不知道对于熟练的程序员来说这是否是一个真正的问题,但它确实让我感到困惑。非常感谢您的评论。

Tabulator 中的回调在实例化后无法更改,但您可以简单地从回调内部调用另一个函数。

如果您只想跟踪用户编辑,而不是所有数据更改,那么 cellEdited 回调可能更适合合适的选择。

function externalFunction(cell){
    do a thing;
}

var table = new Tabulator("#example-table", {
    cellEdited:function(cell){
        externalFunction(cell);
    },
});

如果您希望能够更改所调用函数的性质,那么您可以将函数添加到外部对象上,然后随时更改它。

//define initial function holding object
var functionHolder = {
    externalFunction:function (cell){
        do a thing;
    }
}

//create your table
var table = new Tabulator("#example-table", {
    cellEdited:function(cell){
        functionHolder.externalFunction(cell);
    },
});

//...

//some time later redefine the function
functionHolder.externalFunction = function(cell){
    //do something different
}