[英]Get Id of Row that Matches Regex with data attribute
I have a function that: 我有一个功能:
rtnData
array consisting of what I believe is a row that has a data-attr matching the approved regex. 创建一个rtnData
数组,该数组由我认为是具有匹配批准的正则表达式的数据属性的行组成。 What I want is just the id's of the matching rows rather than what rtnData
is doing. 我想要的只是匹配行的ID,而不是rtnData
在做什么。
function filterPay(val){
// target Rows
$(".loading tr").hide().filter(function() {
var rtnData = "";
//make approved regex based on given value
var payApproved= new RegExp(val.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join('|'), 'i');
//make rtnData array? consisting of rows that match regex
rtnData = (
$(this).attr("data-payment").match(payApproved)
);
return rtnData;
}).fadeIn("fast");
}
//I know this refers to the unique Id of each row but don't know how to
// chain it with the match selection
console.log($(this).attr('data-row'));
So instead of showing the rows is it possible to just get the id's and push them row into a new array, because after this I have to match that array to another filter array and only show the rows where the 2 arrays have matching id's. 因此,除了显示行外,还可以获取ID并将其推入新数组,因为在此之后,我必须将该数组与另一个过滤器数组进行匹配,并且仅显示2个数组具有匹配ID的行。
Your code filters a list of tr
to those whose data-payment
is matched by the regular expression. 您的代码将tr
列表过滤为那些data-payment
与正则表达式匹配的tr
列表。 You now only need to do a map
on the results and extract the id. 现在,您只需要对结果进行map
并提取ID。
Also you should not create the regular expression inside the filter
since it is not affected by each tr
另外,您不应该在filter
内创建正则表达式,因为它不受每个tr
影响
function filterPay(val) {
var payApproved = new RegExp(val.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join('|'), 'i');
// target Rows
var rows = $(".loading tr").hide().filter(function() {
// only use test if the reguar expression does not have the global flag set
return payApproved.test($(this).attr("data-payment"));
}).fadeIn("fast");
var ids = rows.get().map(function(row){return row.id});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.