合并jQuery选择器[JavaScript String Concat]



我正在尝试为我的两个过滤器做一个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是我到目前为止尝试的,但我目前有点陷入困境。

[edit]

<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();

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

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

  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/

甚至更快:

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/

最新更新