[英]How to filter table values from search input noinpepend of lower or upper case jQuery
I working with table data filtering, and I have issues.我使用表数据过滤,我遇到了问题。
At this moment I create a script which is filtering data depends on user typing in the search field, but problem is, in case if a user will write the same keywords, for example, "Apple" but with lower case "apple" , the filter will not work.此时我创建了一个脚本,该脚本根据用户在搜索字段中的输入来过滤数据,但问题是,如果用户编写相同的关键字,例如"Apple"但使用小写"apple" ,则过滤器将不起作用。
My code:我的代码:
$(document).ready(function () {
$('.search').keyup(function () {
searchTable($(this).val());
});
});
function searchTable(inputVal) {
var table = $("#fruit-table");
table.find('tr').each(function (index, row) {
var allCells = $(row).find('td');
if (allCells.length > 0) {
var found = false;
allCells.each(function (index, td) {
var regExp = new RegExp(inputVal, 'i');
if (regExp.test($(td).text())) {
found = true;
return false;
}
});
if (found == true) $(row).show();
else $(row).hide();
}
});
}
But I want some flexible approach, which are will filter independently if the user will type from the upper or lower case, and in the end, he will get the same result.但我想要一些灵活的方法,如果用户从大写或小写输入,它们将独立过滤,最后,他会得到相同的结果。
Try this solution.试试这个解决方案。
function searchTable(inputVal) {
var table = $("#fruit-table");
table.find('tr').each(function (index, row) {
var allCells = $(row).find('td');
if (allCells.length > 0) {
var found = false;
allCells.each(function (index, td) {
inputVal = inputVal.toLowerCase();
var regExp = new RegExp(inputVal, 'i');
var tableData = $(td).text().toLowerCase();
if (regExp.test(tableData)) {
found = true;
return false;
}
});
if (found == true) $(row).show();
else $(row).hide();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.