【问题标题】:How to modify this search table to go through all columns如何修改此搜索表以遍历所有列
【发布时间】:2021-04-16 20:56:16
【问题描述】:

如何修改此搜索以遍历所有列。现在它只是浏览第一列的结果。

let input = document.getElementById("search_table");
let filter = input.value.toUpperCase().trim();
let table = document.getElementById("listingTable");
let tr = table.getElementsByTagName("tr");
            
                
for (let i = 0; i < tr.length; i++) {
  let td = tr[i].getElementsByTagName("td")[0];
    if (td) {
            let txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) > -1) {
                  tr[i].style.display = "";
              } else {
                  tr[i].style.display = "none";
                }
    } 

}

【问题讨论】:

    标签: javascript html search html-table


    【解决方案1】:

    这取决于您需要过滤/搜索如何准确工作,但如果您只想检查关键字是否存在于行中的任何位置,您可以检查行本身的 textContent 或 innerText,无需检查特定列或所有列,即

    for (let i = 0; i < tr.length; i++) {
      tr[i].style.display = (tr[i].innerText.toUpperCase().includes(filter)) ? 'table-row' : 'none';
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 1970-01-01
      • 2010-11-13
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多