简体   繁体   English

创建 Function 以搜索整个表而不是仅搜索一列

[英]Create Function to search entire table and not one column only

I have a table with a search function. Initially, my intention was to only search the contents of the first column.我有一个搜索 function 的表。最初,我的意图是只搜索第一列的内容。 I would like to change this and create a search function that searches the entire table.我想更改它并创建一个搜索 function 来搜索整个表。

Below is a snippet of the current code for it:下面是它的当前代码片段:

<script>
function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("userinfo");
  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++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

I am using onkeyup in a text field to call the function我在文本字段中使用 onkeyup 来调用 function

Below is the current code for it.下面是它的当前代码。

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for Address" title="Search for Address">

Please help me implement a search function that searches the entire table and hides the rows that done match the input.请帮我实现一个搜索 function 搜索整个表并隐藏与输入匹配的行。

regards,问候,

Check if this is want you want,search by country/email/postal code: table-search.surge.sh检查这是不是你想要的,按国家/电子邮件/邮政编码搜索:table-search.surge.sh

If you could give us a piece of your table so i can work my own function around it,anyway,this is what i found and its working(searches the entire table).Try to test this work in another file https://speedysense.com/filter-html-table-using-javascript/如果你能给我们一张你的桌子,这样我就可以围绕它工作我自己的 function,无论如何,这就是我发现的和它的工作(搜索整个表格)。尝试在另一个文件https://speedysense中测试这项工作.com/filter-html-table-using-javascript/

 function searchTable() { var input, filter, found, table, tr, td, i, j; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("userinfo"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { // reading inner html from the td of tr td = tr[i].getElementsByTagName("td"); for (j = 0; j < td.length; j++) { // comparing the inner html if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else{ tr[i].style.display = "none"; } } } }
 <input id='myInput' onkeyup='searchTable()' type='text'> <table id="userinfo"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Be.nett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table>

try the above working code试试上面的工作代码

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

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