-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (43 loc) · 1.17 KB
/
app.js
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
const table = document.getElementById("grid");
const tbody = table.querySelector("tbody");
const btn = document.getElementById("btn");
function sortData(e) {
let th = e.target;
if (th.tagName !== "TH") return;
e.target.classList.toggle("bg");
console.log(th.cellIndex);
console.log(th.dataset.type);
compareData(th.cellIndex, th.dataset.type);
}
function compareData(colNum, type) {
let rowArray = Array.from(tbody.rows);
console.log(rowArray);
switch (type) {
case "number":
rowArray.sort(
(a, b) => a.cells[colNum].innerHTML - b.cells[colNum].innerHTML
);
break;
case "string":
rowArray.sort((a, b) =>
a.cells[colNum].innerHTML - b.cells[colNum].innerHTML ? 1 : -1
);
break;
}
tbody.append(...rowArray);
}
table.addEventListener("click", sortData);
function inputData(e) {
const age = document.querySelector("#inputAge");
const name = document.querySelector("#inputName");
if (!age.value && !name.value) return false;
tbody.innerHTML += `
<tr>
<td>${age.value}</td>
<td>${name.value}</td>
</tr>
`;
age.value = "";
name.value = "";
}
btn.addEventListener("click", inputData);