DataTables中的实时刷新表过滤器

时间:2019-10-10 08:07:01

标签: datatable datatables datatables-1.10

欢迎您, 我无法在DataTables中添加过滤器刷新。

在此示例中,它很好用: http://live.datatables.net/gejojiqu/1/edit

我添加此行对我不起作用:

// The rebuild will clear the exisiting select, so it needs to be repopulated
var currSearch = column.search();
if ( currSearch ) {
   select.val( currSearch.substring(1, currSearch.length-1) );
}

我的代码如下:

$(document).ready(function () {
    $('#projects-table').DataTable({
        "order": [[ 0, "desc" ]],
        "sDom": '<"top">rt<"bottom"p><"clear">',
        stateSave: true,
        initComplete: function () {

            this.api().columns([0, 1, 2]).every(function () {
                var column = this;
                var filtry = $('#projects-filters');
                var th = $("#projects-headers").find("th").eq(column.index());

                var select = $('<select id="' + th.text().replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '') + '" class="custom-select"><option value="">' + th.text() + '</option></select>')
                    .appendTo($(filtry))
                    .on('change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val());

                        column.search(val ? '^' + val + '$' : '', true, false)
                            .draw();
                    });

                column.data().unique().sort().each(function(d, j) {
                    var val = $.fn.dataTable.util.escapeRegex(d);
                    if (column.search() === "^" + val + "$") {
                      select.append(
                        '<option value="' + d + '" selected="selected">' + d + "</option>"
                      );
                    } else {
                      select.append('<option value="' + d + '">' + d + "</option>");
                    }
                });

            });
        }
    });
});

有人有什么想法吗?

0 个答案:

没有答案