繁体   English   中英

用HTML和Javascript排序表格-第一栏的问题

[英]Sort Table in HTML & Javascript - issue with first column

我使用此代码创建了可以排序的表: 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;
}

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

或者,您可以使用children来代替getElementsByTagName ,这将返回所有子项,无论其标记名称是什么。 同样在这里,使用普通索引(第一列为0,第二列为1,依此类推)。

https://developer.mozilla.org/zh-CN/docs/Web/API/ParentNode/children

像这样:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM