-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.html
More file actions
142 lines (122 loc) · 3.12 KB
/
table.html
File metadata and controls
142 lines (122 loc) · 3.12 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
table {
border: 2px solid #3b9393;
}
thead th {
background-color: #54d2d2;
color: #fff;
cursor: pointer;
}
thead {
display: block;
}
tbody {
display: block;
max-height: 500px;
overflow-y: scroll;
}
td, th {
width: 40px;
height: 40px;
background-color: #d9ffff;
text-align: center;
border: 1px solid #ddd;
color: #735e3b;
font-family: Roboto;
}
</style>
</head>
<body>
<form action="#">
<label for="row">
Row
<input type="text" name="row" class="row" id="row" value="1000">
</label>
<label for="col">
Column
<input type="text" name="col" class="col" id="col" value="16">
</label>
<button type="submit" class="create">Create</button>
</form>
<br>
<table class="table">
<thead></thead>
<tbody></tbody>
</table>
</body>
<script>
const table = document.querySelector('.table');
const form = document.querySelector('form');
const thead = document.querySelector('thead');
const tbody = document.querySelector('tbody');
let data = [];
let sortedDate = [];
let isSorted = false;
let sortCol = null;
let lastRenderedRow = 0;
let row = 0;
let col = 0;
generateData();
renderHeader();
renderData(data, lastRenderedRow, 100);
form.addEventListener('submit', e => {
e.preventDefault();
generateData();
tbody.textContent = '';
tbody.scrollTop = 0;
lastRenderedRow = 0;
renderHeader();
renderData(data, lastRenderedRow, 100);
});
tbody.addEventListener('scroll', renderNewRows);
function generateData() {
row = form.elements.row.value;
col = form.elements.col.value;
data = Array.from({ length: row }, () => Array.from({ length: col}, () => randNum(1, 1000)));
}
function renderHeader() {
thead.innerHTML = '<tr>'
+ Array.from({ length: col }, (v, i) => `<th class="sort-col" data-col="${i}">${i}</th>`).join('')
+ '</tr>';
document.querySelectorAll('.sort-col').forEach(c => c.addEventListener('click', sortDataByCol));
}
function renderData(data, minRow, maxRow) {
for (let r = minRow; r < row && r < maxRow; r++) {
const rowElement = document.createElement('tr');
for (let c = 0; c < col; c++) {
const colElement = document.createElement('td');
colElement.textContent = data[r][c];
rowElement.appendChild(colElement);
}
tbody.appendChild(rowElement);
}
lastRenderedRow = maxRow;
}
function renderNewRows(e) {
if (tbody.scrollTop + tbody.clientHeight === tbody.scrollHeight) {
if (isSorted)
renderData(sortedData, lastRenderedRow, lastRenderedRow + 100);
else
renderData(data, lastRenderedRow, lastRenderedRow + 100);
}
}
function sortDataByCol(e) {
const c = this.dataset.col;
isSorted = true;
sortCol = c;
sortedData = data.sort((a, b) => a[c] - b[c]);
tbody.textContent = '';
tbody.scrollTop = 0;
lastRenderedRow = 0;
renderData(sortedData, lastRenderedRow, 100);
}
function randNum(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
</script>
</html>