jQuery 过滤器,带有使用数据类型的下拉框
jQuery filter with drop down box using data-type
我需要使用带有jQuery的下拉框过滤表的行。我不知道该怎么做的是如何将选项的值与表行的数据类型相关联。
.HTML:
<label for="filter">Type:</label>
<select id="filter>
<option value="all">All Steps</option>
<option value="standard">Standards</option>
<option value="review">Reviews</option>
</select>
<table id="table>
<tr id="one" class="row" data-type="standard">
<td>Standard</td>
</tr>
<tr id="one" class="row" data-type="review">
<td>Reviews</td>
</tr>
</table>
.JS:
$("#filter").change(function () {
$("#table").find(data-type).each(function () {
if ($(this).text() != $("#filter").val())
$(this).hide();
else
$(this).parent().show();
if ($("#filter").val() == "all")
$(this).show();
});
});
这里的jQuery只是根据我迄今为止通过研究发现的内容拼凑而成的。将数据类型属性保留在表行中非常重要。
我对jQuery很陌生,所以任何事情都有帮助!
下面是代码笔:http://codepen.io/aburnsworth/pen/XKzgqa?editors=1111
您可以使用.val();
找到选择的值
您将获得该.val()
与$('.row');
匹配所需的所有行
找到匹配项时循环所有行隐藏所有,然后才显示你想要的,b/c电脑做这个这么快似乎瞬间
.each(function(index, element) {});
现在你有一个过滤器
编辑:只需将隐藏全部移动到循环之外即可。
$(".filter").change(function() {
var filterValue = $(this).val();
var row = $('.row');
row.hide()
row.each(function(i, el) {
if($(el).attr('data-type') == filterValue) {
$(el).show();
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label for="filter">Type:</label>
<select class="filter" data-tableId="table1">
<option value="all">All Steps</option>
<option value="standard">Standards</option>
<option value="review">Reviews</option>
<option value="inspection">Inspections</option> <option value="payment">Payments</option>
<option value="document">Documents</option>
</select>
<table id="table1">
<tr id="one" class="row" data-type="standard">
<td>Standard</td>
</tr>
<tr id="two" class="row" data-type="review">
<td>Review</td>
</tr>
<tr id="three" class="row" data-type="inspection">
<td>Inspections</td>
</tr>
<tr id="four" class="row" data-type="payment">
<td>Payments</td>
</tr>
<tr id="five" class="row" data-type="document">
<td>Documents</td>
</tr>
<tr id="one" class="row" data-type="standard">
<td>Standard</td>
</tr>
<tr id="two" class="row" data-type="review">
<td>Review</td>
</tr>
<tr id="three" class="row" data-type="inspection">
<td>Inspections</td>
</tr>
<tr id="four" class="row" data-type="payment">
<td>Payments</td>
</tr>
<tr id="five" class="row" data-type="document">
<td>Documents</td>
</table>
您可以将 js 函数修改为
$(document).ready(function() {
var $rows = $('table tr');
$("#filter").change(function() {
var val = '^(?=.*''b' + $.trim($(this).val()).split(/'s+/).join('''b)(?=.*''b') + ').*$',
reg = RegExp(val, 'i'),
text;
if ($(this).val() !== 'all') {
$rows.show().filter(function() {
text = $(this).data('type').replace(/'s+/g, ' ');
return !reg.test(text);
}).hide();
} else {
$rows.show();
}
});
});
请看这个工作小提琴。
除了@wlin的答案对于下拉列表中的"全部"值。
$("#filter").change(function() {
console.clear();
var filterValue = $(this).val();
var row = $('.row');
row.each(function(i, el) {
if ($(el).attr('data-type') == filterValue) {
row.hide()
$(el).show();
}
});
// In Addition to Wlin's Answer (For "All" value)
if ("all" == filterValue) {
row.show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label for="filter">Type:</label>
<select id="filter">
<option value="all">All Steps</option>
<option value="standard">Standards</option>
<option value="review">Reviews</option>
<option value="inspection">Inspections</option>
<option value="payment">Payments</option>
<option value="document">Documents</option>
</select>
<table id="table">
<tr id="one" class="row" data-type="standard">
<td>Standard</td>
</tr>
<tr id="two" class="row" data-type="review">
<td>Review</td>
</tr>
<tr id="three" class="row" data-type="inspection">
<td>Inspections</td>
</tr>
<tr id="four" class="row" data-type="payment">
<td>Payments</td>
</tr>
<tr id="five" class="row" data-type="document">
<td>Documents</td>
</tr>
</table>
相关文章:
- javascript解释器如何理解变量的数据类型
- 从查询字符串参数推断出正确的数据类型
- 为什么不'我们在javascript中使用函数参数的数据类型
- 基于数据类型的编辑框的汇总列表
- 如何降低此函数的复杂性,该函数根据数据类型进行回调
- 比较和分配数据类型的困难
- 在javascript中调用函数/对象引用时,可容纳任何数据类型
- E4X中是否有一种方法可以将基元数据类型的XMLList转换为类似于Array.join()的Array
- Highcharts系列.数据数据类型限制
- 如何在php循环中显示隐藏的数据类型
- 如何从标签数据类型=“;无线电”;
- 如何使用 Java 脚本从 csv 文件中查找每列的数据类型
- 在 Javascript 中更改数据类型
- 抽象数据类型等效于类型
- JavaScript中的“符号”原始数据类型是什么?
- 数据类型Jasmine
- 有没有一种方法可以在Typescript中自动检查数据类型的数据相等性
- 了解JavaScript中的基元数据类型
- 角度:搜索器(过滤器)根据数据类型工作不同
- jQuery 过滤器,带有使用数据类型的下拉框