数据表日期选择器/日期范围过滤器不起作用

时间:2018-12-13 13:03:14

标签: filter datatables jquery-ui-datepicker date-range

我正在尝试在我的表上实现日期范围/日期选择器过滤器。我的代码不起作用,我不明白为什么。我调试时遇到的唯一断点是:

onSelect: function () {
        lTable.draw();
    },

没有其他断点命中。当我选择一个日期时,什么也没发生。我已经搜寻了数周的网络,试图找出问题所在。

$.fn.dataTable.ext.search.push(
                    function (settings, data, dataIndex) {
                        var from = $('#from').datepicker("getDate");
                        var to = $('#to').datepicker("getDate");

                        //data being searched
                        var startDate = new Date(data[2]);

                        //if true show row/ if not, don't
                        if (from == null && to == null) { return true; }
                        if (from == null && startDate <= to) { return true; }
                        if (to == null && startDate >= from) { return true; }
                        if (startDate <= to && startDate >= from) { return true; }
                        return false;
                    }
                );    

                //DATE RANGE FILTER
                var lTable = $("WTM_LOG").DataTable();
                $("#to").datepicker({
                    onSelect: function () {
                        lTable.draw();
                    },
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: "dd/mm/yy"
                });

                $("#from").datepicker({
                    onSelect: function () {
                        lTable.draw();
                    },
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: "dd/mm/yy"
                });

                // Event listener to the two range filtering inputs to redraw on input
                $('#from, #to').change(function () {
                    lTable.draw();
                });

1 个答案:

答案 0 :(得分:0)

删除此阻止:

onSelect: function() { lTable.draw(); },

然后将您的听众更改为此。尝试将其放入您的$(document).ready()

$('#from, #to').on('keyup change', function () { lTable .order([2, 'asc']) // asc or desc .draw(); });