简体   繁体   English

如何过滤/搜索 HTML 表格中的列?

[英]How to filter/search columns in HTML table?

I have followed this link on how to create/filter search a table.我已按照此链接了解如何创建/过滤搜索表。 But I want to filter search for each column in my own table.但我想过滤搜索我自己表中的每一列。 Currently, it only searches for the First Name column .目前,它只搜索 First Name 列 How can I do this?我怎样才能做到这一点?

This is the code:这是代码:

myHTML:我的HTML:

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<table id="myTable">
    <tr class="header">
        <th> First Name </th>
        <th> Last Name </th> 
        <th> Age </th>
        <th> Language </th>
    </tr>
    <tr>
        <td>John</td>
        <td>Kole</td>
        <td>18</td>
        <td>English</td>
    </tr>
    <tr>
        <td>Pearl</td>
        <td>Shine</td>
        <td>50</td>
        <td>Hindi</td>
    </tr>
   <tr>
        <td>Jacob</td>
        <td>Pool</td>
        <td>22</td>
        <td>Arabic</td>
   </tr>
   <tr>
        <td>David</td>
        <td>Struff</td>
        <td>30</td>
        <td>German</td>
   </tr>
</table>

<script>
    function myFunction() {
        var input, filter, table, tr, td, i, txtValue;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("myTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[0];
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                    }
                else {
                    tr[i].style.display = "none";
                    }
                }       
            }
        }
</script>

How can I filter all columns for searching?如何过滤所有列以进行搜索? Currently, it searches the First Name column only.目前,它只搜索名字列。

Check each nodes of the table row instead of just checking tr[i].getElementsByTagName("td")[0] .检查表行的每个节点,而不仅仅是检查tr[i].getElementsByTagName("td")[0] The zeroth node corresponds to first name field, that's why the search result restricts to the first name field only.. Search for all the nodes instead.第零个节点对应于名字字段,这就是为什么搜索结果仅限于名字字段。而是搜索所有节点。

Leave the tr node with class name of header while making the row hidden.保留tr节点的类名称为header同时隐藏该行。

 function myFunction() { var input, filter, table, tr, td, i, txtValue; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { // td = tr[i].getElementsByTagName("td")[0]; alltags = tr[i].getElementsByTagName("td"); isFound = false; for(j=0; j< alltags.length; j++) { td = alltags[j]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; j = alltags.length; isFound = true; } } } if(!isFound && tr[i].className !== "header") { tr[i].style.display = "none"; } } }
 <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> <table id="myTable"> <tr class="header"> <th> First Name </th> <th> Last Name </th> <th> Age </th> <th> Language </th> </tr> <tr> <td>John</td> <td>Kole</td> <td>18</td> <td>English</td> </tr> <tr> <td>Pearl</td> <td>Shine</td> <td>50</td> <td>Hindi</td> </tr> <tr> <td>Jacob</td> <td>Pool</td> <td>22</td> <td>Arabic</td> </tr> <tr> <td>David</td> <td>Struff</td> <td>30</td> <td>German</td> </tr> </table>

Hope this is what you are looking for.希望这是你正在寻找的。

This line:这一行:

td = tr[i].getElementsByTagName("td")[0];

gets the first td which is the content for the first column in the table meaning the FirstName column.获取第一个td ,它是表中第一列的内容,意思是 FirstName 列。 Do a loop on all td tags instead of getting only the first one and you'll get the functionality you need.对所有td标签进行循环,而不是仅获取第一个标签,您将获得所需的功能。

Get all table rows.获取所有表格行。 Loop through these rows.循环遍历这些行。 Put all the innerText from the <td>s in each row together into a string .将每行<td>s所有innerText一起放入一个字符串中 Check the first occurrence of the search value in the string.检查字符串中第一次出现的搜索值。 If the value is not found, hide the row with display: none else show the row with display: table-row .如果未找到该值,则使用display: none else 隐藏该行,使用display: table-row显示该display: table-row

Example例子

 const id = "myTable"; const selector = `#${id} tr:not(.header)`; // Get all rows const trs = document.querySelectorAll(selector); function myFunction(event) { let search = event.target.value.toLowerCase(); trs.forEach((tr) => { // Get all cells in a row let tds = tr.querySelectorAll("td"); // String that contains all td textContent from a row let str = Array.from(tds).map((td) => { return td.textContent.toLowerCase(); }).join(""); tr.style.display = (str.indexOf(search) > -1) ? "table-row" : "none"; }); }
 <input type="text" id="myInput" onkeyup="myFunction(event)" placeholder="Search for names.." title="Type in a name"> <table id="myTable"> <tr class="header"> <th> First Name </th> <th> Last Name </th> <th> Age </th> <th> Language </th> </tr> <tr> <td>John</td> <td>Kole</td> <td>18</td> <td>English</td> </tr> <tr> <td>Pearl</td> <td>Shine</td> <td>50</td> <td>Hindi</td> </tr> <tr> <td>Jacob</td> <td>Pool</td> <td>22</td> <td>Arabic</td> </tr> <tr> <td>David</td> <td>Struff</td> <td>30</td> <td>German</td> </tr> </table>

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

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