javascript - 过滤表格时丢失标题行 (java/html)

标签 javascript html css

我注意到,在过滤我制作的自定义表格时,我丢失了允许用户对我的表格进行排序的第一个标题行。有人知道我的代码有什么问题吗? (下面的片段是第一行)

<th onclick="sortTable(0)"style="background-color:DeepSkyBlue;">Name</th>
<th onclick="sortTable(1)"style="background-color:DeepSkyBlue;">Year</th>
<th onclick="sortTable(2)"style="background-color:DeepSkyBlue;">State</th>

上面的代码片段是最上面/第一行,一旦文本输入被放入搜索框,它就会消失。 (我省略了一些代码,因为我知道问题出在下面的脚本中)

下面有两个脚本,上面是对所有列进行排序的能力,而不仅仅是第一个。底部脚本是输入文本/过滤。

<script>
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc"; 
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.getElementsByTagName("TR");
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /*check if the two rows should switch place,
      based on the direction, asc or desc:*/
      if (dir == "asc") {
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      //Each time a switch is done, increase this count by 1:
      switchcount ++;      
    } else {
      /*If no switching has been done AND the direction is "asc",
      set the direction to "desc" and run the while loop again.*/
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}
</script>
<script>
function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i;
  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
  for (i = 0; i < tr.length; i++) {
    if (!tr[i].classList.contains('header')) {
      td = tr[i].getElementsByTagName("td"),
      match = false;
      for (j = 0; j < td.length; j++) {
        if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
          match = true;
          break;
        }
      }
      if (!match) {
        tr[i].style.display = "none";
      } else {
        tr[i].style.display = "";
      }
    }
  }
}
</script>

最佳答案

根据这个测试

if (!tr[i].classList.contains('header')) {

你的 header 应该有一个带有关键字“header”的class属性

<th class="header"                   <- this is missing?
    onclick="sortTable(0)"
    style="background-color:DeepSkyBlue;">Name</th>

您也可以在以 i = 1 开始的其他循环中使用相同的技巧。

关于javascript - 过滤表格时丢失标题行 (java/html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47914749/

相关文章:

java - 使用javascript更改网页元素的支持bean的值?

Html 文本区域不可点击,无法在其中输入文本

javascript - 如何使用 onclick 按钮使矩形消失并重新出现

capitalization - 文本转换 : capitalize issue in IE

javascript - 使用带有数据库 javascript MySQL PHP 坐标的 google maps api 创建线

javascript - 使用 JavaScript 使文本框可编辑

html - 如何在 HTML 电子邮件中的 anchor 标记旁边添加图标?

JavaScript:在 div.container < window.height 时做一些事情

css - 标签背景颜色滑动过渡到下一个标签

javascript - html5 canvas - 更新文本