[英]Show table data using by filter from Google Apps Script
I have a data table which is getting complete date as I have from the spreadsheet.我有一个数据表,它正在从电子表格中获取完整的日期。
<script>
function createTable(dataArray) {
if(dataArray){
var result = "<table class='table table-sm' style='font-size:0.8em'>"+
"<thead style='white-space: nowrap'>"+
"<tr>"+ //Change table headings to match with he Google Sheet
"<th scope='col'>Delete</th>"+
"<th scope='col'>Edit</th>"+
"<th scope='col'>ID</th>"+
"<th scope='col'>Type</th>"+
"<th scope='col'>serial</th>"+
"<th scope='col'>ssbarcode</th>"+
"<th scope='col'>market</th>"+
"<th scope='col'>program</th>"+
"<th scope='col'>owner</th>"+
"<th scope='col'>condition</th>"+
"<th scope='col'>active</th>"+
"</tr>"+
"</thead>";
for(var i=0; i<dataArray.length; i++) {
result += "<tr>";
result += "<td><button type='button' class='btn btn-danger btn-xs deleteBtn' onclick='deleteData(this);'>Delete</button></td>";
result += "<td><button type='button' class='btn btn-warning btn-xs editBtn' onclick='editData(this);'>Edit</button></td>";
for(var j=0; j<dataArray[i].length; j++){
result += "<td>"+dataArray[i][j]+"</td>";
}
result += "</tr>";
}
result += "</table>";
var div = document.getElementById('inventorydataTable');
div.innerHTML = result;
document.getElementById("message").innerHTML = "";
}else{
var div = document.getElementById('inventorydataTable');
div.innerHTML = "Data not found!";
}
}
</script>
/* requirement */ /* 要求 */
Now I wanted to get the data from "inventorydataTable" using filters on UI like if I select only for "US" than table should show for the "US" market data.现在,我想使用 UI 上的过滤器从“inventorydataTable”中获取数据,例如我的 select 仅适用于“美国”,而不是表应该显示“美国”市场数据。
You need to create a filter functionality consisting of two steps:您需要创建一个包含两个步骤的过滤器功能:
1.Create an input field where the user inserts a string to filter for (eg US) and bind an onkeyup event listener to it. 1.创建一个输入字段,用户在其中插入要过滤的字符串(例如 US),并将onkeyup事件侦听器绑定到它。
Sample:样本:
<input type="text" id="myFilter" onkeyup="filterRows()">';
filterRows()
to apply the filter to the column of your choice (the one where you expect to find US
)编写filterRows()
以将过滤器应用于您选择的列(您希望找到US
的列)Sample:样本:
function filterRows(a) {
var table = document.getElementById("ID OF YOUR TABLE");
var tr = table.getElementsByTagName("tr");
var length = tr.length;
// specify the column on which you want to perform the filtering:
var columnNumber = 2;
var searchField = document.getElementById("myFilter");
var filter = searchField.value;
// Loop through all table rows, and hide those who don't match the search query
for (var i = 0; i < tr.length; i++) {
var td = tr.getElementsByTagName("t")[columnNumber];
var input = td.innerHTML;
if (input && input.indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
References:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.