-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-table.html
78 lines (75 loc) · 2.42 KB
/
filter-table.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filter table</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
tr {
display: none;
}
tr.show {
display: table-row;
}
tr#table-headline {
display: table-row !important;
}
</style>
</head>
<body onload="pageLoaded()">
<div class="wrap">
<main role="main">
<div id="filter-container">
<input id="filter" type="text">
</div>
<table>
<thead>
<tr id="table-headline">
<th>Your</th>
<th>headlines</th>
</tr>
</thead>
<tbody>
<tr class="show">
<td>Your</td>
<td>content</td>
</tr>
</tbody>
</table>
</main>
</div>
<script type="text/javascript">
function pageLoaded() {
var filterInput = document.getElementById('filter');
// Set the cursor into the filter-<input>
filterInput.focus();
filterInput.select();
// Call filtering method on every keyup
filterInput.onkeyup = function() {
filterTable(filterInput.value);
}
// Filter the table for a given search-string
function filterTable(value) {
var rows = document.getElementsByTagName('tr'),
rowsLength = rows.length;
if (value == '') {
for (var i = 0; i < rowsLength; ++i) {
rows[i].className = 'show';
}
} else {
for (var i = 0; i < rowsLength; ++i) {
rows[i].className = '';
var tds = rows[i].getElementsByTagName('td'),
tdsLength = tds.length;
for (var tdsCounter = 0; tdsCounter < tdsLength; ++tdsCounter) {
if (tds[tdsCounter].innerText.indexOf(value) > -1) {
tds[tdsCounter].parentNode.className = 'show';
}
}
}
}
}
}
</script>
</body>
</html>