javascript - 通过文本字段过滤列表

标签 javascript html filter

我创建了一个小应用程序,可以帮助我提高工作效率。它按文本内容过滤标签列表。目前,它非常适合查找连续的文本行,但我更愿意将其重新实现为适用于非连续文本组合的过滤器。

例如,如果标签内的字符串是“Appleville, MN”,如果您输入“Apple”或“Appleville”或“App”,当前过滤器将找到它,但我希望能够成功过滤输入“App MN”等。

有人能给我指出正确的方向吗?我不确定我需要使用什么确切的算法。谢谢!

附注:我在下面添加了一些代码,其中包含我正在过滤的搜索项类型、当前过滤功能以及搜索字段的示例。

function filterList() {
  var input, filter, ul, li, a, i, txtValue;
  input = document.getElementById('myInput');
  filter = input.value.toUpperCase();
  ul = document.getElementById("itemList");
  li = ul.getElementsByTagName('li');
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
<input type="text" id="myInput" onkeyup="filterList()" placeholder="Search list..">
<ul id="itemList">
  <li onclick=applevilleMN()><a href="#">Appleville, MN</a></li>
  <li onclick=orangevilleOR()><a href="#">Orangeville, OR</a></li>
  <li onclick=kiwivilleCA()><a href="#">Kiwiville, CA</a></li>
  <li onclick=bananavilleCA()><a href="#">Bananaville, CA</a></li>
</ul>

最佳答案

这是一个修改版本,可以满足您的需求。

它将过滤器字符串按空格分割以创建过滤器数组,仅当它们对于数组的每个子字符串都有效时才会显示li

function filterList() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById('myInput');
    filter = input.value.toUpperCase();
    ul = document.getElementById("itemList");
    li = ul.getElementsByTagName('li');

    for (i = 0; i < li.length; i++) {
      a = li[i].getElementsByTagName("a")[0];
      txtValue = a.textContent || a.innerText;
      
      // split filter by spaces, gives ["app", "MN"] in your example  
      let filters = filter.split(" ") 

      // remove the empty filters (if your filter string 
      // starts or ends by a space) since they are source of errors

      // Array.filter takes in parameter a function returning a boolean
      // it create a new array containing only element where 
      // the function returned truthy value

      // here we return the length of the string which is falsy (== 0) for "" 
      // and truthy for every other string (!= 0)
      filters = filters.filter(f => f.length)   

      let shouldDisplay = true
      // test each filter and store true only if string contains all filter
      filters.forEach(filt => {
        shouldDisplay = shouldDisplay && txtValue.toUpperCase().includes(filt)
      })
      
      // update visibility
      // set visible if the string include all filters
      // or if there is no filter
      li[i].style.display = (shouldDisplay || filters.length === 0) ? "" : "none";
    }
  }
<ul id="itemList">
      <li onclick=applevilleMN()><a href="#" >Appleville, MN</a></li>
      <li onclick=orangevilleOR()><a href="#" >Orangeville, OR</a></li>
      <li onclick=kiwivilleCA()><a href="#" >Kiwiville, CA</a></li>
      <li onclick=bananavilleCA()><a href="#" >Bananaville, CA</a></li>
</ul>

<input type="text" id="myInput" onkeyup="filterList()" placeholder="Search list.." >

文档:Array.filter , truthy values , falsy values

关于javascript - 通过文本字段过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098566/

相关文章:

javascript - 为什么我的字符串是 "new String(''); "return an object? But "字符串 ('my string' ); “返回一个字符串

javascript - 如何在 jQuery 中看到目标 div 时突出显示 anchor 链接?

javascript - 使用 json 和 jsPDF 生成发票 PDF 或帐单 PDF

javascript - 使用 javascript 进行 Bootstrap 模式调用,最佳实践

javascript - 如何使用 JS 将十六进制代码添加到 HTML 元素(内联样式)

WordPress:如何获取 'the_content' 过滤器的所有注册函数

r - 向量:如何过滤掉值递减的段

sql - 我可以在 Advantage 数据库表 (TAdsTable) 上使用 "IN"过滤器吗?

javascript - Javascript 中的自调用函数有害吗?

html - 如何在 web 2.0 中绘图?