一旦提取数据,制表符就添加“ numrow”

时间:2018-12-16 09:31:32

标签: javascript tabulator

我有一张未知数据一些列和行)的表,现在我需要使用功能table.updateData()这就要求列id必须存在于数据结构中,由于数据是从未知来源提取的,因此我无法保证。

有没有解决的办法,或者以后有什么替代的方式来更新数据?

  • p.s。我只使用香草 javascript 而不使用 jquery

1 个答案:

答案 0 :(得分:1)

在数据的每一行上都需要设置一个唯一的索引,以便Tabulator知道要引用的行。

您可以使用表构造函数中的 index 选项将其设置为行数据中的任何列字段

var table = new Tabulator("#example-table", {
    index:"age", //set the index field to the "age" field.
});

默认情况下,此字段设置为已提交的 id

如果要在本地设置此值,则可以使用变量器在此字段中为您创建一个值:

//define variable to count index's to ensure they are unique
var index = 0;

//define custom mutator
var idMutator = function(value, data, type, params, component){
    //value - original value of the cell
    //data - the data for the row
    //type - the type of mutation occurring  (data|edit)
    //params - the mutatorParams object from the column definition
    //component - when the "type" argument is "edit", this contains the cell component for the edited cell, otherwise it is the column component for the column

    return index++; //return the index for the row.
}

//column definition
{field:"id", mutator:idMutator, visible:false}

但是,如果要从远程源更新数据,则没有麻烦将其绑定到表中的数据。

标准做法是在此类数据中包含索引或唯一标识符,以允许客户端和服务器端之间进行同步