簡體   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