Js filter function problem.

Js filter function problem.

pirate666pirate666 Posts: 6Questions: 1Answers: 0

Hi,

I have the following javascript code:

$(document).ready(function() {
    // Sort dates code
   jQuery.extend(jQuery.fn.dataTableExt.oSort, {
        "extract-date-pre": function(value) {
            var date = $(value, 'span')[0].innerHTML;
            date = date.split('/');
            return Date.parse(date[1] + '/' + date[0] + '/' + date[2])
        },
        "extract-date-asc": function(a, b) {
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        },
        "extract-date-desc": function(a, b) {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    });
    
    // Datable
    var table = $('#tabla5').DataTable({
        "pagingType": "full_numbers",
        language: {
            url: '\\Spanish.json'
        },
        columnDefs: [{
            type: 'extract-date',
            targets: [1] // [1] es la columna fecha
        }]
    });
 
    // date filter listener
    $('#min').keyup( function() { table.draw(); } );
    $('#max').keyup( function() { table.draw(); } );
    
});

The code (// Sort dates code) I use to sort dates with the format dd / mm / yyyy. It works ok.

On the other hand (// date filter listener) I use it to filter the dates between two values ​​captured with inputs.

My problem is that the function to sort dates and the function to filter dates do not work together:
- If I delete // sort dates code, the filter between two dates works.
- If I delete // date filter listener, the date arrangement dd / mm / yyyy works.

What can I do to make all my js work at once?

THANKS!

Replies

  • pirate666pirate666 Posts: 6Questions: 1Answers: 0

    I forgot to say that this part of the code:

    // date filter listener
        $('#min').keyup( function() { table.draw(); } );
        $('#max').keyup( function() { table.draw(); } );
    

    works with range_dates.js:

    $.fn.dataTableExt.afnFiltering.push(
        function( oSettings, aData, iDataIndex ) {
            var iFini = document.getElementById('min').value; // id
            var iFfin = document.getElementById('max').value; // id
            var iStartDateCol = 1; // date column
            var iEndDateCol = 1; // date column
    
            iFini=iFini.substring(6,10) + iFini.substring(3,5)+ iFini.substring(0,2);
            iFfin=iFfin.substring(6,10) + iFfin.substring(3,5)+ iFfin.substring(0,2);
    
            var datofini=aData[iStartDateCol].substring(6,10) + aData[iStartDateCol].substring(3,5)+ aData[iStartDateCol].substring(0,2);
            var datoffin=aData[iEndDateCol].substring(6,10) + aData[iEndDateCol].substring(3,5)+ aData[iEndDateCol].substring(0,2);
    
            if ( iFini === "" && iFfin === "" )
            {
                return true;
            }
            else if ( iFini <= datofini && iFfin === "")
            {
                return true;
            }
            else if ( iFfin >= datoffin && iFini === "")
            {
                return true;
            }
            else if (iFini <= datofini && iFfin >= datoffin)
            {
                return true;
            }
            return false;
        }
    );
    
  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @pirate666 ,

    At a glance it looks like it should be OK. Would you be able to create a test case so we can see it running? Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • pirate666pirate666 Posts: 6Questions: 1Answers: 0

    live.datatables.net/sozoxaxo/1/edit

    I have the same code as in this test page, in whick everything works perfectly >.<

    But, in my localhost web, the date filter not working with

    $.fn.dataTable.moment( 'DD/MM/YYYY' ); // sort dates
    

    I have no idea how to fix it

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @pirate666 ,

    That's odd it works on that test page, but not locally. Are you using the same versions of DataTables/jQuery/etc, and are they all defined in the same order? I'm really not sure what to suggest other than that!

    Could you link to your page? Would that be possible?

    Cheers,

    Colin

  • pirate666pirate666 Posts: 6Questions: 1Answers: 0

    The truth is that I do not know what the problem was. I copied the same file in another location and it worked: S. Thank you very much for your help @colin !

This discussion has been closed.