javascript-引导表过滤器-没有结果时如何添加“找不到匹配的记录”行?
作者:互联网
我使用以下代码片段http://jsfiddle.net/giorgitbs/52aK9/1/:
$(document).ready(function () {
(function ($) {
$('#filter').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('.searchable tr').hide();
$('.searchable tr').filter(function () {
return rex.test($(this).text());
}).show();
})
}(jQuery));
});
这很好.但是,当没有结果而不是空白表时,如何显示包含文本“找不到匹配的记录”的行?
解决方法:
我会做这样的事情.首先,我向该表添加(默认情况下为隐藏)行,以不显示任何数据.然后在jQuery中,检查可见的行数.如果为0,则显示隐藏的行.
$(document).ready(function () {
(function ($) {
$('#filter').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('.searchable tr').hide();
$('.searchable tr').filter(function () {
return rex.test($(this).text());
}).show();
$('.no-data').hide();
if($('.searchable tr:visible').length == 0)
{
$('.no-data').show();
}
})
}(jQuery));
});
.no-data {
display: none;
text-align: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group"> <span class="input-group-addon">Filter</span>
<input id="filter" type="text" class="form-control" placeholder="Type here...">
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Code</th>
<th>Name</th>
<th>Default</th>
<th>Status</th>
</tr>
</thead>
<tbody class="searchable">
<tr class="no-data">
<td colspan="4">No data</td>
</tr>
<tr>
<td>EUR</td>
<td>EURO</td>
<td></td>
<td>Active</td>
</tr>
<tr>
<td>GBP</td>
<td>Pound</td>
<td></td>
<td>Active</td>
</tr>
<tr>
<td>GEL</td>
<td>Georgian Lari</td>
<td><span class="glyphicon glyphicon-ok"></span>
</td>
<td>Active</td>
</tr>
<tr>
<td>USD</td>
<td>US Dollar</td>
<td></td>
<td>Active</td>
</tr>
</tbody>
</table>
标签:twitter-bootstrap,javascript 来源: https://codeday.me/bug/20191121/2050051.html