简体   繁体   English

使用来自 Google Apps 脚本的过滤器显示表格数据

[英]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 仅适用于“美国”,而不是表应该显示“美国”市场数据。

Sample how to filter rows HTML side示例如何过滤行 HTML 端

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()">';
  1. Write funciton 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.

相关问题 如何使用来自不同一维数组的数据过滤二维数组 - Javascript/Google Apps 脚本? - How to filter a two dimensional array using data from a different one dimensional array - Javascript/Google Apps Script? 如何使用 Google Apps Script 在来自 Google 表格的多行中拆分 Google 幻灯片表中的数据(基于字符限制)? - How to split data in a Google Slides Table (based on character limit) in multiple rows coming in from a Google Sheet using Google Apps Script? 使用应用程序脚本将表格图表从谷歌电子表格发送到松弛 - Sending table charts from google spreadsheet to slack using apps script 使用Google Apps脚本作为服务从电子表格中检索数据 - Using Google Apps Script as service to retrieve data from spreadsheet 使用 Google Apps 脚本从 Firebase 检索数据 - Retrieve data from Firebase using Google Apps Script 如何使用 Google Apps Script 取消透视表? - How to unpivot a table using Google Apps Script? 从数组在Google Apps脚本中制作表格 - Making table in google apps script from array 如何在 Apps 脚本(Google 表格)中过滤从 UrlFetchApp 检索到的数据 - How can I filter the data retrieved from UrlFetchApp in Apps Script (Google sheets) 如何使用 Google Apps 脚本在 Google 文档中的表格单元格内复制和编辑文本数据 - How to copy and edit text data inside a table cell in Google Documents using Google Apps Script 没有使用 Apps 脚本抓取表格的数据 - No data scraping a table using Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM