【问题标题】:Javascript match/search table for results - total beginner结果的 Javascript 匹配/搜索表 - 初学者
【发布时间】:2016-12-12 21:22:34
【问题描述】:

诚然,我非常缺乏经验,但我正在努力改进我公司的工作流程。我正在使用来自here 的代码来创建内部站点以搜索当前部件号。我的员工正在按描述进行搜索,但示例中包含的 javascript 按精确输入而不是单个单词进行搜索。

例如,使用网站上的示例代码,我希望搜索“Trading Island”以返回与“Island Trading”相同的结果。我知道这是可能的,但不知道如何让它发挥作用。

<!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>

【问题讨论】:

  • 我们需要查看一些代码来提供帮助
  • @MatthewFlanigan 我刚刚更新了我的答案(并简化了它),对表格中的单元格内容进行了单词搜索。输入单词的顺序无关紧要。现在,必须将单元格中的所有单词都输入到搜索中才能进行匹配,但是如果您阅读 cmets,您会发现可以更改此设置,以便只需要输入一些输入的搜索词要找到的单元格的匹配项。

标签: javascript search html-table match


【解决方案1】:

搜索词匹配时,最好将搜索字符串放在 放入数组中,将要搜索的单元格内容放入数组中。

然后您可以轻松地使用Array.prototype.every()Array.prototype.some() 方法查看单元格中是否存在所有或部分搜索词。

请参阅下面的我的 cmets 内联了解更多详细信息。

// 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2011-04-04
    • 2011-04-24
    相关资源
    最近更新 更多