简体   繁体   English

每个表列的JavaScript过滤单元格

[英]JavaScript filtering cells for each table columns

I have a table with n rows by m columns. 我有一个表,其中n行乘m列。 My goal is to put multiple filtering boxes so I can filter multiple columns in separated filtering boxes. 我的目标是放置多个过滤框,以便可以在单独的过滤框中过滤多个列。 I have JavaScript like this: 我有这样的JavaScript:

function (document) {
    'use strict';
        var MyTableFilter = (function (Arr) {
            var _input;
            function _onInputEvent(e) {
                _input = e.target;
                var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
                Arr.forEach.call(tables, function (table) {
                    Arr.forEach.call(table.tBodies, function (tbody) {
                        Arr.forEach.call(tbody.rows, _filter);
                    });
                });
            }
            function _filter(row) {    
                var input0Value = document.getElementById('input0').value;
                var input1Value = document.getElementById('input1').value;
                var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
                var tdsArray = row.children;
                var td0Content = tdsArray[0].textContent;
                var td1Content = tdsArray[1].textContent;
                function isDisplayRow() {
                    return td0Content.indexOf(input0Value) !== -1 &&
                            td1Content.indexOf(input1Value) !== -1
                }
                row.style.display = isDisplayRow() ? 'table-row' : 'none';
            }
            return {
                init: function () {
                    var inputs = document.getElementsByClassName('my-table-filter');
                    Arr.forEach.call(inputs, function (input) {
                        input.oninput = _onInputEvent;
                    });
                }
            };
        })(Array.prototype);

        document.addEventListener('readystatechange', function () {
            if (document.readyState === 'complete') {
                MyTableFilter.init();
            }
        });

  })(document);

I have input id / class in form like this: 我有这样的形式输入id / class

<input type="search" id="input0" class="my-table-filter" data-table="order-table" placeholder="Firstname"/>
<input type="search" id="input1" class="my-table-filter" data-table="order-table" placeholder="Lastname"/>

However, when I run the program, the result is not what I expected. 但是,当我运行该程序时,结果不是我期望的。 So for example, I have John Smith, Henry Wills in the table. 例如,我在桌子上有约翰·史密斯,亨利·威尔斯。 If I type 'H'; 如果我输入“ H”; in first name search box and 'S' in last name search box, I should only get 'JOHN SMITH'. 在名字搜索框中输入“ S”,在姓氏搜索框中输入“ S”,我应该只会得到“ JOHN SMITH”。 However, I am getting 'HENRY WILLS' as well because 'HENRY' has 'N' and 'WILLS' has 'S'. 但是,我也得到了“ HENRY WILLS”,因为“ HENRY”具有“ N”,而“ WILLS”具有“ S”。 If I type out the whole words of 'JOHN SMITH', it works OK. 如果我输入“ JOHN SMITH”的整个单词,就可以了。

Also, how do I turn off case sensitive for the input text? 另外,如何关闭输入文本的区分大小写? I am very new to JavaScript. 我对JavaScript非常陌生。 Any suggestion would be appreciated! 任何建议,将不胜感激!

The issue comes from your filter function, which does a substring search. 问题出在您的过滤器函数中,该函数执行子字符串搜索。 You can modify it to a prefix search easily: 您可以轻松地将其修改为前缀搜索:

function isDisplayRow() {
    return td0Content.indexOf(input0Value) === 0 &&
        td1Content.indexOf(input1Value) === 0
}

如果只想从String的开头进行搜索,则可以将indexOf更改为String.prototype.startsWith或td0Content.substring(0,input0Value.length)=== input0Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM