-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (59 loc) · 1.59 KB
/
script.js
File metadata and controls
64 lines (59 loc) · 1.59 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
var data = [];
function getDatafromApi() {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => response.json())
.then((json) => {
data = json;
constructHTML();
});
}
getDatafromApi();
function constructHTML() {
let html = "";
data = data.map((row, i) => {
row.render = `<tr class="table-row" id="row-${i}">
<td>${row.name}</td>
<td>${row.username}</td>
<td>${row.email}</td>
<td>${row.address.city}</td>
<td>${row.company.name}</td>
</tr>`;
html += row.render;
return row;
});
document.querySelector(".tbody").innerHTML = html;
}
function filter(el) {
const value = el.value.toLowerCase();
data.forEach((row, i) => {
if (!value) document.querySelector(`#row-${i}`).style.display = "";
else if (row.name.toLowerCase().includes(value)) {
document.querySelector(`#row-${i}`).style.display = "";
} else {
document.querySelector(`#row-${i}`).style.display = "none";
}
});
}
function sort() {
var filterTable, rows, sorted, i, x, y, sortFlag;
filterTable = document.querySelector(".table");
sorted = true;
while (sorted) {
sorted = false;
rows = filterTable.rows;
for (i = 1; i < rows.length - 1; i++) {
sortFlag = false;
x = rows[i].getElementsByTagName("td")[0];
y = rows[i + 1].getElementsByTagName("td")[0];
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
sortFlag = true;
break;
}
}
if (sortFlag) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
sorted = true;
}
}
}
function paginate() {}