2016-06-22 9 views
-2

私は二つのフィルターのためのjQueryのセレクタジェネレータを作成しようとしている - 狙いはちょうどカレンダーのいくつかの要素をhiddingされる -マージjqueryのセレクタ[javascriptの文字列の連結]

私はDB_ID 2グループにフィルタを適用した場合私はpersonn DB_IDをフィルタ場合#mod_calendar .gid_2

:私はグループDB_ID 2でフィルタリングした場合、私は、次のセレクタを生成したい #mod_calendar .gid_2.pid_1

:とpersonn DB_ID 1に私は次のセレクタを生成したいと思い1次のセレクタを生成したい:#mod_calendar .pid_1

Iグループ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

Iがグループにフィルタ場合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

だから1は、私はあなたがポイントを得ると思います... この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コード:私はこの正確なexemple、これら二つの配列で取得したい何

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

私がコメントで言ったように、私はbの交差点を持っていたいと思いますフィルタ。私の問題を理解する人のために

+0

私はあなたが何を求めているのか、何が問題なのかは分かりません。フィドラーには2つの文字列を連結する1行のコードがあり、私たちは何をしたいのですか?質問をより明確にし、より多くのコードと特定の問題を表示できますか? – Esko

+0

@Esko私の投稿を編集しました – Su4p

答えて

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/

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/