我制作了一个电话簿,供我内部使用。所有结果都显示在表格中。我以前只是有这个作为我的功能表上的筛选:使正则表达式仅基于单列筛选表
function LiveSearch() {
$('input#srch-field').on('keyup', function() {
var rex = new RegExp($(this).val(), 'i');
$('.srch-table tr').hide();
$('.srch-table tr').filter(function() {
return rex.test($(this).text());
}).show();
});
}
但我的用户是与此显然不高兴请求的功能,所以他们希望有一个过滤器来代替。所以我做了一个应该像过滤器一样的下拉菜单。然后我看着我的功能,并在想“我怎么能修改此,使它一般工作一样吗?”,所以我想出了这个:
function LiveSearch() {
$('input#srch-field').on('keyup', function() {
var rex = new RegExp($(this).val(), 'i');
var e = document.getElementById("srchFilter");
var filter = e.options[e.selectedIndex].value;
if (filter === 'all') {
$('.srch-table tr').hide();
$('.srch-table tr').filter(function() {
return rex.test($(this).text());
}).show();
} else {
$('.srch-table tr[id=' + filter + ']').hide();
$('.srch-table tr[id=' + filter + ']').filter(function() {
return rex.test($(this).text());
}).show();
}
});
}
的想法是,在我选择的每个值:
<div class="form-group">
<select id="srchFilter" class="form-control">
<option value="all" selected="selected">No Filter</option>
<option value="name">Name</option>
<option value="title">Title</option>
<option value="department">Department</option>
<option value="private-phone">Private Phone</option>
<option value="work-email">Work Email</option>
<option value="work-phone-land">Work Phone Landline</option>
<option value="work-phone-mobile">Work Phone Mobile</option>
</select>
</div>
对应于我的表中的列ID。但是如果我的过滤器只是all
,它根本不会做任何过滤。我可能会误解正则表达式的工作原理。任何人都可以点亮它吗?
编辑
的请求我的表的代码:
<div class="col-lg-6" id="customtable">
<table class="table table-striped" id="tablesorter">
<thead>
<tr>
<th class="col-xs-4" id="name">
Name<span class="glyphicon glyphicon-filter"></span>
</th>
<th class="col-xs-4" id="title">
Title<span class="glyphicon glyphicon-filter"></span>
</th>
<th class="col-xs-4" id="department">
Department<span class="glyphicon glyphicon-filter"></span>
</th>
<th style="display: none;" id="private-phone">
Private Phone
</th>
<th style="display: none;" id="work-email">
Work Email
</th>
<th style="display: none;" id="work-phone-land">
Work Phone Landline
</th>
<th style="display: none;" id="work-phone-mobile">
Work Phone Mobile
</th>
</tr>
</thead>
<tbody class="srch-table" id="srch-table">
<tr>
<td class="col-xs-12"><b>Please Wait...</b></td>
</tr>
</tbody>
</table>
<div class="fl-separator"></div>
</div>
请分享您的餐桌代码。 – 31piy
@ 31piy现在我已经添加了:) – OmniOwl