合并jquery选择器[javascript string concat]

时间:2016-06-22 12:54:00

标签: javascript jquery selector string-concatenation

我试图为我的两个过滤器创建一个jquery-selector生成器 - 目的只是隐藏日历中的一些元素 -

如果我过滤组DB_ID 2和personn DB_ID 1,我想生成以下选择器: #mod_calendar .gid_2.pid_1

如果我过滤组DB_ID 2,我想生成以下选择器:#mod_calendar .gid_2

如果我在personn DB_ID 1上过滤我想生成以下选择器:#mod_calendar .pid_1

如果我过滤组DB_ID 2,5,6,8和personn DB_ID 1,我想生成以下选择器: #mod_calendar .gid_2.pid_1,#mod_calendar .gid_5.pid_1,#mod_calendar .gid_6.pid_1,#mod_calendar .gid_8.pid_1

如果我过滤组DB_ID 2,5,6,8和personn DB_ID 1,2,我想生成以下选择器: #mod_calendar .gid_2.pid_1,#mod_calendar .gid_5.pid_1,#mod_calendar .gid_6.pid_1,#mod_calendar .gid_8.pid_1#mod_calendar .gid_2.pid_2,#mod_calendar .gid_5.pid_2,#mod_calendar .gid_6.pid_2,#mod_calendar .gid_8.pid_2

所以,我认为你明白了...... 这个https://jsfiddle.net/5mr60f6p/1是我到目前为止所尝试的,但我现在有点卡住了。

[编辑]

<div id="mod_calendar">
<div class="pid_1 gid_1">pid_1 gid_1 [do not match gid_filter should not be shown]</div>
<div class="pid_2 gid_2">pid_2 gid_2 [do not match pid_filter should not be shown]</div>
<div class="pid_5">pid_5 [do not match gid_filter should not be shown]</div>
<div class="pid_5 gid_2">pid_5 gid_2 [match gid_filter and pid_filter should be shown]</div>
</div>

JS代码:

var pid_filter= [5,32,56,8,4];
var gid_filter=[2,5];
function generate_filter_selector(prefix,values){
    return prefix+values.join(','+prefix);
}
console.log($(generate_filter_selector('#mod_calendar .pid_',test)).show());
//I would like the intersection of both selector and not like I did one after the other.
console.log($(generate_filter_selector('#mod_calendar .gid_',test2)).show());

我想用这个确切的例子来获得这两个数组:

$("#mod_calendar pid_5.gid_2,#mod_calendar pid_32.gid_2,#mod_calendar pid_56.gid_2,#mod_calendar pid_8.gid_2,#mod_calendar pid_4.gid_2,#mod_calendar pid_5.gid_5,#mod_calendar pid_32.gid_5,#mod_calendar pid_56.gid_5,#mod_calendar pid_8.gid_5,#mod_calendar pid_4.gid_5").show();

就像我在评论中所说的那样,我想要两个过滤器的交集。

2 个答案:

答案 0 :(得分:0)

对于那些了解我的问题的人,这是我的解决方案:

  function generate_jquery_selector_from_fitlers(filters){
  var number_of_values = 1;
  $.each( filters, function( index, selectors ) {
    number_of_values = number_of_values*selectors.length;
    return number_of_values;
  });
  filters_length = $.map( filters, function( selectors, i ) {
    return selectors.length;
  });
  var string_selector= '';
  for(var i =0; i < number_of_values;i++){
    var current = i;
    $.each( filters, function( index, selectors ) {
      var ind = parseInt(current % filters_length[index]);
      var ligne = selectors[ ind ];
      current = parseInt(current / filters_length[index]);
      string_selector += '.' + ligne;
    });
    string_selector += ',';
  }
  return string_selector.substring(0, string_selector.length - 1);
}

这是jsfiddle https://jsfiddle.net/skncsvw0/

答案 1 :(得分:0)

甚至更快:

function generate_jquery_selector_from_fitlers(filters,prefix,item_prefix){
        prefix = prefix || '';
        item_prefix = item_prefix || '.';
        return prefix+item_prefix+allPossibleCases(filters).join(' ,'+prefix+item_prefix);
        function allPossibleCases(arr) {
            if (arr.length == 1) {
                return arr[0];
            }
            else {
                var result = [];
                var allCasesOfRest = allPossibleCases(arr.slice(1));
                for (var i = 0; i < allCasesOfRest.length; i++) {
                    for (var j = 0; j < arr[0].length; j++) {
                        result.push(arr[0][j] +'.'+ allCasesOfRest[i]);
                    }
                }
                return result;
            }
        }

    };

jsfiddle:https://jsfiddle.net/prazj7qp/