I’m trying to hide the whole div(where there’s a table inside) when the result from filtering/searching thru the table is empty.
So far, I have this:
JavaScript
x
134
134
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
5
<meta name="viewport" content="width=device-width, initial-scale=1">
6
<style>
7
* {
8
box-sizing: border-box;
9
}
10
11
#myInput {
12
background-image: url('/css/searchicon.png');
13
background-position: 10px 10px;
14
background-repeat: no-repeat;
15
width: 100%;
16
font-size: 16px;
17
padding: 12px 20px 12px 40px;
18
border: 1px solid #ddd;
19
margin-bottom: 12px;
20
}
21
22
.mytable {
23
border-collapse: collapse;
24
width: 100%;
25
border: 1px solid #ddd;
26
font-size: 18px;
27
}
28
29
.mytable th, .mytable td {
30
text-align: left;
31
padding: 12px;
32
}
33
34
.mytable tr {
35
border-bottom: 1px solid #ddd;
36
}
37
38
.mytable tr.header, .mytable tr:hover {
39
background-color: #f1f1f1;
40
}
41
</style>
42
</head>
43
<body>
44
45
<h2>My Customers</h2>
46
47
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
48
<div id="myTable1div">
49
<table id="myTable" class="mytable" data-name="mytable">
50
<tr class="header">
51
<th style="width:60%;">Name</th>
52
<th style="width:40%;">Country</th>
53
</tr>
54
<tr>
55
<td>Alfreds Futterkiste</td>
56
<td>Germany</td>
57
</tr>
58
<tr>
59
<td>Berglunds snabbkop</td>
60
<td>Sweden</td>
61
</tr>
62
<tr>
63
<td>Island Trading</td>
64
<td>UK</td>
65
</tr>
66
<tr>
67
<td>Laughing Bacchus Winecellars</td>
68
<td>Canada</td>
69
</tr>
70
</table>
71
</div>
72
<br><br>
73
<div id="myTable1div">
74
<table id="myTable" class="mytable" data-name="mytable">
75
<tr class="header">
76
<th style="width:60%;">Name</th>
77
<th style="width:40%;">Country</th>
78
</tr>
79
<tr>
80
<td>Alfreds Futterkiste</td>
81
<td>Germany</td>
82
</tr>
83
<tr>
84
<td>Berglunds snabbkop</td>
85
<td>Sweden</td>
86
</tr>
87
<tr>
88
<td>Island Trading</td>
89
<td>UK</td>
90
</tr>
91
</table>
92
</div>
93
<script>
94
function myFunction() {
95
var input, filter, table, tr, td, i,alltables;
96
alltables = document.querySelectorAll("table[data-name=mytable]");
97
input = document.getElementById("myInput");
98
filter = input.value.toUpperCase();
99
alltables.forEach(function(table){
100
tr = table.getElementsByTagName("tr");
101
for (i = 0; i < tr.length; i++) {
102
td = tr[i].getElementsByTagName("td")[0];
103
if (td) {
104
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
105
tr[i].style.display = "";
106
} else {
107
tr[i].style.display = "none";
108
}
109
}
110
}
111
});
112
}
113
</script>
114
115
<! ––hide div that doesnt have any data––>
116
<script>
117
$(function(){
118
var hide = true;
119
120
$('#myTable td').each(function(){
121
var td_content = $(this).text();
122
123
if(td_content!=""){
124
hide = false;
125
}
126
})
127
128
if(hide){
129
$('#myTable1div').hide();
130
}
131
})
132
</script>
133
</body>
134
</html>
As you can see, although the table is properly searched, the div and the table that has no value in its <td>
is still showing.
How can I hide the whole div if the table’s <td>
is empty while searching it?
Advertisement
Answer
Your first problem is that you had several elements with the same ID, e.g. <div id="myTable1div">
. I had to give them each a separate ID.
I created a helper function, hideDivs()
, which counts the number of rows which are set to display: none
, and if there is just one of them (i.e. the header), then hide that corresponding <div>
.
JavaScript
1
137
137
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
6
<meta name="viewport" content="width=device-width, initial-scale=1">
7
<style>
8
* {
9
box-sizing: border-box;
10
}
11
12
#myInput {
13
background-image: url('/css/searchicon.png');
14
background-position: 10px 10px;
15
background-repeat: no-repeat;
16
width: 100%;
17
font-size: 16px;
18
padding: 12px 20px 12px 40px;
19
border: 1px solid #ddd;
20
margin-bottom: 12px;
21
}
22
23
.mytable {
24
border-collapse: collapse;
25
width: 100%;
26
border: 1px solid #ddd;
27
font-size: 18px;
28
}
29
30
.mytable th,
31
.mytable td {
32
text-align: left;
33
padding: 12px;
34
}
35
36
.mytable tr {
37
border-bottom: 1px solid #ddd;
38
}
39
40
.mytable tr.header,
41
.mytable tr:hover {
42
background-color: #f1f1f1;
43
}
44
</style>
45
</head>
46
47
<body>
48
49
<h2>My Customers</h2>
50
51
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
52
<div id="myTable1div">
53
<table id="myTable" class="mytable" data-name="mytable">
54
<tr class="header">
55
<th style="width:60%;">Name</th>
56
<th style="width:40%;">Country</th>
57
</tr>
58
<tr>
59
<td>Alfreds Futterkiste</td>
60
<td>Germany</td>
61
</tr>
62
<tr>
63
<td>Berglunds snabbkop</td>
64
<td>Sweden</td>
65
</tr>
66
<tr>
67
<td>Island Trading</td>
68
<td>UK</td>
69
</tr>
70
<tr>
71
<td>Laughing Bacchus Winecellars</td>
72
<td>Canada</td>
73
</tr>
74
</table>
75
</div>
76
<br><br>
77
<div id="myTable2div">
78
<table id="myTable2" class="mytable" data-name="mytable">
79
<tr class="header">
80
<th style="width:60%;">Name</th>
81
<th style="width:40%;">Country</th>
82
</tr>
83
<tr>
84
<td>Alfreds Futterkiste</td>
85
<td>Germany</td>
86
</tr>
87
<tr>
88
<td>Berglunds snabbkop</td>
89
<td>Sweden</td>
90
</tr>
91
<tr>
92
<td>Island Trading</td>
93
<td>UK</td>
94
</tr>
95
</table>
96
</div>
97
<script>
98
function hideDivs() {
99
['#myTable1div', '#myTable2div'].forEach(function (id) {
100
// From https://stackoverflow.com/a/5325109/378779
101
if ($(id + ' tr:not([style*="display: none"])').length == 1) {
102
$(id).hide();
103
} else {
104
$(id).show();
105
}
106
})
107
}
108
109
function myFunction() {
110
var input, filter, table, tr, td, i, alltables;
111
alltables = document.querySelectorAll("table[data-name=mytable]");
112
input = document.getElementById("myInput");
113
filter = input.value.toUpperCase();
114
alltables.forEach(function (table) {
115
tr = table.getElementsByTagName("tr");
116
for (i = 0; i < tr.length; i++) {
117
td = tr[i].getElementsByTagName("td")[0];
118
if (td) {
119
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
120
tr[i].style.display = "";
121
} else {
122
tr[i].style.display = "none";
123
}
124
}
125
}
126
});
127
128
hideDivs();
129
}
130
131
// hide div that doesn't have any data
132
hideDivs();
133
</script>
134
135
</body>
136
137
</html>