-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
74 lines (66 loc) · 2.2 KB
/
main.js
File metadata and controls
74 lines (66 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let table = $(`<table>`);
let tableBody = $(`<tbody>`);
$.each(data[0], function (key, value) {
if (typeof value !== 'object') {
th = $(`<th>`);
th.append(`<th>${key}</th>`).css({
'border': '1px solid black',
'font-size': '22px',
'background-color': 'rgba(0,0,0, 0.1)',
'position': 'relative',
'min-width': '55px'
});
}
table.append(th);
});
$('body').append(table);
$.each(data, function (key, elem) {
tr = $(`<tr>`);
$.each(elem, function (index, value) {
if (typeof value !== 'object') {
td = $(`<td>${value}</td>`).css('border', '1px solid black')
tr.append(td);
}
});
tableBody.append(tr);
table.append(tableBody);
});
$('body').append(table);
$('th').click(function () {
$('th').not($(this)).removeClass();
$(this).removeClass('desc');
$(this).addClass('asc');
let rows = table.find('tr').toArray().sort(sortRows($(this).index()));
this.asc = !this.asc
if (!this.asc) {
$(this).removeClass('asc');
$(this).addClass('desc');
rows = rows.reverse()
}
for (let i = 0; i < rows.length; i += 1) {
table.append(rows[i])
}
})
function sortRows(index) {
return function (a, b) {
let userA = $(a).children('td').eq(index).text()
let userB = $(b).children('td').eq(index).text()
return $.isNumeric(userA) && $.isNumeric(userB) ? userA - userB : userA.toString().localeCompare(userB)
}
}
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('.back').addEventListener('click', function () {
history.back()
});
document.querySelector('.go').addEventListener('click', function () {
history.go(1)
});
});
document.addEventListener('click', function (event) {
if (event.target.tagName == 'TH') {
let initUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
let sortUrl = initUrl + `?sortBy=${event.target.textContent}`;
history.pushState(null, null, sortUrl);
event.preventDefault();
}
});