Search code examples
tabulator

How to reload data in tabulator data table?


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


Solution

  • 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.