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

问题描述 投票:0回答:1

我注意到,当我通过自定义表进行过滤时,我正在丢失允许用户对表进行排序的第一个标题行。谁知道我的代码有什么问题? (以下代码片段是第一行)

<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>

上面的代码段是文本输入放入搜索框后消失的顶行/第一行。 (我遗漏了一些代码,因为我知道问题出现在下面的脚本中)

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

<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>
javascript html css
1个回答
0
投票

根据这个测试

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

您的标题应具有带有关键字“标题”的class属性

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

您也可以使用与i = 1开头的另一个循环相同的技巧。

© www.soinside.com 2019 - 2024. All rights reserved.