Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload data in tabulator data table?

Tags:

tabulator

I am trying to load a dynamic data using tabulator I can generate table and data using this code:

 $("#example-table").tabulator({
        layout:"fitColumns",

        columns:[ //Define Table Columns
        {title:"Name", field:"name", width:150},
        {title:"Age", field:"age", align:"left", formatter:"progress"},
        {title:"Favourite Color", field:"col"},
        {title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
    ],
      });
      var tabledata = [
      {id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
      {id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
      {id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
      {id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
      {id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
      ];

    //  $("#example-table").tabulator("setData", []);

      $("#example-table").tabulator("setData", tabledata);

But when I am trying to reload same data onclick function I am getting error:

 Options Error - Tabulator does not allow options to be set after initialization unless there is a function defined for that purpose

So my question is how to empty data of table and reload new data or same data

like image 852
Nilay Singh Avatar asked Oct 16 '25 15:10

Nilay Singh


2 Answers

The accepted answer on this is a bit overkill, you can call the setData and setColumns functions at any after the table is created. there is no need to destroy it first

//update columns and data without destroying the table
$("#example-table").tabulator("setColumns", res["column"]);
$("#example-table").tabulator("setData", res["data"]);
like image 96
Oli Folkerd Avatar answered Oct 19 '25 13:10

Oli Folkerd


I guess you want to populate your table with new data for that you need to empty the data of current table for that you need to see this link:

Tabulator Discussion

Here olifolkerd is saying that you could either reinitialise the grid, by destroying it and recreating it using:

 $("#example-table").tabulator("destroy");
 $("#example-table").tabulator({...}); 

If you want to check if table is empty of not use this jquery to check if tabulator data is active or not:

 var j = $( "#example-table" ).hasClass( "tabulator" )
     if (j) {
        $("#example-table").tabulator("destroy");

      }
//set new columns
      $("#example-table").tabulator("setColumns", res["column"]);

      //set new data
      $("#example-table").tabulator("setData", res["data"]);

I think this will solve your problem.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!