こちらのソースを元に作成しているのですが、下記の条件に合わせてアレンジしようとしています。
「attribute」「type」のどちらにもそれぞれに対応する「すべて」をつけた場合、jqueryがうまくできませんでした。
https://jsfiddle.net/jwqyan5j/2/
・「attribute」「type」の2つの条件で絞り込むAND検索
・デフォルトで「attribute」「type」どちらも「すべて」にチェックがはいっている。
・リストには「attribute〇〇」と「type〇〇」の2つのクラスが入っている。
・「attribute01」にチェックを入れると「attribute01」のクラスがついたものだけが表示される。(type〇〇はすべて表示対象)
・「type01」にチェックを入れると「type01」のクラスがついたものだけが表示される。(attribute〇〇はすべて表示対象)
html
1<div id="search"> 2 <ul id="attribute"> 3 <li><label class="selected"><input type="checkbox" id="all_attribute" class="sort" checked="checked">すべて</label></li> 4 <li><label><input type="checkbox" id="attribute01" class="sort">attribute01</label></li> 5 <li><label><input type="checkbox" id="attribute02" class="sort">attribute02</label></li> 6 <li><label><input type="checkbox" id="attribute03" class="sort">attribute03</label></li> 7 <li><label><input type="checkbox" id="attribute04" class="sort">attribute04</label></li> 8 <li><label><input type="checkbox" id="attribute05" class="sort">attribute05</label></li> 9 </ul> 10 11 <ul id="type"> 12 <li><label class="selected"><input type="checkbox" id="all_type" class="sort" checked="checked">すべて</label></li> 13 <li><label><input type="checkbox" id="type01" class="sort">type01</label></li> 14 <li><label><input type="checkbox" id="type02" class="sort">type02</label></li> 15 <li><label><input type="checkbox" id="type03" class="sort">type03</label></li> 16 <li><label><input type="checkbox" id="type04" class="sort">type04</label></li> 17 <li><label><input type="checkbox" id="type05" class="sort">type05</label></li> 18 <li><label><input type="checkbox" id="type06" class="sort">type06</label></li> 19 <li><label><input type="checkbox" id="type07" class="sort">type07</label></li> 20 <li><label><input type="checkbox" id="type08" class="sort">type08</label></li> 21 </ul> 22 23</div><!-- /.#search --> 24 25<ul id="list"> 26 <li class="attribute01 type01"><a href="#">attribute01 type01</a></li> 27 <li class="attribute03 type02"><a href="#">attribute03 type02</a></li> 28 <li class="attribute02 type05"><a href="#">attribute02 type05</a></li> 29 <li class="attribute05 type06"><a href="#">attribute05 type06</a></li> 30 <li class="attribute03 type07"><a href="#">attribute03 type07</a></li> 31 <li class="attribute04 type04"><a href="#">attribute04 type04</a></li> 32 <li class="attribute03 type02"><a href="#">attribute03 type02</a></li> 33 <li class="attribute04 type08"><a href="#">attribute04 type08</a></li> 34 <li class="attribute01 type03"><a href="#">attribute01 type03</a></li> 35 <li class="attribute02 type02"><a href="#">attribute02 type02</a></li> 36</ul>
javascript
1$( function() { 2 $( '#search li label input:not(#all_attribute)' ).click( function() { 3 var inputkey = [ 'attribute', 'type' ] 4 , i, l = inputkey.length 5 , $res, $tmp, res = [] 6 , $all = $( '#all_attribute' ); 7 ; 8 $tmp = $( '#list > li' ).removeClass( 'active' ); 9 for ( i = 0; i < l; i++ ) { 10 res = [] 11 $( '#search input[id^="' + inputkey[ i ] + '"]:checked' ).each( function() { 12 res.push( $( this )[ 0 ].id ); 13 } ); 14 if ( !res.length ) { continue; } 15 $tmp = $( '#list > li.' + res.join( ', #list > li.' ) ); 16 if ( !$res || !$res.length ) { 17 $res = $tmp; 18 } else if ( $tmp.length ) { 19 $res = $res.filter( '.' + res.join( ', .' ) ); 20 } 21 } 22 if ( $res && $res.length ) { 23 $res.addClass( 'active' ); 24 $all.prop( 'checked', false ); 25 } else { 26 // No Selected 27 res = false; 28 $all.prop( 'checked', true ); 29 } 30 } ); 31 $( '#all_attribute' ).click( function() { 32 if ( $( this ).prop( 'checked') === true ) { 33 $( '#list > li' ).removeClass( 'active' ); 34 $( '#search input:checked:not(#all_attribute)' ).prop( 'checked', false ); 35 } 36 } ); 37 $( '#search li label input:not(#all_type)' ).click( function() { 38 var inputkey = [ 'attribute', 'type' ] 39 , i, l = inputkey.length 40 , $res, $tmp, res = [] 41 , $all = $( '#all_attribute' ); 42 ; 43 $tmp = $( '#list > li' ).removeClass( 'active' ); 44 for ( i = 0; i < l; i++ ) { 45 res = [] 46 $( '#search input[id^="' + inputkey[ i ] + '"]:checked' ).each( function() { 47 res.push( $( this )[ 0 ].id ); 48 } ); 49 if ( !res.length ) { continue; } 50 $tmp = $( '#list > li.' + res.join( ', #list > li.' ) ); 51 if ( !$res || !$res.length ) { 52 $res = $tmp; 53 } else if ( $tmp.length ) { 54 $res = $res.filter( '.' + res.join( ', .' ) ); 55 } 56 } 57 if ( $res && $res.length ) { 58 $res.addClass( 'active' ); 59 $all.prop( 'checked', false ); 60 } else { 61 // No Selected 62 res = false; 63 $all.prop( 'checked', true ); 64 } 65 } ); 66 $( '#all_type' ).click( function() { 67 if ( $( this ).prop( 'checked') === true ) { 68 $( '#list > li' ).removeClass( 'active' ); 69 $( '#search input:checked:not(#all_type)' ).prop( 'checked', false ); 70 } 71 } ); 72} );
css
1.active { 2 background-color: red; 3} 4 5#search { 6 overflow: hidden; 7} 8#search > ul { 9 float: left; 10}
以下、追記いたします。
https://jsfiddle.net/mekadoq/m5z9hezb/2/
上記URLのように、2つある「すべて」のidをそれぞれ「all_attribute」「all_type」として、javascriptもそれぞれに対応させようとしたところ、各選択肢にチェックをいれた際に「すべて」のチェックが外れません。
また、「.addClass( 'active' );」「.removeClass( 'active' );」が実行されなくなってしまいました。
