Date range search

Date range search

dheeru46dheeru46 Posts: 4Questions: 3Answers: 0

Hi
i want to search data with date range from server side or Ajax. we are trying to do but it's not working.please see the code given below.
$(document).ready(function() {

    //datatables
    table = $('#example1').DataTable({ 
        "scrollX": true,
        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.

        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "ReportMembershipMonthly.php')?>",
            "type": "POST",
            "data": {"date1": $('#datefrom').val(),"date2": $('#dateto').val()}
        },
    });
});

with this code first time date find.but when we select another date this code not working.

Answers

  • devkennethdevkenneth Posts: 1Questions: 0Answers: 0
    edited September 2016

    Hi,

    I would try something like this. Please not that I am using moment.js for handling dates.
    Please note that my input strings are DD.MM.YYYY (03.12.2015). You can change the input string as you like. Also I have all the dates in one column. You can also include other columns.

    `
    $.fn.dataTableExt.afnFiltering.push(
    function (oSettings, aData, iDataIndex) {
    var start = moment($('#beginDate').val(), "DD.MM.YYYY");
    var end = moment($('#endDate').val(), "DD.MM.YYYY");
    var iStartDateCol = 0;
    var between = moment(aData[iStartDateCol], "DD.MM.YYYY");

          if (start.isValid() && end.isValid()) {
              if (between.isBetween(start, end, 'days', '[]')) {
                  return true;
              }
              else {
                  return false;
              }
    
          }  else {
              return true;
          }
        }
    

    );`

    Now i just need to listen for the events on begin and end date fields.
    $('#beginDato').change(function () { table.draw(); });
    What I tend to do is to change endate first then begindate and only listen for the begindate event. This will ensure only one table.draw() and that both begin and enddate is valid..

    Moment.js makes date range easy to implement.

This discussion has been closed.