Search code examples
javascriptjquerydatatablesdate-rangedaterangepicker

Filter datatable with date range


I want to integrate datarange picker plugin into datatables and I write:

$(document).ready(function() {

  $(function() {
    var start = moment("2017-01-01 12:34:16");
    var end = moment("2018-03-03 10:08:07");

    function cb(start, end) {
      $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    }

    $('#reportrange').daterangepicker({
      startDate: start,
      endDate: end,
      ranges: {
        'Today': [moment(), moment()],
        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
      }
    }, cb);

    cb(start, end);

  });


  $('#reportrange').on('apply.daterangepicker', function(ev, picker) {
   var start = picker.startDate.format('YYYY-MM-DD');
   var end = picker.endDate.format('YYYY-MM-DD');



  $.fn.dataTable.ext.search.push(
    function(settings, data, dataIndex) {
      var min = start;
      var max = end;
      var startDate = new Date(data[1]);
      if (min == null && max == null) {
        return true;
      }
      if (min == null && startDate <= max) {
        return true;
      }
      if (max == null && startDate >= min) {
        return true;
      }
      if (startDate <= max && startDate >= min) {
        return true;
      }
      return false;
    }
  );
  table.draw();
});

  var dataSet = [
    ['2093',
      'Feb 23, 2018',
      'asd asd ',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 23, 2019",
    ],
    ['2092',
      'Feb 23, 2018',
      'asddd asddd',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 23, 2019",
    ],
    ['2050',
      'Feb 20, 2018',
      'Angus Fret',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 20, 2019",
    ],
    ['2048',
      'Feb 19, 2018',
      'John Smith',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 19, 2019",
    ],

    ['2046',
      'Feb 19, 2018',
      'Ana Ana',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 19, 2019",
    ],
    ['2045',
      'Feb 19, 2018',
      'Ray N',
      '[email protected]',
      '£ 50',
      '£0.00',
      "Feb 19, 2019",
    ],
    ['2044',
      'Feb 16, 2018',
      'Paul  N',
      '[email protected]',
      '£ 200',
      '£0.00',
      "Feb 16, 2019",
    ],
    ['2042',
      'Feb 13, 2018',
      'Bradley New',
      '[email protected]',
      '£ 100',
      '£0.00',
      "Feb 13, 2019",
    ],

    ['2012',
      'Jan 27, 2018',
      'Mark Zuckenberg',
      '[email protected]',
      '£ 150',
      '£0.00',
      "Jan 27, 2019",
    ],

  ];
  var table = $('#example').DataTable({
    "order": [
      [0, "desc"]
    ],
    lengthChange: false,
    data: dataSet,
    columns: [{
        title: "ORDER ID"
      },
      {
        title: "ORDER DATE"
      },
      {
        title: "PURCHASED BY"
      },
      {
        title: "RECIPIENT"
      },
      {
        title: "ORDER TOTAL"
      },
      {
        title: "VAT"
      },
      {
        title: "EXPIRY"
      }

    ]
  });






});

And as you can see I use a search datatable API function to compare dates and to get data between dates (min, max). What is the problem?

When I try to draw the table with new filtered data only with dates between min and max date I get 0 rows from datatable plugin. Why?

Here is the Fiddle: https://jsfiddle.net/ankepv5v/16/

How I can solve my problem? Why does the table return 0 results?


Solution

  • The main issue is your date comparisons

    var start = picker.startDate.format('YYYY-MM-DD');
    var end = picker.endDate.format('YYYY-MM-DD');
    

    Should be:

    var start = picker.startDate;
    var end = picker.endDate;
    

    Then you also end up with a problem that your data is getting removed completely out of the search when you filter so you need this after you draw your table

    $.fn.dataTable.ext.search.pop();
    

    https://jsfiddle.net/ankepv5v/79/