How to add filter option in Datatable?

I have a datatable where I am fetching all records from the db. Now I need to add filter option so that Admin can click on the filter button and download the data in Excel/PDF/CSV.
So far I have created the filter option but when I select the date range its showing me no data records found.

Php File below

<body>
  <div id="reportrange" class="btn btn-success btn-lg"><span></span> <b class="caret"></b>
  </div>
  <hr>
  <br>
  <table id="allempreports" class="table table-striped" cellspacing="0" width="100%">  </table>
 </body>

Javascript

<script type="text/javascript">
     $(document).ready(function() {

  $(function() {
    var start = moment("2019-01-01 00:00:00");
    var end = moment("2019-12-31 00:00:00");

    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();
  $.fn.dataTable.ext.search.pop();
});
</script>

I solved it. Thanks all

Sponsor our Newsletter | Privacy Policy | Terms of Service