gpt4 book ai didi

javascript - 尝试使用 w3.js 对表进行排序,但它没有正确对数字进行排序

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

我正在排序的表格如下。

<!DOCTYPE html>
<html>

<title>W3.JS</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://www.w3schools.com/lib/w3.js"></script>
<body class="w3-container">

<h2>Testing W3.JS with CSS</h2>

<h2>Sort Tables</h2>

<p>Click the table headers to sort the table accordingly:</p>

<table id="myTable" class="w3-table-all">
<tr>
<th onclick="w3.sortHTML('#myTable', '.item', 'td:nth-child(1)')" style="cursor:pointer">Name</th>
<th onclick="w3.sortHTML('#myTable', '.item', 'td:nth-child(2)')" style="cursor:pointer">Random Number</th>
</tr>
<tr class="item">
<td>Berglunds snabbköp</td>
<td>6</td>
</tr>
<tr class="item">
<td>North/South</td>
<td>1</td>
</tr>
<tr class="item">
<td>Alfreds Futterkiste</td>
<td>2</td>
</tr>
<tr class="item">
<td>Königlich Essen</td>
<td>5</td>
</tr>
<tr class="item">
<td>Magazzini Alimentari Riuniti</td>
<td>4</td>
</tr>
<tr class="item">
<td>Paris spécialités</td>
<td>10</td>
</tr>
<tr class="item">
<td>Island Trading</td>
<td>6</td>
</tr>
<tr class="item">
<td>Laughing Bacchus Winecellars</td>
<td>102</td>
</tr>
</table>

</body>
</html>

似乎进行排序的代码部分来自:https://www.w3schools.com/lib/w3.js

w3.sortHTML = function(id, sel, sortvalue) {
var a, b, i, ii, y, bytt, v1, v2, cc, j;
a = w3.getElements(id);
for (i = 0; i < a.length; i++) {
for (j = 0; j < 2; j++) {
cc = 0;
y = 1;
while (y == 1) {
y = 0;
b = a[i].querySelectorAll(sel);
for (ii = 0; ii < (b.length - 1); ii++) {
bytt = 0;
if (sortvalue) {
v1 = b[ii].querySelector(sortvalue).innerHTML.toLowerCase();
v2 = b[ii + 1].querySelector(sortvalue).innerHTML.toLowerCase();
} else {
v1 = b[ii].innerHTML.toLowerCase();
v2 = b[ii + 1].innerHTML.toLowerCase();
}
if ((j == 0 && (v1 > v2)) || (j == 1 && (v1 < v2))) {
bytt = 1;
break;
}
}
if (bytt == 1) {
b[ii].parentNode.insertBefore(b[ii + 1], b[ii]);
y = 1;
cc++;
}
}
if (cc > 0) {break;}
}
}
};

如何修改它以便正确对数字进行排序?

最佳答案

问题是 w3.sortHTML通过 (v1 > v2) 排序和(v1 < v2) ,其中v1v2每个文本字符串 - 当然,不会考虑数字字符串并按照您期望的方式对它们进行排序。假设你不想猴子补丁 w3.sortHTML ,您可以创建自己的函数来执行类似的操作,但使用 localeComparenumeric选项:

const myTable = document.querySelector('#myTable');
// select all trs below the header:
const trs = [...myTable.querySelectorAll('tr')].slice(1);

myTable.addEventListener('click', ({ target }) => {
if (!target.matches('th')) return;
const thIndex = Array.prototype.indexOf.call(target.parentElement.children, target);
const getText = tr => tr.children[thIndex].textContent;
trs.sort((a, b) => getText(a).localeCompare(getText(b), undefined, { numeric: true }));
trs.forEach(tr => myTable.appendChild(tr));
});
<table id="myTable" class="w3-table-all">
<tr>
<th style="cursor:pointer">Name</th>
<th style="cursor:pointer">Random Number</th>
</tr>
<tr class="item">
<td>Berglunds snabbköp</td>
<td>6</td>
</tr>
<tr class="item">
<td>North/South</td>
<td>1</td>
</tr>
<tr class="item">
<td>Alfreds Futterkiste</td>
<td>2</td>
</tr>
<tr class="item">
<td>Königlich Essen</td>
<td>5</td>
</tr>
<tr class="item">
<td>Magazzini Alimentari Riuniti</td>
<td>4</td>
</tr>
<tr class="item">
<td>Paris spécialités</td>
<td>10</td>
</tr>
<tr class="item">
<td>Island Trading</td>
<td>6</td>
</tr>
<tr class="item">
<td>Laughing Bacchus Winecellars</td>
<td>102</td>
</tr>
</table>

要在单击两次后反向排序,请使用一个持久对象来跟踪最后排序的顺序:

const myTable = document.querySelector('#myTable');
// select all trs below the header:
const trs = [...myTable.querySelectorAll('tr')].slice(1);
const sortAscending = {
0: 1,
1: 1
};
myTable.addEventListener('click', ({ target }) => {
if (!target.matches('th')) return;
const thIndex = Array.prototype.indexOf.call(target.parentElement.children, target);
const getText = tr => tr.children[thIndex].textContent;
trs.sort((a, b) => (
getText(a).localeCompare(getText(b), undefined, { numeric: true })
* sortAscending[thIndex]
));
trs.forEach(tr => myTable.appendChild(tr));
sortAscending[thIndex] *= -1;
});
<table id="myTable" class="w3-table-all">
<tr>
<th style="cursor:pointer">Name</th>
<th style="cursor:pointer">Random Number</th>
</tr>
<tr class="item">
<td>Berglunds snabbköp</td>
<td>6</td>
</tr>
<tr class="item">
<td>North/South</td>
<td>1</td>
</tr>
<tr class="item">
<td>Alfreds Futterkiste</td>
<td>2</td>
</tr>
<tr class="item">
<td>Königlich Essen</td>
<td>5</td>
</tr>
<tr class="item">
<td>Magazzini Alimentari Riuniti</td>
<td>4</td>
</tr>
<tr class="item">
<td>Paris spécialités</td>
<td>10</td>
</tr>
<tr class="item">
<td>Island Trading</td>
<td>6</td>
</tr>
<tr class="item">
<td>Laughing Bacchus Winecellars</td>
<td>102</td>
</tr>
</table>

关于javascript - 尝试使用 w3.js 对表进行排序,但它没有正确对数字进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53202862/

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