在HTML&中排序表Javascript - 第一栏的问题

时间:2018-01-03 18:42:03

标签: javascript html sorting html-table

我使用此代码创建了一个我可以排序的表:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_table_desc

我的问题是第2,3和4列的排序没有问题,但不是第一列。基本上,如果我按照示例,它总是下一列被排序而不是我点击的那一列。所以我使用n-1而不是n来调整脚本以过滤正确的列,但这阻止我过滤第一个。我无法弄清楚这个问题来自何处。

<div>

    <table id="myTable">
            <colgroup>
           <col span="1" style="width: 45%;">
           <col span="1" style="width: 20%;">
           <col span="1" style="width: 20%;">
           <col span="1" style="width: 20%;">
            </colgroup>
      <tr>
       <!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->  
        <th onclick="sortTable(0)">Scenario</th>
        <th onclick="sortTable(0)">Delta returns</th>
        <th onclick="sortTable(1)">Sensitivity</th>
        <th onclick="sortTable(2)">Delta volatility</th>
      </tr>
      <tr>
        <th>Scenario 1</th>
        <td>2</td>
        <td>3</td>
        <td>4</td>
      </tr>
      <tr>
        <th>Scenario 2</th>
        <td>7</td>
        <td>1</td>
        <td>5</td>
      </tr>
      <tr>
        <th>Scenario 3</th>
        <td>5</td>
        <td>1</td>
        <td>0</td>
      </tr>
      <tr>
        <th>Scenario 4</th>
        <td>0</td>
        <td>2</td>
        <td>7</td>
      </tr>

    </table>

</div>

脚本。

<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-1];
      y = rows[i + 1].getElementsByTagName("TD")[n-1];
      /*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>

和相关的css:

table {
    border-collapse: collapse;
    border-spacing: 0;
    height:400px;
    border: 1px solid #ddd;
    overflow-y: scroll;
    overflow-x: scroll;
    display: block;
    font-family: helvetica;
    font-size:12px;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even){background-color: #002560;
    color:white;
}

th {
    cursor: pointer;
}

2 个答案:

答案 0 :(得分:2)

您正在使用getElementsByTagName,但每行的第一个单元格是TH元素。 TH不会被考虑在内,因此您无法对其进行过滤,并且您的索引偏离1,因为第0列将是第一个TD元素(这是您的第二列)。您可以将TH替换为TD,并使用普通索引(n代替n - 1)。

或者您可以使用children代替getElementsByTagName,这将返回所有孩子,无论他们的标签名称是什么。在这里,使用普通索引(第一列为0,第二列为1,等等)。

https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children

像这样:

x = rows[i].children[n];
y = rows[i + 1].children[n];

答案 1 :(得分:0)

尝试从0开始,而不是1。