繁体   English   中英

在所有列上使用JavaScript过滤带有文本字段的表

[英]Filtering a table with text field with JavaScript on all columns

我有一个记录表,我上面有一个文本字段,可以动态过滤特定表列上的记录。 我按照这个方便的方法https://www.w3schools.com/howto/howto_js_filter_table.asp实现了它

现在,这可以像宣传的那样工作,但我希望修改它,以便过滤器查看所有列,而不仅仅是其中一列。 在How-To的示例中,我希望能够键入名称或国家/地区,并返回所有匹配项。 我尝试在JavaScript中执行此操作:

function myFunction() {   
// Declare variables   
var input, filter, table, tr, td, i;   
input = document.getElementById("myInput");   
filter = input.value.toUpperCase();   
table = document.getElementById("myTable");   
tr = table.getElementsByTagName("tr");


  // Loop through all table rows, and hide those who don't match the search query   
for (i = 0; i < tr.length; i++) {
  for (j = 0; j < tr.width; j++) {
    td = tr[i].getElementsByTagName("td")[j];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
        } else {
            tr[i].style.display = "none";
          }}}}}

j变量是我希望另外遍历每个i的列的地方,但这样做似乎打破了过滤功能,即。 输入任何内容对显示的记录没有影响 - 所有记录始终显示。 有人能告诉我我做错了什么吗?

你有几个错误:

1.循环无效:

for (j = 0; j < tr.width; j++)

没有tr.width这样的东西。 或者更确切地说,它是一个jQuery函数,因此当您键入tr.width时,您将获得它的文本。

因此脚本中断,因为for loop子句如果无效。

如果您实际上调用了这样的tr.width()您将获得该元素的像素宽度,因此它也无济于事。

你需要的是其中的<td>计数。 这是你如何得到它:

let rowTds = tr[i].getElementsByTagName("td")
for (j = 0; j < rowTds.length; j++){...}

现在你可以遍历行中的所有<td>s

2.隐藏行逻辑需要修订到多列检查:

现在您还需要更改隐藏的逻辑,因为如果最后一个<td>与过滤字符串不匹配,它将保持隐藏行。

td = tr[i].getElementsByTagName("td")[j];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
        break; // this will break the row looping on j after making the row visible.
        } else {
            tr[i].style.display = "none";
          }
        }
      }
    }
  }

结论:从W3schools教程调整完整的工作代码:

链接: https//www.w3schools.com/code/tryit.asp?filename = FVXZWZMELE1Q

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th, #myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
  background-color: #f1f1f1;
}
</style>
</head>
<body>

<h2>My Customers</h2>

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

<table id="myTable">
  <tr class="header">
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris specialites</td>
    <td>France</td>
  </tr>
</table>

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

</body>
</html>

暂无
暂无

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

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