DATE RANGE FILTER

DATE RANGE FILTER

maurotrentinimaurotrentini Posts: 1Questions: 1Answers: 0

In my datatable i want to give users ability to:

1) Search for a specific email in a text email field
2) Filter by dates range (for example "Last 3 Months", "Last Month", "All") in a date field

I got 1) to work following this example (https://datatables.net/examples/api/multi_filter_select.html) but applying it to a text search (also got some hints from here -> https://datatables.net/examples/api/multi_filter.html).

However I cannot get 2) to work. I'm following this example (http://datatables.net/reference/api/filter()), but it doesn't work for me. It triggers a POST on change, but no filter is applied to my table.

My understanding is that, although I used .search for 1), I need to use .filter() for 2) because I'm trying to filter a range of values (am I wrong?).

Below is a snapshot of my code:

var table = $('#p2p_cases_dev').dataTable( {
            initComplete: function () {
                        var api = this.api();

                        /* DATES RANGE FILTER - DOES NOT WORK */
                        var columnTimeOpened = api.column( 1 );
                        var select = $('<select id="timeopened_fltr">' +   
                                            '<option value="1">Last Month</option>' +  
                                            '<option value="3" selected="true">Last 3 Months</option>' +  
                                            '<option value="0">All</option>' +  
                                        '</select>')
                            .appendTo( $(columnTimeOpened.footer()).empty() )
                            .on( 'change', function () {
                                var val = $.fn.dataTable.util.escapeRegex( //the selected drop down value (0,1,or 3)
                                    $(this).val()
                                );
                                columnTimeOpened
                                      .data()
                                      .filter( function ( value, index, api ) {
                                            //here i will put my dates calculation, like for example: "if date - 3 months return true, else return false"
                                            //BUT FOR NOW NO MATTER WHAT I RETURN NO FILTER IS APPLIED
                                            return  false; //i tried return true, nothing changes.
                                      } )
                                      .draw();
                            } );

                            /* EMAIL TEXT SEARCH - WORKS REALLY WELL */
                            var columnEmail = api.column( 5 );
                            var title = $('#p2p_cases_dev thead th').eq( 5 ).text();
                            var input = $('<input type="text" placeholder="Search '+title+'" />')
                                .appendTo( $(columnEmail.footer()).empty() )
                                .on( 'keyup', function () {
                                        var val = $.fn.dataTable.util.escapeRegex( //the search text
                                            $(this).val()
                                        );
                                        if (val.length > 3 || val.length == 0){ //minimum search chars before trigger search (or clear search)
                                            columnEmail
                                                .search( val ? val : '', false, false )
                                                .draw();
                                        }
                            } );
            },
            "dom": 'lrtp',
            "scrollY": 500,
            "scrollX": true,
            ajax: {
                url: "/dte/p2pcases/load",
                type: "POST"
            },
            "deferRender": true,
            serverSide: true,
            processing: true,
            "lengthMenu": [[10,25,50,100,1000,10000],[10,25,50,100,"1,000","10,000"]],

            "pagingType": "full_numbers", //http://datatables.net/reference/option/pagingType

            order: [ 0, 'desc' ],   //https://datatables.net/examples/basic_init/table_sorting.html
    "columns": [

..............
]
} );

This discussion has been closed.