Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,98 +1,99 @@
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid black;
padding: 4px;
}
th {
cursor: pointer;
}
th:hover {
background: yellow;
}
</style>
</head>

<head>
<meta charset="utf-8">
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 4px;
}
th {
cursor: pointer;
}
th:hover {
background: yellow;
}
</style>
</head>

<body>
<body>
<table id="grid">
<thead>
<tr>
<th data-type="number">Age</th>
<th data-type="string">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td>John</td>
</tr>
<tr>
<td>2</td>
<td>Pete</td>
</tr>
<tr>
<td>11</td>
<td>Ann</td>
</tr>
<tr>
<td>9</td>
<td>Eugene</td>
</tr>
<tr>
<td>1</td>
<td>Ilya</td>
</tr>
</tbody>
</table>

<table id="grid">
<thead>
<tr>
<th data-type="number">Age</th>
<th data-type="string">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td>John</td>
</tr>
<tr>
<td>2</td>
<td>Pete</td>
</tr>
<tr>
<td>12</td>
<td>Ann</td>
</tr>
<tr>
<td>9</td>
<td>Eugene</td>
</tr>
<tr>
<td>1</td>
<td>Ilya</td>
</tr>
</tbody>
</table>
<script>
grid.onclick = function (e) {
if (e.target.tagName != "TH") return;

<script>
let th = e.target;
// TH이면 분류하세요
// cellIndex is the number of th:
// 0 for the first column
// 1 for the second column, etc
sortGrid(th.cellIndex, th.dataset.type);
};

grid.onclick = function(e) {
if (e.target.tagName != 'TH') return;
function sortGrid(colNum, type) {
let tbody = grid.querySelector("tbody");

let th = e.target;
// if TH, then sort
// cellIndex is the number of th:
// 0 for the first column
// 1 for the second column, etc
sortGrid(th.cellIndex, th.dataset.type);
};
let rowsArray = Array.from(tbody.rows);

function sortGrid(colNum, type) {
let tbody = grid.querySelector('tbody');
// compare(a, b) compares two rows, need for sorting
let compare;

let rowsArray = Array.from(tbody.rows);
switch (type) {
case "number":
compare = function (rowA, rowB) {
return (
rowA.cells[colNum].innerHTML - rowB.cells[colNum].innerHTML
);
};
break;
case "string":
compare = function (rowA, rowB) {
return rowA.cells[colNum].innerHTML > rowB.cells[colNum].innerHTML
? 1
: -1;
};
break;
}

// compare(a, b) compares two rows, need for sorting
let compare;
// sort
rowsArray.sort(compare);

switch (type) {
case 'number':
compare = function(rowA, rowB) {
return rowA.cells[colNum].innerHTML - rowB.cells[colNum].innerHTML;
};
break;
case 'string':
compare = function(rowA, rowB) {
return rowA.cells[colNum].innerHTML > rowB.cells[colNum].innerHTML ? 1 : -1;
};
break;
tbody.append(...rowsArray);
}

// sort
rowsArray.sort(compare);

tbody.append(...rowsArray);
}
</script>

</body>
</script>
</body>
</html>