-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcars-sort-table.html
135 lines (128 loc) · 3.56 KB
/
cars-sort-table.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<title>Cars</title>
<style>script src="https://www.w3schools.com/lib/w3.js"></script>
w3.sortHTML(selector)
<h1> Cars </h1>
<table {
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th {
cursor: pointer;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
</style>
</head>
<body>
<p><strong>Click the headers to sort the table.</strong></p>
<p>The first time you click, the sorting direction is ascending (A to Z).</p>
<p>Click again, and the sorting direction will be descending (Z to A):</p>
<table id="Uncommon">
<tr>
<!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->
<th> Image </th>
<th onclick="sortTable(0)">Year</th>
<th onclick="sortTable(1)">Make</th>
<th onclick="sortTable(2)">Model</th>
<th onclick="sortTable(3)">Description</th>
<th onclick="sortTable(4)">Date (YYYY/MM/DD)</th>
<th onclick="sortTable(5)">Location</th>
<th onclick="sortTable(6)">Camera</th>
</tr>
<tr>
<td>IMAGE</td>
<td>2018</td>
<td>Volkswagen</td>
<td>Atlas</td>
<td>Testing</td>
<td>2018/06/23</td>
<td>Dufferin</td>
<td>Moto E4</td>
</tr>
<tr>
<td>IMAGE</td>
<td>2005</td>
<td>Volkswagen</td>
<td>Passat</td>
<td>Testing</td>
<td>2008/01/28</td>
<td>Major Mack</td>
<td>Moto E4</td>
</tr>
<tr>
<td>IMAGE</td>
<td>1956</td>
<td>Chevrolet</td>
<td>Imapala</td>
<td>Testing</td>
<td>2010/12/03</td>
<td>Finch</td>
<td>iPod Touch</td>
</tr>
</table>
<script>
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("Uncommon");
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.rows;
/*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];
y = rows[i + 1].getElementsByTagName("TD")[n];
/*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>
</body>
</html>