-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·93 lines (89 loc) · 3.2 KB
/
index.html
File metadata and controls
executable file
·93 lines (89 loc) · 3.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Sort Test with Minified Script</title>
<!-- 引入編譯過後的 tableSort.min.js 文件 -->
<script src="./dist/tableSort.min.js" defer></script>
<style>
/* 基本表格樣式 */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
cursor: pointer;
background-color: #f2f2f2;
}
th[aria-sort="ascending"]::after {
content: " 🔼";
}
th[aria-sort="descending"]::after {
content: " 🔽";
}
</style>
</head>
<body>
<!-- 排序按鈕 -->
<button id="sortButton">Sort by Age</button>
<button id="sortDateButton">Sort by Date of Birth</button>
<!-- 可排序的表格 -->
<table class="table-sortable" id="testTable">
<thead>
<tr>
<th data-sort-method="string">Name</th>
<th data-sort-method="number">Age</th>
<th data-sort-method="date">Date of Birth</th>
</tr>
</thead>
<tbody class="table-body">
<tr class="table-row">
<td>John Doe</td>
<td>25</td>
<td data-sort="2023-01-15">15 Jan 2023</td>
</tr>
<tr class="table-row">
<td>Jane Smith</td>
<td>30</td>
<td data-sort="2022-05-10">10 May 2022</td>
</tr>
<tr class="table-row">
<td>Alice Johnson</td>
<td>28</td>
<td data-sort="2021-08-22">22 Aug 2021</td>
</tr>
<tr class="table-row">
<td>Bob Brown</td>
<td>35</td>
<td data-sort="2020-03-17">17 Mar 2020</td>
</tr>
</tbody>
</table>
<script>
document.addEventListener("DOMContentLoaded", function() {
// 初始化 TableSort,並使用 tableSort.min.js 提供的 TableSort 類
const tableSortInstance = new tableSortjs.TableSort(document.getElementById("testTable"));
// 設置按鈕事件監聽器來觸發排序
document.getElementById("sortButton").addEventListener("click", function() {
const ageHeader = document.querySelector("#testTable th:nth-child(2)"); // 取得 "Age" 表頭
if (ageHeader) {
tableSortInstance.sortTable(ageHeader); // 使用 sortTable 方法進行排序
}
});
// 設置按鈕事件監聽器來觸發日期排序
document.getElementById("sortDateButton").addEventListener("click", function() {
const dateHeader = document.querySelector("#testTable th:nth-child(3)"); // 取得 "Date of Birth" 表頭
if (dateHeader) {
tableSortInstance.sortTable(dateHeader); // 使用 sortTable 方法進行排序
}
});
});
</script>
</body>
</html>