[英]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>
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.