[英]jQuery filter with drop down box using data-type
I need to filter through rows of a table using a drop-down box with jQuery. 我需要使用带有jQuery的下拉框来过滤表的行。 What I can't figure out how to do is how to relate the value of the options to the data-type of the table rows.
我无法弄清楚如何将选项的值与表行的数据类型联系起来。
HTML: 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: 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();
});
});
The jQuery here is just pieced together based off of what I've found so far by researching around. 这里的jQuery只是根据我迄今为止通过研究发现的内容拼凑而成。 It's important that I leave the data-type attribute in the table rows.
将data-type属性保留在表行中非常重要。
I'm pretty new to jQuery, so anything helps! 我是jQuery的新手,所以一切都有帮助!
Here's the Code Pen: http://codepen.io/aburnsworth/pen/XKzgqa?editors=1111 这是代码笔: http : //codepen.io/aburnsworth/pen/XKzgqa? editors = 1111
You find you what value is selected by using .val();
您可以使用
.val();
找到选择的值.val();
You get all the rows you need that .val()
to match against $('.row');
您将获得所需的所有行
.val()
以匹配$('.row');
Loop all the rows when you find a match hide all, only then show what you want, b/c computer do this so fast seems instant 当你找到一个匹配隐藏所有行时循环所有行,然后显示你想要的东西,b / c计算机这么快就看起来很快
.each(function(index, element) {});
Now you have a filter 现在你有一个过滤器
EDIT: just move the hide all outside of the loop should do it. 编辑:只需将循环移动到循环外部即可。
$(".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>
You can modify your js function to 您可以将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();
}
});
});
In Addition to @wlin's Answer For "All" value in dropdown. 除了@ 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.