-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortable.js
More file actions
51 lines (44 loc) · 2.2 KB
/
sortable.js
File metadata and controls
51 lines (44 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// JavaScript code for table sorting
document.addEventListener("DOMContentLoaded", function () {
const tables = document.querySelectorAll(".sortable-table");
tables.forEach(table => {
const headers = table.querySelectorAll(".sortable");
headers.forEach(header => {
header.addEventListener("click", () => {
const type = header.getAttribute("data-type");
const column = header.cellIndex;
const rows = Array.from(table.querySelectorAll("tbody tr"));
const ascending = header.getAttribute("data-order") === "asc";
rows.sort((a, b) => {
let aValue = a.cells[column].textContent.trim();
let bValue = b.cells[column].textContent.trim();
// Convert values based on data type
switch (type) {
case "text":
aValue = aValue.toLowerCase();
bValue = bValue.toLowerCase();
break;
case "number":
aValue = parseFloat(aValue);
bValue = parseFloat(bValue);
break;
// Add additional cases for other data types as needed
default:
break;
}
if (ascending) {
return aValue > bValue ? 1 : -1;
} else {
return aValue < bValue ? 1 : -1;
}
});
const tbody = table.querySelector("tbody");
tbody.innerHTML = '';
rows.forEach(row => tbody.appendChild(row));
header.setAttribute("data-order", ascending ? "desc" : "asc");
headers.forEach(header => header.classList.remove("asc", "desc"));
header.classList.add(ascending ? "asc" : "desc");
});
});
});
});