Kristen L
Kristen L

Reputation: 65

Datatables cant search by date

I have issue with searching by date range. I have in date also time so if I search range then it not finding it. But if i remove time. It working and can find date range.

I also tryed colspan="2" but its giving error:

Cannot set property '_DT_CellIndex' of undefined

My code:

    $(document).ready(function () { 

            $.fn.dataTable.ext.search.push(
                function (settings, data, dataIndex) {
                    var min = $('#min').datepicker("getDate");
                    var max = $('#max').datepicker("getDate");
                    var d = data[4].split("/");
                    var startDate = new Date(d[1]+ "/" +  d[0] +"/" + d[2]);

                    if (min == null && max == null) { return true; }
                    if (min == null && startDate <= max) { return true;}
                    if(max == null && startDate >= min) {return true;}
                    if (startDate <= max && startDate >= min) { return true; }
                    return false;
                }
            );


            $("#min").datepicker({ onSelect: function () { oTable.draw(); }, changeMonth: true, changeYear: true , dateFormat:"dd/mm/yy"});
            $("#max").datepicker({ onSelect: function () { oTable.draw(); }, changeMonth: true, changeYear: true, dateFormat:"dd/mm/yy" });
            var oTable = $('#sort').DataTable();
            $('#min, #max').change(function () {
                oTable.draw();
            });
        });

Also tryed this method add colspan and extra td tag for time: https://jsfiddle.net/yerz2dg2/

But its giving error, what I mentioned on upper.

Upvotes: 0

Views: 339

Answers (1)

annoyingmouse
annoyingmouse

Reputation: 5699

I'm not sure what was happening in your original code as the JSFiddle didn't have the references to the libraries you were using (datepicker). But I've rewritten it a wee bit and it should now work:

$(document).ready(function() {
    var oTable = $('#sort').DataTable({
        initComplete: function() {
            $("#min").datepicker({
                onSelect: function() {
                    oTable.draw();
                },
                changeMonth: true,
                changeYear: true,
                dateFormat: "dd/mm/yy"
            });
            $("#max").datepicker({
                onSelect: function() {
                    oTable.draw();
                },
                changeMonth: true,
                changeYear: true,
                dateFormat: "dd/mm/yy"
            });
        }

    });
});

$.fn.dataTable.ext.search.push(
    function(settings, data, dataIndex) {
        var min = (moment($('#min').val(), "DD/MM/YYYY").isValid()) ? ~~moment($('#min').val(), "DD/MM/YYYY").format("X") : null;
        var max = (moment($('#max').val(), "DD/MM/YYYY").isValid()) ? ~~moment($('#max').val(), "DD/MM/YYYY").format("X") : null;
        var d = ~~moment(data[1], "DD/MM/YYYY").format("X");
        if (min == null && max == null) {
            return true;
        }
        if (min == null && d <= max) {
            return true;
        }
        if (max == null && d >= min) {
            return true;
        }
        if (d <= max && d >= min) {
            return true;
        }
        return false;
    }
);

Working JSFiddle. I've added momentjs as who really can be bothered making sense of JS dates?

Hope that helps.

Upvotes: 1

Related Questions