如果搜索不匹配,如何隐藏 table?
how to hide table if search not match?
如果 table 搜索匹配隐藏所有 tr
元素,table
或 thead
应该自动隐藏。
我试过这个方法,但是不行。
if ($("table tr:visible").length === 1) {
$("tbody").addClass('display-hide');
}
或
$(document).each(function () {
if ($("table tr:visible").length === 1) {
$("tbody").addClass('display-hide');
}
})
演示:https://jsfiddle.net/abilashu/m5k7av21/13/
如果可以,请给个demo。
您可以使用 each
循环遍历 tables,然后使用 $(this).find("tbody tr:visible").length
获取 table 内可见 trs 的长度,然后根据长度 addClass
或 removeClass
来自 tbody 或 thead。
演示代码 :
class tableLiveSearch {
constructor(table, searchable) {
this.table = table;
this.searchable = searchable;
this.hide_row_list = new Array();
}
updateTable(search_query) {
this.hide_row_list = this.searchTable(search_query);
this.showAllTableRows();
this.hideTableRows();
}
searchTable(search_query) {
var temporary_list = new Array();
var $searchable_items = $(this.table + ' ' + this.searchable);
$searchable_items.each(function(index, value) {
var $this_element = $(this);
search_query = search_query.toLowerCase();
var search_content = $this_element.text().toLowerCase();
if (search_content.indexOf(search_query) == -1)
temporary_list.push($this_element.parent());
});
return temporary_list;
}
showAllTableRows() {
$(this.table + ' ' + this.searchable).each(function(index, value) {
$(this).parent().show();
});
}
hideTableRows() {
$.each(this.hide_row_list, function(index, value) {
$(this).hide();
});
}
}
var searchtable = new tableLiveSearch('.search-table', '.searchable');
$('#search').keyup(function() {
searchtable.updateTable($(this).val());
//remove class from tbody and thead
$("thead , tbody").removeClass('display-hide')
//loop through table
$("table.search-table").each(function() {
//check if the length is (hide or show
$(this).find("tbody tr:visible").length == 0 ? $(this).find("thead , tbody").addClass('display-hide') : $(this).find("thead , tbody").removeClass('display-hide')
})
});
.display-hide {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container mt-5">
<div class="row mt-5">
<div class="col-12 mt-4">
<form class="form">
<input class="form-control" id="search" type="search" placeholder="Search a name..." aria-label="Search">
</form>
</div>
</div>
<div class="row mt-4">
<div class="col">
<table class="table search-table">
<thead>
<tr>
<th>Item Name</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td class="searchable">Whosebug</td>
<td>33</td>
</tr>
<tr>
<td class="searchable">Google</td>
<td>2</td>
</tr>
<tr>
<td class="searchable">Twitter</td>
<td>2</td>
</tr>
</tbody>
</table>
<table class="table search-table">
<thead>
<tr>
<th>Item Name</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td class="searchable">Apple</td>
<td>345</td>
</tr>
<tr>
<td class="searchable">Banana</td>
<td>34</td>
</tr>
<tr>
<td class="searchable">Orange</td>
<td>87</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
tr
元素,table
或 thead
应该自动隐藏。
我试过这个方法,但是不行。
if ($("table tr:visible").length === 1) {
$("tbody").addClass('display-hide');
}
或
$(document).each(function () {
if ($("table tr:visible").length === 1) {
$("tbody").addClass('display-hide');
}
})
演示:https://jsfiddle.net/abilashu/m5k7av21/13/
如果可以,请给个demo。
您可以使用 each
循环遍历 tables,然后使用 $(this).find("tbody tr:visible").length
获取 table 内可见 trs 的长度,然后根据长度 addClass
或 removeClass
来自 tbody 或 thead。
演示代码 :
class tableLiveSearch {
constructor(table, searchable) {
this.table = table;
this.searchable = searchable;
this.hide_row_list = new Array();
}
updateTable(search_query) {
this.hide_row_list = this.searchTable(search_query);
this.showAllTableRows();
this.hideTableRows();
}
searchTable(search_query) {
var temporary_list = new Array();
var $searchable_items = $(this.table + ' ' + this.searchable);
$searchable_items.each(function(index, value) {
var $this_element = $(this);
search_query = search_query.toLowerCase();
var search_content = $this_element.text().toLowerCase();
if (search_content.indexOf(search_query) == -1)
temporary_list.push($this_element.parent());
});
return temporary_list;
}
showAllTableRows() {
$(this.table + ' ' + this.searchable).each(function(index, value) {
$(this).parent().show();
});
}
hideTableRows() {
$.each(this.hide_row_list, function(index, value) {
$(this).hide();
});
}
}
var searchtable = new tableLiveSearch('.search-table', '.searchable');
$('#search').keyup(function() {
searchtable.updateTable($(this).val());
//remove class from tbody and thead
$("thead , tbody").removeClass('display-hide')
//loop through table
$("table.search-table").each(function() {
//check if the length is (hide or show
$(this).find("tbody tr:visible").length == 0 ? $(this).find("thead , tbody").addClass('display-hide') : $(this).find("thead , tbody").removeClass('display-hide')
})
});
.display-hide {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container mt-5">
<div class="row mt-5">
<div class="col-12 mt-4">
<form class="form">
<input class="form-control" id="search" type="search" placeholder="Search a name..." aria-label="Search">
</form>
</div>
</div>
<div class="row mt-4">
<div class="col">
<table class="table search-table">
<thead>
<tr>
<th>Item Name</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td class="searchable">Whosebug</td>
<td>33</td>
</tr>
<tr>
<td class="searchable">Google</td>
<td>2</td>
</tr>
<tr>
<td class="searchable">Twitter</td>
<td>2</td>
</tr>
</tbody>
</table>
<table class="table search-table">
<thead>
<tr>
<th>Item Name</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td class="searchable">Apple</td>
<td>345</td>
</tr>
<tr>
<td class="searchable">Banana</td>
<td>34</td>
</tr>
<tr>
<td class="searchable">Orange</td>
<td>87</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>