我有一个未知数据表(一些列和行),我正处于需要使用table.updateData()
要求列id
存在于数据结构中的函数的地步,我不能保证,因为数据是从中提取的来源不明。
有没有办法解决这个问题,或者以后有没有其他更新数据的方法?
- ps我只使用香草javascript而不是jquery
我有一个未知数据表(一些列和行),我正处于需要使用table.updateData()
要求列id
存在于数据结构中的函数的地步,我不能保证,因为数据是从中提取的来源不明。
有没有办法解决这个问题,或者以后有没有其他更新数据的方法?
需要在数据的每一行上设置唯一索引,以便 Tabulator 知道您要引用哪一行。
您可以使用表构造函数中的index选项将其设置为行数据中的任何列字段
var table = new Tabulator("#example-table", {
index:"age", //set the index field to the "age" field.
});
默认情况下,这设置为id字段
如果您想在本地设置它,您可以使用 mutator 在此字段中为您创建一个值:
//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}
但是,如果您要从远程源更新数据,则无法将其与表中的数据联系起来。
在此类数据中包含索引或唯一标识符以允许客户端和服务器端之间的同步是标准做法