简体   繁体   English

组合过滤器Jquery同位素

[英]Combining filters Jquery Isotope

I am trying to combine filters like this example: http://codepen.io/desandro/pen/JEojz/?editors=101 but I can't. 我正在尝试结合以下示例的过滤器: http : //codepen.io/desandro/pen/JEojz/?editors=101,但我不能。

Codepen: http://codepen.io/anon/pen/gpbypp Codepen: http ://codepen.io/anon/pen/gpbypp

HTML: HTML:

 <div id="filters"> <label class="pull-left">Seats: </label> <div class="btn-group" data-toggle="buttons" data-filter-group="seats"> <label class="btn btn-default active"> <input type="radio" data-filter="*">All </label> <label class="btn btn-default"> <input type="radio" data-filter=".seats-4">4 </label> <label class="btn btn-default"> <input type="radio" data-filter=".seats-5">5 </label> </div> <br> <br> <label class="pull-left">Transmission: </label> <div class="btn-group" data-toggle="buttons" data-filter-group="transmission"> <label class="btn btn-default"> <input type="radio" data-filter=".manual">Manual </label> <label class="btn btn-default"> <input type="radio" data-filter=".auto">Auto </label> </div> </div> <div id="isotope-container"> <div class="col-sm-3 item seats-5 auto"> <h3>Volkswagen Polo</h3> <p>5 seats auto</p> </div> <div class="col-sm-3 item seats-4 auto"> <h3>Suzuki Jimny</h3> <p>4 seats auto</p> </div> <div class="col-sm-3 item seats-4 manual"> <h3>Volkswagen Caddy</h3> <p>4 seats manual</p> </div> <div class="col-sm-3 item seats-5 manual"> <h3>Opel Zafira</h3> <p>5 seats manual</p> </div> </div> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery.isotope/2.2.0/isotope.pkgd.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 

Isotope website 同位素网站

Starting with the codepen from Pixelsmith, I swapped out the JS to match the example that you posted. 从Pixelsmith的codepen开始,我换出了JS以匹配您发布的示例。 I believe this is what you were looking for. 我相信这就是您想要的。 The is-checked formatting is a little wonky, but the functionality is there. 经过检查的格式有些奇怪,但是功能在那里。

This is the new JS. 这是新的JS。 Working example 工作实例

$('.filters').on( 'click', '.btn', function() {
var $this = $(this);

var $buttonGroup = $this.parents('.btn-group');
var filterGroup = $buttonGroup.attr('data-filter-group');
// set filter for group
filters[ filterGroup ] = $this.attr('data-filter');

var filterValue = concatValues( filters );
$container.isotope({ filter: filterValue });
});

// change is-checked class on buttons
$('.btn-group').each( function( i, buttonGroup ) {
 var $buttonGroup = $( buttonGroup );
  $buttonGroup.on( 'click', 'button', function() {
    $buttonGroup.find('.is-checked').removeClass('is-checked');
    $( this ).addClass('is-checked');
  });
});

// flatten object by concatting values
function concatValues( obj ) {
  var value = '';
  for ( var prop in obj ) {
    value += obj[ prop ];
  }
  return value;
}

There's an error in your JS that's stopping the function from running, and then quirks in your code that seem to break the function. JS中出现错误,导致该函数停止运行,然后在您的代码中出现古怪的现象,似乎破坏了该函数。

In the function you need to change the line $('#filters input').on('click', '.btn', function() { to $('#filters').on('click', '.btn', function() { , otherwise you're asking the browser to watch all input s in the #filters div for a click on .btn and that won't ever happen. 在函数中,您需要将行$('#filters input').on('click', '.btn', function() {更改$('#filters').on('click', '.btn', function() { ,否则,您是在要求浏览器监视#filters div中的所有input s,以click .btn而这永远不会发生。

Second, I couldn't get the filters to work with the label/radio combination that you'd created, but I did get it to work with buttons. 其次,我无法使过滤器与您创建的标签/无线电组合一起使用,但确实可以与按钮一起使用。 Is there a reason you're choosing radio buttons? 您选择单选按钮是否有原因? If so you'll need to work with the code a bit more to make it happen, and you may want to move away from click and use attr('checked') instead. 如果是这样,您将需要多使用一些代码来实现它,并且您可能想摆脱click而使用attr('checked')代替。

Working Codepen 工作码笔

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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