- 我正在尝试执行此操作,而是在同一个表的多个列中进行搜索:[https://www.w3schools.com/howto/howto_js_filter_table.asp][1]
- 在上面的示例中,我不仅希望按“名称”进行过滤,还希望按“国家/地区”进行过滤。
- 我在这里找到了类似的帖子,但它是针对七个单独的列,并且我找不到其他普通的 JS 代码:[https://stackoverflow.com/questions/47274028/filtering-table-with-multiple-filters ?answertab=oldest#tab-top][2]
- 我尝试复制循环(如下所示),但显然这不起作用,因为匹配与第一个循环冲突。
这对我来说有点太先进了,我需要一个解决方案。如有任何帮助,我们将不胜感激。
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
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++) {
td1 = tr[i].getElementsByTagName("td")[0];
if (td1) {
txtValue1 = td1.textContent || td1.innerText;
if (txtValue1.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
// this is what i've tried adding, but oviously, it conflicts with the first loop when matching
for (j = 0; j < tr.length; j++) {
td2 = tr[j].getElementsByTagName("td")[1];
if (td2) {
txtValue2 = td2.textContent || td2.innerText;
if (txtValue2.toUpperCase().indexOf(filter) > -1) {
tr[j].style.display = "";
} else {
tr[j].style.display = "none";
}
}
}
}
最佳答案
也许这个修改后的功能对你有帮助。基本的变化是它不在单个表格单元格内搜索,而是在完整表格行的串联文本内容内搜索。
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++) {
var rowContent = tr[i].textContent;
rowContent = rowContent.replace(/[\s]+/g, ' ');
//console.log(rowContent);
if (rowContent) {
if (rowContent.toUpperCase().includes(filter)) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
* {
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;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</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>
</body>
</html>
关于javascript - 具有多个搜索字符串的过滤表(Vanilla JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204324/