JavaScript匹配/搜索表以获取结果-Total初学者



我肯定是我经验不足的,但是我正在尝试增强公司的工作流程。我正在使用此处的代码创建内部站点来搜索当前零件号。我的员工正在通过描述进行搜索,但是示例搜索中包含的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>

最新更新