列重新排序后,jQuery数据表过滤不起作用

时间:2015-09-17 19:09:21

标签: jquery datatables

使用jquery数据表我通过页脚中的下拉列表进行过滤。 我遇到的问题是,当重新排序列时,过滤器会停止工作,并会抛出其中一个神秘错误,说明http://datatables.net/tn/4更多信息(这并没有让我更接近解决这个问题) )。

var datatableOpts = {
        "dom": 'CRT<"clearfix"><"pull-left"l><"pull-right"f>r<"horizontal-scroll"t><"pull-left"i><"pull-right"p><"clearfix">',
        "colReorder": {
            "reorderCallback": function () { }
        },
        "stateSave": true,
        "stateLoadCallback": function (settings) {
            return reportJson;
        },
        "initComplete": function () {
            $(this.api().columns()[0]).each(function (i, item) {
                var column = $("#reportTable").DataTable().columns(item)
                var currentFilter = $("#reportTable").DataTable().columns(i).search()[0]
                var select = $("<select><option value=''>No Filter</option></select>")
                    .appendTo($(column.footer()).empty())
                    .on("change", function () {
                        var val = $(this).val()

                        //I added the following 2 rows as on initialize the column the filter was being applied against was set and after re-order, that columns index would then be different..... didn't solve the issue.
                        var index = $(this).parent("th").index();
                        column = $("#reportTable").DataTable().columns(index);

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

                $(_.uniq(column.data()[0])).sort().each(function (d, j) {
                    var value = removeSpanTags(j).trim()

                    if ($(select).find("option[value='" + HtmlEncode(value) + "']").length >= 1) {
                        return;
                    }

                    var opt = $("<option></option>").attr("value", value).text(value)

                    if (currentFilter != "" && currentFilter == value) {
                        opt.attr("selected", "selected")
                    }

                    opt.appendTo(select)
                });
            });
        }
    }

0 个答案:

没有答案