简体   繁体   English

获取与正则表达式匹配的行ID与data属性

[英]Get Id of Row that Matches Regex with data attribute

I have a function that: 我有一个功能:

  1. filters tr rows 过滤tr行
  2. Makes an approved regex based on the value passed into the function 根据传递给函数的值制作批准的正则表达式
  3. Makes an 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM