当前位置:网站首页>JS table sorting
JS table sorting
2022-04-22 02:54:00 【The_ Caibe】
//1. Look up the form <th> Elements , Make them clickable
function makeSortable(table) {
var headers = document.getElementsByTagName("th");
for (var i = 1; i < headers.length; i++) {
//1-1. Traverse line header
(function (n) {
var flag = false;
headers[n].onclick = function () {
//1-2 If a line header is clicked
//1-3. Get the sort of data in the table
// sortrows(table,n);
// console.log(headers[n])
var tbody = document.getElementsByTagName("tbody")[0];// obtain <tbody> first
// console.log(tbody)
var rows = tbody.getElementsByTagName("tr");// obtain tbody All rows in
// var rows = $('#tb').bootstrapTable('getData');
rows = Array.prototype.slice.call(rows, 0);// Snapshot in real array -- All right rows Convert tags in to array format
// Based on the first n individual <td> The value of the element sorts the rows
rows.sort(function (row1, row2) {
var cell1 = row1.getElementsByTagName("td")[n];// For the first n A cell
var cell2 = row2.getElementsByTagName("td")[n];
console.log("rows",rows,"row1-2",row1,row2,"cell1-2",cell1,cell2)
var val1 = cell1.textContent || cell1.innerText;// Get text content
var val2 = cell2.textContent || cell2.innerText;
if (!isNaN(parseFloat(val1))) {
//parseFloat() Judge whether it is a number character by character , If the first is not a return NaN(Not a Number)
// Otherwise, return the number up to the end or before the non numeric position , And change the type to numeric type
val1 = parseFloat(val1);
}
if (!isNaN(parseFloat(val2))) {
val2 = parseFloat(val2);
}
if (val1 < val2) {
return -1;
} else if (val1 > val2) {
return 1;
} else {
return 0;
}
});
var ielm = headers[n].getElementsByTagName("i")[0] // 1-4. The top and bottom labels of the line are changed
if (flag) {
$(ielm).removeClass("glyphicon glyphicon-chevron-up pull-right");
$(ielm).addClass("glyphicon glyphicon-chevron-down pull-right"); // toggleClass If there is, remove , Add if not
rows.reverse(); // Click again to call rows
} else {
$(ielm).removeClass("glyphicon glyphicon-chevron-down pull-right");
$(ielm).addClass("glyphicon glyphicon-chevron-up pull-right");
}
//1-5. stay tbody Add rows to the end in their order
// This will automatically remove them from their current position , So there is no need to delete them in advance
// If <tbody> It also includes <tr> Any other element of , These nodes will float to the top position
for (var j = 0; j < rows.length; j++) {
tbody.appendChild(rows[j]);
}
//2-1. Table alternate color
var tab = document.getElementById("tb");
var len = tab.tBodies[0].rows.length;
for (var k = 0; k < len; k++) {
if (k % 2 === 0) {
tab.tBodies[0].rows[k].style.backgroundColor = "rgba(255,255,255,0.52)";
} else {
tab.tBodies[0].rows[k].style.backgroundColor = "rgba(218,217,217,0.2)";
}
}
flag = !flag;
}
}(i));
}
}
window.onload = function () {
var table = document.getElementsByTagName("table")[0];
makeSortable(table);
//3-1. Select all button
var all_c = document.getElementById("all")
var checke = document.getElementsByClassName("c")
all_c.onchange = function () {
for (var i = 0; i < checke.length; i++) {
checke[i].checked = all_c.checked
}
}
}
The reference is reprinted by other leaders Code , Then on this basis, I found parseFloat(), It optimizes the small defect that the original numeric type is compared as string type
版权声明
本文为[The_ Caibe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220248300287.html
边栏推荐
- Analysis on the development status of meta universe
- MODEM拨号放音,原来写在大富翁,汇总过来。
- After the asynchronous operation, let the subsequent code of await continue to execute
- How to solve the deadlock problem in MySQL?
- Flutter03 dart asynchronous
- Apple watch theme picture crawl!
- 收藏的技巧链接
- IIS提示:“此网站要求您登录”的解决办法。
- class数组的类似常量定义
- [TIANTI competition] l2-040 Zhezhi game (25 points (s)) (simulation)
猜你喜欢

刷题计划——动态规划dynamic programming(一)

WSOLA principle and MATLAB simulation

C language character classification and conversion

金融信息安全实训——22/4/19(上)

(进阶)C函数调用

Flutter04-Widget初体验
![[TIANTI competition] l2-040 Zhezhi game (25 points (s)) (simulation)](/img/5f/151c6e9cc68a1f28621187a0c925db.png)
[TIANTI competition] l2-040 Zhezhi game (25 points (s)) (simulation)

Regression prediction | MATLAB realizes Bayes Gru (Bayes optimized gating cycle unit) multiple input and single output
![[➾ leetcode Sword means offer 12. Path in Matrix (simple)]](/img/88/2c1c7a9954d824495f0b99223ce7fc.png)
[➾ leetcode Sword means offer 12. Path in Matrix (simple)]

How to provide CPU branch prediction efficiency at the code level
随机推荐
[※ leetcode refers to the path in offer 12. Matrix (simple)]
72. Edit distance
Development management · Huawei IPD
Web网站访问响应慢可能的原因
Softmax operation
How to solve the deadlock problem in MySQL?
Analysis on the development status of meta universe
MySQL execution process
C language character classification and conversion
Buy a Tencent cloud server to play
身份认证和访问控制
Performance test and comparison of oceanbase and MariaDB using dbt-3
When encountering a strange problem, start both local and remote transactions at the same time. The remote transaction is SQL2000. It is sql2008. No error is reported, but the data cannot be written
Delphi automatically adapts to the screen size
Excel tips - vlookup automatic matching
[Xiao Yang takes you to play with C language] circular structure (detailed explanation)
Is it safe to open an account online? How to open an account better?
In depth study of MySQL
Why does MySQL index use B + tree instead of jump table?
Digital twin tank warfare, technology promotes battlefield information digitization