簡體   English   中英

Javascript匹配/搜索結果表-初學者總數

[英]Javascript match/search table for results - total beginner

我當然經驗不足,但是我正在努力改善公司的工作流程。 我正在使用此處的代碼創建內部站點以搜索當前零件號。 我的員工正在按描述進行搜索,但是示例中包含的javascript按確切的輸入而不是單個單詞進行搜索。

例如,使用網站上的示例代碼,我想搜索“交易島”以返回與“島嶼貿易”相同的結果。 我知道這是可能的,但無法弄清楚如何使其工作。

<!DOCTYPE html>
<html>
<head>
<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++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
</script>

</body>
</html>

當搜索單詞匹配項時,最好將搜索字符串放入數組中,並將要搜索的單元格內容放入數組中。

然后,您可以輕松地使用Array.prototype.every()Array.prototype.some()方法查看單元格中是否存在所有或某些搜索詞。

有關更多詳細信息,請參見下面的我的評論。

 // Get a reference to the input var searchElement = document.getElementById("myInput"); // Hook element up to event handler: searchElement.addEventListener("keyup", search); // Event handler: function search(){ // Now, break the search input into an array of words by splitting the string where // there are spaces (regular expression used here to locate the strings). // Also, note that strings are case-sensitive, so we are taking the input and // forcing it to lower case and we'll match by lower case later. var searchWords = searchElement.value.toLowerCase().split(/\\s+/); // Test to see the individual words //console.clear(); //console.log(searchWords); // Now you have to have some content to search against. // So, we'll get all the cells, which will be our data source: var theCells = document.querySelectorAll("td"); // Loop over each cell theCells.forEach(function(cell){ // Reset any previous cell matches cell.style.backgroundColor = "inherit"; // Get the words in the cell as an array (note: we are forcing lower // case again here to match lower case against lower case var cellWords = cell.textContent.toLowerCase().split(/\\s+/); // See if the cell contains all of the search words (You can substitute // "some" for "every" to just see if the cell contains some of the search words var result = cellWords.every(elem => searchWords.indexOf(elem) > -1); // every and some return a boolean letting you know if your condition was met: if(result){ console.clear(); console.log("Match found in: " + cell.textContent); cell.style.backgroundColor = "#ff0"; } }); } 
 <input type="text" id="myInput" 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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM