I have list of filter table with jQuery. If user is searching the data that existed in the table it will filter and displayed the data. But if user search data that not existed in the data I want to display the not found
. How to I displayed the not found
in the filter function ?
JavaScript
x
8
1
$(document).ready(function(){
2
$("#myInput").on("keyup", function() {
3
var value = $(this).val().toLowerCase();
4
$("#myTable tr").filter(function() {
5
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
6
});
7
});
8
});
JavaScript
1
15
15
1
table {
2
font-family: arial, sans-serif;
3
border-collapse: collapse;
4
width: 100%;
5
}
6
7
td, th {
8
border: 1px solid #dddddd;
9
text-align: left;
10
padding: 8px;
11
}
12
13
tr:nth-child(even) {
14
background-color: #dddddd;
15
}
JavaScript
1
36
36
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<input id="myInput" type="text" placeholder="Search..">
4
<br><br>
5
6
<table>
7
<thead>
8
<tr>
9
<th>Firstname</th>
10
<th>Lastname</th>
11
<th>Email</th>
12
</tr>
13
</thead>
14
<tbody id="myTable">
15
<tr>
16
<td>John</td>
17
<td>Doe</td>
18
<td>john@example.com</td>
19
</tr>
20
<tr>
21
<td>Mary</td>
22
<td>Moe</td>
23
<td>mary@mail.com</td>
24
</tr>
25
<tr>
26
<td>July</td>
27
<td>Dooley</td>
28
<td>july@greatstuff.com</td>
29
</tr>
30
<tr>
31
<td>Anja</td>
32
<td>Ravendale</td>
33
<td>a_r@test.com</td>
34
</tr>
35
</tbody>
36
</table>
Advertisement
Answer
JavaScript
1
28
28
1
$(document).ready(function(){
2
// select notFound row
3
var notFound = $("#notFound")
4
// make it hidden by default
5
notFound.hide()
6
7
$("#myInput").on("keyup", function() {
8
var value = $(this).val().toLowerCase()
9
10
// select all items
11
var allItems = $("#myTable tr")
12
13
// get list of matched items, (do not toggle them)
14
var matchedItems = $("#myTable tr").filter(function() {
15
return $(this).text().toLowerCase().indexOf(value) > -1
16
});
17
18
// hide all items first
19
allItems.toggle(false)
20
21
// then show matched items
22
matchedItems.toggle(true)
23
24
// if no item matched then show notFound row, otherwise hide it
25
if (matchedItems.length == 0) notFound.show()
26
else notFound.hide()
27
});
28
});
JavaScript
1
15
15
1
table {
2
font-family: arial, sans-serif;
3
border-collapse: collapse;
4
width: 100%;
5
}
6
7
td, th {
8
border: 1px solid #dddddd;
9
text-align: left;
10
padding: 8px;
11
}
12
13
tr:nth-child(even) {
14
background-color: #dddddd;
15
}
JavaScript
1
37
37
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<input id="myInput" type="text" placeholder="Search..">
4
<br><br>
5
6
<table>
7
<thead>
8
<tr>
9
<th>Firstname</th>
10
<th>Lastname</th>
11
<th>Email</th>
12
</tr>
13
</thead>
14
<tbody id="myTable">
15
<tr id="notFound"><td colspan="3">Not Found</td></tr>
16
<tr>
17
<td>John</td>
18
<td>Doe</td>
19
<td>john@example.com</td>
20
</tr>
21
<tr>
22
<td>Mary</td>
23
<td>Moe</td>
24
<td>mary@mail.com</td>
25
</tr>
26
<tr>
27
<td>July</td>
28
<td>Dooley</td>
29
<td>july@greatstuff.com</td>
30
</tr>
31
<tr>
32
<td>Anja</td>
33
<td>Ravendale</td>
34
<td>a_r@test.com</td>
35
</tr>
36
</tbody>
37
</table>