简体   繁体   English

如何让搜索过滤器循环遍历库存项目表中的 MULTIPLE 数据类型,并缩小显示的结果范围?

[英]How can I get a search filter to loop through MULTIPLE data types in a stock items table, and narrow down what results displayed?

Please excuse me if I use the wrong terminology.如果我使用了错误的术语,请原谅。

I have working javascript code in a php file which filters what data is displayed in a table of stock items on a WordPress page front-end.我在 php 文件中有工作 javascript 代码,该文件过滤了 WordPress 页面前端的库存项目表中显示的数据。

ie search by serial number and the more you type the less items are shown on a WordPress front-end until one unique item is found.即按序列号搜索,键入的越多,在 WordPress 前端显示的项目就越少,直到找到一个唯一项目。

Client wants to be able to search in a variety of ways - and I can change which single topic is being searched eg I can get it to search by dispatch number, serial, asset type, description etc, but cannot get the search to look at all the topics.客户希望能够以多种方式进行搜索 - 我可以更改正在搜索的单个主题,例如我可以通过调度号、序列号、资产类型、描述等进行搜索,但无法让搜索查看所有的话题。 ie the id in the code only allows me to choose one of the elements即代码中的id只允许我选择其中一个元素

If I change the "1" here to "2" or "3" etc it chnages the search focus from column 1 to 2 to 3 etc.如果我将此处的“1”更改为“2”或“3”等,它将搜索焦点从第 1 列更改为 2 到 3 等。

td = tr[i].getElementsByTagName("td")[1];

Can anyone suggest how to search all the data at the same time?谁能建议如何同时搜索所有数据?

<script>
    function helloWorld() {
        var input, filter, table, tr, td, sn, ty, own, i, id;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("adminTable");
        tr = table.getElementsByTagName("tr");
        var data = new Array();
        // Loop through all table rows, and hide those who don't match the search query
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[0];
            if (td) {
                if(td.children[0].checked) {
                    sn = tr[i].getElementsByTagName("td")[4];
                    ty = tr[i].getElementsByTagName("td")[9];
                    id = tr[i].getElementsByTagName("td")[10];
                    own = tr[i].getElementsByTagName("td")[11];
                    var record = {serial: sn.textContent || sn.innerText, type: ty.textContent || ty.innerText, id: id.textContent || id.innerText, owner: own.textContent || own.innerText};
                    data.push(record);
                }
            }
        }

        if(data.length > 0) {
            var uri = JSON.stringify(data);
            var res = encodeURIComponent(uri); 
            window.location.href = '../stock?transferData='.concat(res);
        }
    }
function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("adminTable");
  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++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

enter image description here在此处输入图像描述

This javascript https://fusejs.io/ search library might be all you need.这个 javascript https://fusejs.io/搜索库可能是您所需要的。 You might need to map your WP data first but this will let you search for anything you want.您可能需要先 map 您的 WP 数据,但这将让您搜索您想要的任何内容。

Try this:尝试这个:

Replace代替

 for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }

by this这样

(Set column numbers you need in the [1,4,5] list) (在 [1,4,5] 列表中设置您需要的列号)

  var columns = [1,4,5];
  // Loop through all table rows, and hide those who don't match the search query  
  for (var j=0, lenj=columns.length; j < lenj; j++) {     
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[columns[j]];
        if (td) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何缩小多复选框过滤器中的搜索结果? - How to narrow search results in a multiple checkbox filter? 如何在一张表上使用多个过滤器列表(html-select)来缩小结果而不产生冲突? - How to use multiple filter lists (html-select) on one table to narrow down results without the conflicting? 如何更新Stock Quote API以获取搜索结果? - How can I update Stock Quote API to take in search results? 如何获得此代码以不断循环遍历各个项目? - How can I get this code to continuously loop through the items? 如何通过由所有类型的数据类型组成的嵌套对象数组来实现搜索? 简单来说,我想搜索表格 - How to Implement search through an array of nested objects consisting of all types of data types? In simple term I want to search through the table 我可以在TypeScript中缩小范围吗? - Can I narrow this down in TypeScript? 如何在JavaScript中过滤这些搜索结果? - How can I filter these search results in JavaScript? 多个复选框过滤缩小搜索范围 - Multiple Checkbox Filtering to Narrow Down a Search jQuery循环遍历具有多个元素类型的表并获取值 - jQuery loop through table with multiple element types and get values 从Stripe API检索发票时,如何缩小结果范围以仅从对象内的一个属性检索数据? - When retrieving an invoice from the Stripe API, how do I narrow down the results to only retrieve data from one attribute within the object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM