DataTables案例不敏感搜索问题

问题描述 投票:1回答:1

我正在使用DataTables和jQuery创建一组复选框,以使用搜索框过滤表格。这在http://www.lynden.com/about/brochures-test.html完全实现。基本上,当您选中带有复选框的选项时,它会将该选项作为字符串并将其插入DataTables搜索框中。它工作得很好,除了caseInsensitive功能在没有任何先前过滤的情况下将输入输入搜索框时直接起作用,这很奇怪。更奇怪的是,当用“mi”进行搜索时,它会在动态中提取三个结果,但完全会忽略银河公司的类别。有没有人知道为什么它可以对搜索案例不敏感但忽略大写单词的开头?

    $('.dropdown-container')
.on('click', '.dropdown-button', function () {
    $('.dropdown-list').toggle();
})
.on('input', '.dropdown-search', function () {
    var target = $(this);
    var search = target.val().toLowerCase();
console.log(search);
    if (!search) {
        $('li').show();
        return false;
    }

    $('li').each(function () {
        var text = $(this).text().toLowerCase();
        var match = text.indexOf(search) > -1;
        $(this).toggle(match);
    });
})
.on('change', '[type="checkbox"]', function () {
    var numChecked = $('[type="checkbox"]:checked').length;
    $('.quantity').text(numChecked || 'Any');
});

$(document).ready(function () {
table = $('#brochure').DataTable({
    "search": {
"caseInsensitive": true
  },
    "pageLength": 25,
    "order": [
                [2, "desc"]
            ],
    "lengthMenu": [
                [25, 50, 75, -1],
                [25, 50, 75, "All"]
            ],
    "columnDefs": [{
        "targets": [2, 4, 5],
        "visible": false
            }]
});
var pID = location.search; //grab everything after and including the "?" in the URL
console.log(pID);
mloc = pID.indexOf("=") + 1; //identify the location of the actual value
pID = pID.slice(mloc) // captures the value of the parameter
table.search(pID, [1, 2, 3, 4], true, false, false, true).draw(); // assigns the parameter to the hidden input tag's value
})

function filter() {
//build a industry string 
var filters = $('input:checkbox[name="filter"]:checked').map(function ()    {
    return this.value;
}).get().join(' ');
console.log(filters);
//now filter in column 3, with a regular expression, no smart filtering, no inputbox, not case sensitive
table.search(filters, [1, 2, 3, 4], true, false, false, true).draw();
}

我会在这个问题上得到一些帮助!

jquery filter datatable datatables case-insensitive
1个回答
1
投票

你错误地使用search() API方法。显然,您对search()的初始错误调用会导致表的行为与配置不同。

您应该如下所示调用它:

table.search(pID).draw();

table.search(filters).draw();

可以省略search()的其他参数,因为它们启用“智能”和不区分大小写的搜索。

有关代码和演示,请参阅this example

© www.soinside.com 2019 - 2024. All rights reserved.