【问题标题】:filtering html table containing links过滤包含链接的 html 表
【发布时间】:2021-03-22 16:01:11
【问题描述】:

基于How to filter a html table using simple javascript? 我想用链接到url 的单元格过滤我的html 表格。 如何防止后台网址也被过滤?

例如,当我搜索“sweden”时,它还显示“德国”,因为“Germany”链接到表中的https://germany.net/sweden

function myFunction() {
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (var i = 0; i < tr.length; i++) {
    var tds = tr[i].getElementsByTagName("td");
    var flag = false;
    for(var j = 0; j < tds.length; j++){
      var td = tds[j];
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        flag = true;
      } 
    }
    if(flag){
        tr[i].style.display = "";
    }
    else {
        tr[i].style.display = "none";
    }
  }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names then click to launch.." title="Type in a name">

<table id="myTable">
  <tr class="header">
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td><a href="https://germany.net/sweden">Germany</a></td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris specialites</td>
    <td>France</td>
  </tr>
</table>
</body>
</html>

更新:附加问题 - 是否也可以根据列名进行过滤?因此,例如在过滤器领域使用例如。 Country:ger 查找“国家”列中包含字符串 ger 的所有行?

【问题讨论】:

    标签: html html-table


    【解决方案1】:

    innerHTML 更改为innerText,因此请避免在过滤器中包含该链接。

    旁注:这仅在您想确保 HTML 元素不包含在您的搜索中时才有效。如果您的用例超出此范围,您将需要不同结构(如 JSON)中的数据进行过滤,这也会生成表格。

    function myFunction() {
      var input, filter, table, tr, td, i;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      table = document.getElementById("myTable");
      tr = table.getElementsByTagName("tr");
      for (var i = 0; i < tr.length; i++) {
        var tds = tr[i].getElementsByTagName("td");
        var flag = false;
        for(var j = 0; j < tds.length; j++){
          var td = tds[j];
          if (td.innerText.toUpperCase().indexOf(filter) > -1) {
          //      ^-- I changed this
            flag = true;
          } 
        }
        if(flag){
            tr[i].style.display = "";
        }
        else {
            tr[i].style.display = "none";
        }
      }
    }
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names then click to launch.." title="Type in a name">
    
    <table id="myTable">
      <tr class="header">
        <th style="width:60%;">Name</th>
        <th style="width:40%;">Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td><a href="https://germany.net/sweden">Germany</a></td>
      </tr>
      <tr>
        <td>Berglunds snabbkop</td>
        <td>Sweden</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Koniglich Essen</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Italy</td>
      </tr>
      <tr>
        <td>North/South</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Paris specialites</td>
        <td>France</td>
      </tr>
    </table>
    </body>
    </html>

    【讨论】:

      【解决方案2】:
      use textContent instead of innerHtml.
      

      function myFunction() {
            var input, filter, table, tr, td, i;
            input = document.getElementById("myInput");
            filter = input.value.toUpperCase();
            table = document.getElementById("myTable");
            tr = table.getElementsByTagName("tr");
            for (var i = 0; i < tr.length; i++) {
              var tds = tr[i].getElementsByTagName("td");
              var flag = false;
              for(var j = 0; j < tds.length; j++){
                var td = tds[j];
               console.log(td.textContent);
      
                if (td.textContent.toUpperCase().indexOf(filter) > -1) {
                  flag = true;
                } 
              }
              if(flag){
                  tr[i].style.display = "";
              }
              else {
                  tr[i].style.display = "none";
              }
            }
          }
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
          <html>
          <head>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          </head>
          <body>
          <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names then click to launch.." title="Type in a name">
      
          <table id="myTable">
            <tr class="header">
              <th style="width:60%;">Name</th>
              <th style="width:40%;">Country</th>
            </tr>
            <tr>
              <td>Alfreds Futterkiste</td>
              <td><a href="https://germany.net/sweden">Germany</a></td>
            </tr>
            <tr>
              <td>Berglunds snabbkop</td>
              <td>Sweden</td>
            </tr>
            <tr>
              <td>Island Trading</td>
              <td>UK</td>
            </tr>
            <tr>
              <td>Koniglich Essen</td>
              <td>Germany</td>
            </tr>
            <tr>
              <td>Laughing Bacchus Winecellars</td>
              <td>Canada</td>
            </tr>
            <tr>
              <td>Magazzini Alimentari Riuniti</td>
              <td>Italy</td>
            </tr>
            <tr>
              <td>North/South</td>
              <td>UK</td>
            </tr>
            <tr>
              <td>Paris specialites</td>
              <td>France</td>
            </tr>
          </table>
          </body>
          </html>

      【讨论】:

        【解决方案3】:

        问题在于使用 textContent 而不是 innerHTML

        这是现代版

        const search = document.getElementById("myInput");
        
        const toggle = () => {
          const text = search.value.toUpperCase();
          const rows = [...document.querySelectorAll("#myTable tr")]
        
          rows.forEach(tr => {
            const show = [...tr.querySelectorAll("td")].filter(td => {
              return td.textContent.toUpperCase().includes(text)
            }).length > 0
        
            tr.classList.toggle("hide", text && !show)
          })
        }
        
        search.addEventListener('keydown', ({ key }) => {
          if (["Backspace", "Delete"].includes(key))  toggle()
        })
        
        search.addEventListener('input', toggle)
        .hide {
          display: none
        }
        <input type="text" id="myInput" autocomplete="off" placeholder="Search for names then click to launch.." title="Type in a name">
        
        <table>
          <thead>
            <tr class="header">
              <th style="width:60%;">Name</th>
              <th style="width:40%;">Country</th>
            </tr>
          </thead>
          <tbody id="myTable">
            <tr>
              <td>Alfreds Futterkiste</td>
              <td><a href="https://germany.net/sweden">Germany</a></td>
            </tr>
            <tr>
              <td>Berglunds snabbkop</td>
              <td>Sweden</td>
            </tr>
            <tr>
              <td>Island Trading</td>
              <td>UK</td>
            </tr>
            <tr>
              <td>Koniglich Essen</td>
              <td>Germany</td>
            </tr>
            <tr>
              <td>Laughing Bacchus Winecellars</td>
              <td>Canada</td>
            </tr>
            <tr>
              <td>Magazzini Alimentari Riuniti</td>
              <td>Italy</td>
            </tr>
            <tr>
              <td>North/South</td>
              <td>UK</td>
            </tr>
            <tr>
              <td>Paris specialites</td>
              <td>France</td>
            </tr>
          </tbody>
        </table>

        【讨论】:

          猜你喜欢
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-12
          • 2021-04-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多