Vipul Singh
Vipul Singh

Reputation: 393

DataTable filter with Date range

I have 2 DataTable in one page for both DataTable I have added date range using

function filterDateRangeService(){
        $.fn.dataTable.ext.search.push(

                function( settings, data, dataIndex ) {                 
                    var dateStart = moment($("#fromDateSer").val(),"DD/MM/YYYY");
                    var dateEnd = moment($("#toDateSer").val(),"DD/MM/YYYY");

                    var evalDate= moment(data[5],"DD/MM/YYYY");
            console.log("dateStart +"+dateStart+" dateEnd "+dateEnd)

                // console.log("insidde")
                    if (evalDate >= dateStart && evalDate <= dateEnd) {
                        return true;
                    }
                    else {
                        return false;
                    }
                     }


            );

function filterDateRangePatient(){
            $.fn.dataTable.ext.search.push(

                    function( settings, data, dataIndex ) {                 
                        var dateStart = moment($("#fromDate").val(),"DD/MM/YYYY");
                        var dateEnd = moment($("#toDate").val(),"DD/MM/YYYY");

                        var evalDate= moment(data[6],"DD/MM/YYYY");
                console.log("dateStart +"+dateStart+" dateEnd "+dateEnd)

                    // console.log("insidde")
                        if (evalDate >= dateStart && evalDate <= dateEnd) {
                            return true;
                        }
                        else {
                            return false;
                        }
                         }


                ); 

Both the function are called when respective checkbox is clicked. Problem is when I use date range for first function it filters the table for specific Date range, but When i try to filter the other table with 2nd function, only first function is called, i.e which ever function is called first only that function is called each time. Calling function Using:

$('input[name="radioFilter"]').click(function(){        
                filterFunction();
            });
            $('input[name="radioFilterSer"]').click(function(){
                filterFunctionSer();
            });  
function filterFunction(){
//some functions..
filterDateRangePatient()
}
function filterFunctionSer(){
//some functions..
filterDateRangeService()
} 

How to resolve this, Why is the first function which is called first is only being called later also, or any thing I am doing Wrong. Or is it because of return Statement.

Upvotes: 0

Views: 1539

Answers (1)

Siva.G ツ
Siva.G ツ

Reputation: 839

What about write single function with params and use it on both functions

function filterFunction(){
    //some functions..
    filterDateRange($("#fromDateSer").val(), $("#toDateSer").val(), 5)
}
function filterFunctionSer(){
    //some functions..
    filterDateRange($("#fromDate").val(), $("#toDate").val(), 6)
}

And the filterDateRange Function: (Updated: Added search pop function)

function filterDateRange(from, to, num){

    $.fn.dataTable.ext.search.pop();

    $.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {                 
        var dateStart = moment(from,"DD/MM/YYYY");
        var dateEnd = moment(to,"DD/MM/YYYY");

        var evalDate= moment(data[num],"DD/MM/YYYY");
        console.log("dateStart +"+dateStart+" dateEnd "+dateEnd);

        if (evalDate >= dateStart && evalDate <= dateEnd) {
            return true;
        }
        else {
            return false;
        }
    });
}  

It will work.

Upvotes: 2

Related Questions