Search code examples
tabulator

Tabulator 4.2: sorter:"date" not working?


Even in the example given @ the Tabulator site:

http://tabulator.info/basic/4.2

the date column does not sort, unlike the others. Is there a fix for that?


Solution

  • Date sorting for Tabulator is dependent on MomentJS. Import Moment Js to your code

    {title: "Date Of Birth", field: "dob", sorter:"date", sorterParams:{format:"DD/MM/YY"}},
    

    Please check this Snippet

      const tabledata1 = [
        {id: 1, name: "Oli ", money: "0", col: "red", dob: "14/05/1982"},
        {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 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", sorterParams:{format:"DD/MM/YY"}},
        ]
      });
    
      function removeData() {
        table.clearData();
      }
    
      function update() {
        table.updateOrAddData(tabledata2);
        // table.addData(tabledata2);
      }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/js/tabulator.min.js"></script>
    <link href="https://unpkg.com/[email protected]/dist/css/tabulator.min.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    
    <div id="example-table"></div>