Filter by date range

Filter by date range

oldano97oldano97 Posts: 10Questions: 4Answers: 0

I have a column of dates which has this format "yyyy-mm-dd hh:mm:ss". I want to be able to filter based on a date range. Is this possible?

Answers

  • SradesignSradesign Posts: 25Questions: 4Answers: 1
    $.fn.dataTableExt.afnFiltering.push(
    function( settings, data, dataIndex ) {
                                var mindate = $('#fromSelector').val(),
                                var mindate = new Date(mindate).getTime() / 1000;
                                var maxdate = $('#toSelector').val();
                                var maxdate = new Date(maxdate).getTime() / 1000;
                                
                                                            // ... column that you stored the date in my case is column 1
                                var rangedate = parseFloat( data[1] ) || 0;
                         
                                if ( ( isNaN( mindate ) && isNaN( maxdate ) ) ||
                                     ( isNaN( mindate ) && rangedate <= maxdate ) ||
                                     ( mindate <= rangedate   && isNaN( maxdate ) ) ||
                                     ( mindate <= rangedate   && age <= maxdate ) )
                                {
                                    return true;
                                }
                                if ( document.getElementById('example') == settings.nTable ){
                                    return false;
                                } else {
                                    return true;
                                }
                            }
                        );
    
  • oldano97oldano97 Posts: 10Questions: 4Answers: 0

    I added this function in the afnFiltering and added this:

    $('#fromSelector, #toSelector').change( function() {
              table.fnDraw();
     });
    

    But I have no errors and nothing happens.

  • oldano97oldano97 Posts: 10Questions: 4Answers: 0

    I used this bootstrap datetimepicker : http://tarruda.github.io/bootstrap-datetimepicker/

  • oldano97oldano97 Posts: 10Questions: 4Answers: 0
    edited July 2015

    I solved the problem with that solution:

    $.fn.dataTable.ext.search.push(
       function( settings, data, dataIndex ) {
         var min = Date.parse( $('#fromSelector').data("datetimepicker").getDate());
         var max = Date.parse( $('#toSelector').data("datetimepicker").getDate());
         var date = Date.parse( data[1] ) || 0;
       
          if ( ( isNaN( min ) && isNaN( max ) ) ||
               ( isNaN( min ) && date <= max ) ||
               ( min <= date   && isNaN( max ) ) ||
               ( min <= date   && date <= max ) )
          {
            return true;
          }
          return false;
      }
    );
    

    And the correct event handler is:

    $('#fromSelector, #toSelector').on("changeDate", function() {
      table.draw();
    });
    
  • oldano97oldano97 Posts: 10Questions: 4Answers: 0

    I was testing the last code I've posted in other browsers than google chrome, and it works only in opera and google chrome. In firefox and i.e. 9 the table shows me the message "No matching records found" when I chage the fromSelector value.
    How can I solve it ??

This discussion has been closed.