javascript - 表多列过滤器

标签 javascript jquery

我想为我的一个表编写一个多列过滤器。我现在不想用插件来做到这一点。我发现它有效,但仅适用于 1 列。您对如何修改所有列有任何想法吗?

$(".filter").keyup(function(e) { // filter is class 
    if (e.keyCode == 13) {
        var indexColumn = $(this).attr('data-id');
        console.log('INDEX ' + indexColumn);
        console.log("value=%o", this.value);
        //split the current value of searchInput
        var data = this.value.split(" ");
        //create a jquery object of the rows
        var jo = $("#fbody").find("tr")
            //hide all the rows
        if (this.value == "") {
            jo.show();
            return;
        }
        jo.hide();
        //Recusively filter the jquery object to get results.
        jo.filter(function(i, v) {
            var $t = $(this).children(":eq(" + indexColumn + ")");
            for (var d = 0; d < data.length; ++d) {
                if ($t.is(":contains('" + data[d] + "')")) {

                    // console.log(data[d]);
                    return true;
                }
            }
            return false;
        }).show(); //show the rows that match.
        updateTotals();
    }
});

Fiddle demo

最佳答案

在这种情况下,您可以使用 each() jquery 方法。

$(".filter").each(function() {
    $(this).keyup(function(e) {
        if (e.keyCode == 13) {
            var indexColumn = $(this).attr('data-id');
            console.log('INDEX ' + indexColumn);
            console.log("value=%o", this.value);
            //split the current value of searchInput
            var data = this.value.split(" ");
            //create a jquery object of the rows
            var jo = $("#fbody").find("tr")
                //hide all the rows
            if (this.value == "") {
                jo.show();
                return;
            }
            jo.hide();
            //Recusively filter the jquery object to get results.
            jo.filter(function(i, v) {
                var $t = $(this).children(":eq(" + indexColumn + ")");
                for (var d = 0; d < data.length; ++d) {
                    if ($t.is(":contains('" + data[d] + "')")) {

                        // console.log(data[d]);
                        return true;
                    }
                }
                return false;
            }).show(); //show the rows that match.
        }
    })
});

JS Demo

关于javascript - 表多列过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435238/

相关文章:

javascript - 使用 map 在 React 上进行 2 次循环后添加类

javascript - AES-256-CBC 解密 - Cryptojs 加密和 PHP 解密

JavaScript。需要在函数内连接两个数组

javascript - 如何使用 Javascript 设置所有元素的样式?没有 Jquery(描述中的 2 个示例)

jquery - 循环遍历一个元素的所有后代,获取它们的属性

javascript - 获取脚本以返回 HTML 中的链接

javascript - 为什么我的变量未定义?

javascript - 列表中的三个元素并排渲染

jquery - 单击表格行以使用 jQuery 选择复选框

javascript - 概述行为,同时关注点击并关注选项卡