筛选带有多个搜索字符串的表格(Vanilla JS)

时间:2019-03-17 06:04:32

标签: javascript html


  • 我正在尝试执行此操作,但要在同一张表的多个列中进行搜索:[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";
      }
    } 
  }

}

3 个答案:

答案 0 :(得分:1)

也许此修改后的功能对您有帮助。基本的变化是,它不会在单个表格单元格中进行搜索,而是在完整表格行的连接文本内容中进行搜索。

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>

答案 1 :(得分:0)

按空格分隔搜索字符串,并过滤每个单词的列。您可以为两个列使用相同的for()。尝试以下代码:

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

答案 2 :(得分:0)

好吧,我以不同的方式认为我的错误,您可以将搜索功能分开以简化工作,并且可以尝试使用字符串连接修改我的代码。


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");
   search(input, filter, table, tr);

}


function search(input, filter, table, tr){
 for (i = 1; i < tr.length ; i++) {
    td = tr[i].getElementsByTagName("td");
    var concat='';
    for (j = 0; j < td.length; j++) {
     txtValue = td[j].textContent || td[j].innerText;
     concat += txtValue+' ';
     }
     console.log(concat);
    if (td) {

      if (concat.toUpperCase().search(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";

      }
      }
  }
}