我有一个记录表,在它上面有一个文本字段,它动态地过滤特定表列上的记录。我按照这个简单的How-To 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的列的地方,但这样做似乎会破坏过滤功能,即。键入任何内容都不会影响显示的记录-所有记录都会一直显示。谁能告诉我我哪里做错了?
发布于 2018-10-06 04:03:24
这里有几个错误:
1. for循环无效:
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>
https://stackoverflow.com/questions/52672345
复制相似问题