简体   繁体   中英

tabulator update column data

I've a table set up with Tabulator with unknown data ( pulled from a file) so i can't make a column definitions setup for it except for general setup that works with any kind of data.

now I'm in situation that i need to add certain code for each row under column code which is included with the existing table.

i looked for away to add code something like SX0001 on the first row and just add 1 each row so the second row would be look like SX0002

I've checked this link http://tabulator.info/docs/4.1/update#alter-replace i saw some functions that works on rows but not columns .

solution i'm trying to achieve:- the link includes under updateData function that i need to have index with default value (Id)

giving that I'm not sure what type of date it might have or be included with the table i've added a new column using this code

table.addColumn({title:"id", field: "id" , formatter:"rownum",width:40, align:"center"} , true)

now I don't know how to get the Length of the Column to loop through each one of the column and run function like this

var no = 1000;
var str = "SX";
var x = str + no ;

table.updateData([{id:1, code:x}]);
var no = no +1;

is there's anyway to do this ?

You can retrieve the data stored in the table using the getData function.

var data = table.getData();

This will return an array containing the data objects for each row in the table.

Then to get your lenght just do data.length

If you want to count the table data the getDataCount exactly for this, which returns a count of rows in the table:

var count = table.getDataCount();

But actually your current approach will result in the table being redrawn a load of times as you update the row data.

Mutators

Instead you should look at using a Mutator , these allow you to alter data as it enters the table, so in this case you could do something like this:

//global variable to keep track of row id
var rowCount = 0;

//custom mutator
var idMutator = function(value, data, type, params, component){
  //increment rowCount and assign as column value
  return rowCount++;
}

//in your column definition set this mutator
{title:"ID", field:"id", mutatorData:idMutator}

By using this approach the value is set as the data is loaded into the table, nothing needs to be redrawn and the whole proceess is much more efficient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM