gpt4 book ai didi

javascript - 在 HTML 和 Javascript 中对表格进行排序 - 第一列的问题

转载 作者:行者123 更新时间:2023-12-01 02:34:00 25 4
gpt4 key购买 nike

我使用此代码创建了一个可以排序的表: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 元素(这是您的第二列)。您可以将 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];

关于javascript - 在 HTML 和 Javascript 中对表格进行排序 - 第一列的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48083316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com