如何在最新版本的Tabulator中重新初始化数据表

问题描述 投票:0回答:1

我想重新初始化数据表,因为表中的数据已更新。我注意到$('#...').tabulator("destroy")可能是一个解决方案。但是,似乎在最新版本的Tabulator中没有制表函数。

请问如何在旧版本中实现与tabulator("destroy")相同的功能?

tabulator
1个回答
1
投票

不要使用Destroy它将销毁整个Tabulator,使用clearData Update和按照documentation添加。

检查下面的代码

<!DOCTYPE html>
<html lang="en">
<head><link href="https://unpkg.com/[email protected]/dist/css/tabulator.min.css" rel="stylesheet"></head>
<body>
<div id="example-table"></div>

<button onclick="removeData()">Clear Data</button>
<button onclick="update()">Update Data</button>


<script
  src="https://code.jquery.com/jquery-3.4.0.min.js"
  integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg="
  crossorigin="anonymous"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/js/tabulator.min.js"></script>
<script>


  const tabledata1 = [
    {id: 1, name: "Oli ", money: "0", col: "red", dob: ""},
    {id: 2, name: "Mary ", money: "0", col: "blue", dob: "14/05/1982"},
    {id: 3, name: "Christine ", money: "0", col: "green", dob: "22/05/1982"},
    {id: 4, name: "Brendon ", money: "0", col: "orange", dob: "01/08/1980"},
    {id: 5, name: "Margret ", money: "0", col: "yellow", dob: "31/01/1999"},
  ];

  const tabledata2 = [
    {id: 1, name: " Bob", money: "12", col: "red", dob: ""},
    {id: 2, name: " May", money: "1", col: "blue", dob: "14/05/1982"},
    {id: 3, name: " Lobowski", money: "42", col: "green", dob: "22/05/1982"},
    {id: 4, name: "Brendon ", money: "0", col: "orange", dob: "01/08/1980"},
    {id: 5, name: " Marmajuke", money: "16", col: "yellow", dob: "31/01/1999"},
  ];

  const table = new Tabulator("#example-table", {
    height: 205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
    data: tabledata1, //assign data to table
    layout: "fitColumns", //fit columns to width of table (optional)
    columns: [ //Define Table Columns
      {title: "Name", field: "name", width: 150},
      {
        title: "money",
        field: "money",
        align: "left",
        formatter: "money"
      },
      {title: "Favourite Color", field: "col"},
      {title: "Date Of Birth", field: "dob", sorter: "date", align: "center"},
    ]
  });

  function removeData() {
    table.clearData();
  }

  function update() {
    table.updateOrAddData(tabledata2);
    // table.addData(tabledata2);
  }


</script>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.