asp.net 我如何过滤一个表的多列onkeyup与java脚本

e4eetjau  于 2023-03-31  发布在  .NET
关注(0)|答案(3)|浏览(114)

我试图通过多个列来过滤一个表onkeyup与java脚本我能够做到这一点时,我指定的列索引只有一个td一样说

td = tr[i].getElementsByTagName("td")[1];

但过滤器不工作,当我尝试添加anothor列索引一样,说

td = tr[i].getElementsByTagName("td")[1][2];

问题是,我不知道如何通过为同一树指定两个或多个列索引来做到这一点
这是我的密码

6rvt4ljy

6rvt4ljy1#

好了,这是代码:
//遍历所有表行,隐藏不匹配查询的行

for (i = 0; i < tr.length; i++)
{

   td = tr[i].getElementsByTagName("td"); //Watch carefully this

 for(j=0;j<td.length;++j) // new loop
       {
          console.log(td[j]);
            if (td[j]) { //Change over here
              txtValue = td[j].textContent || td[j].innerText;
              console.log(txtValue);
              if (txtValue.toUpperCase().indexOf(filter) > -1) {
                 tr[i].style.display = "";
              } else {
                 tr[i].style.display = "none";
                }
           }
      }
}

希望有帮助。

rur96b6h

rur96b6h2#

我认为你应该为tds加上另一个循环,就像你在trs上做的那样

function myFunction() {
  // Declare variables
    var input, filter, table, tr, td, i, txtValue;
    input = document.getElementById('<%=txtsearch1.ClientID%>');
    filter = input.value.toUpperCase();
    table = document.getElementById('<%=mygrid.ClientID%>');
    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++) {
        for (j = 0; j < tr[i].getElementsByTagName("td").length; j++) {
            td = tr[i].getElementsByTagName("td")[j];

            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
 }
xjreopfe

xjreopfe3#

稍微调整Altaf Khokhar的答案,使用正确的隐藏值,并在找到命中时快速转义。我发现我的头部丢失了,所以我必须将tr索引偏移1。

function myFunction() {
      // Declare variables
      var input, filter, table, tr, td, i, txtValue, filterelement;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      table = document.getElementById("myTable");
      tr = table.getElementsByTagName("tr");

      // Loop through all table rows, and hide those who don't match the search query
      // Should be offset 1 as first index is the header
      for (i = 1; i < tr.length; i++) {
        filterelement = true;

        for (j = 0; j < tr[i].getElementsByTagName("td").length; j++) {
            td = tr[i].getElementsByTagName("td")[j];
            txtValue = td.textContent || td.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                filterelement = false;
                break;
            }
        }
        if (filterelement) {
            tr[i].style.display = "none";
        } else {
            tr[i].style.display = "";
        }
      }
    };

相关问题