-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (86 loc) · 2.51 KB
/
script.js
File metadata and controls
104 lines (86 loc) · 2.51 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
94
95
96
97
98
99
100
101
102
103
104
document.addEventListener('DOMContentLoaded', init, false);
let data, table, sortCol;
let sortAsc = false;
const pageSize = 5;
let curPage = 1;
async function init() {
// Select the table (well, tbody)
table = document.querySelector('#table tbody');
// get the users
let resp = await fetch('https://64743e827de100807b1a84ab.mockapi.io/api/v1/leaderboard/users');
data = await resp.json();
renderTable();
// listen for sort clicks
document.querySelectorAll('#table thead tr th').forEach(t => {
t.addEventListener('click', sort, false);
});
document.querySelector('#nextButton').addEventListener('click', nextPage, false);
document.querySelector('#prevButton').addEventListener('click', previousPage, false);
}
function renderTable() {
// create html
let result = '';
data.filter((row, i) => {
let start = (curPage-1)*pageSize;
let end =curPage*pageSize;
if(i >= start && i < end) return true;
}).forEach((user, i) => {
result += `<tr>
<td>${i+1}</td>
<td>${user.name}</td>
<td>${user.country}</td>
<td>${user.score}</td>
<td><img src="${user.photo}"</td>
</tr>`;
});
table.innerHTML = result;
}
function previousPage() {
if(curPage > 1) curPage--;
renderTable();
}
function nextPage() {
if((curPage * pageSize) < data.length) curPage++;
renderTable();
}
// sort functionality
function sort(e) {
let thisSort = e.target.dataset.sort;
if(sortCol === thisSort) sortAsc = !sortAsc;
sortCol = thisSort;
console.log('sort dir is ', sortAsc);
data.sort((a, b) => {
if(a[sortCol] < b[sortCol]) return sortAsc?1:-1;
if(a[sortCol] > b[sortCol]) return sortAsc?-1:1;
return 0;
});
renderTable();
}
// search Functionality
function search() {
let input, filter, table, tr, td, i, txtValue
input = document.getElementById('search')
filter = input.value.toUpperCase()
table = document.getElementById('table')
tr = table.getElementsByTagName('tr')
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td')[1]
if (td) {
txtValue = td.textContent || td.innerText
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = ""
}
else {
tr[i].style.display = "none"
}
}
}
}
// <!-- Hamburger Menu -->
const btn = document.getElementById('menu-btn')
const nav = document.getElementById('menu')
btn.addEventListener('click', () => {
btn.classList.toggle('open')
nav.classList.toggle('flex')
nav.classList.toggle('hidden')
})