Isotope Metafizzy Filtering

349 views Asked by At

Solved based on http://jsfiddle.net/fu3knn67/4/

Here a standard demo on combining filters http://codepen.io/desandro/pen/JEojz from isotope.metafizzy.co

I have little knowledge on JS so this may be a silly question.

I need to be able to combine filters from within a data-filter-group. (sorta like checkboxes) Example: ability to select both red and yellow, then choose small, wide and tall.

This demo clears out the previous filter, only one filter is selectable.

Please check the codepen demo to for working example.

html.

<h1>Isotope - combination filters</h1>

<div class="filters">

<div class="ui-group">
<h3>Color</h3>
<div class="button-group js-radio-button-group" data-filter-group="color">
  <button class="button is-checked" data-filter="">any</button>
  <button class="button" data-filter=".red">red</button>
  <button class="button" data-filter=".blue">blue</button>
  <button class="button" data-filter=".yellow">yellow</button>
 </div>
 </div>

 <div class="ui-group">
 <h3>Size</h3>
<div class="button-group js-radio-button-group" data-filter-group="size">
  <button class="button is-checked" data-filter="">any</button>
  <button class="button" data-filter=".small">small</button>
  <button class="button" data-filter=".wide">wide</button>
  <button class="button" data-filter=".big">big</button>
  <button class="button" data-filter=".tall">tall</button>
</div>
</div>

<div class="ui-group">
<h3>Shape</h3>
<div class="button-group js-radio-button-group" data-filter-group="shape">
  <button class="button is-checked" data-filter="">any</button>
  <button class="button" data-filter=".round">round</button>
  <button class="button" data-filter=".square">square</button>
</div>
</div>

</div>

<div class="grid">
<div class="color-shape small round red"></div>
<div class="color-shape small round blue"></div>
<div class="color-shape small round yellow"></div>
<div class="color-shape small square red"></div>
<div class="color-shape small square blue"></div>
<div class="color-shape small square yellow"></div>
<div class="color-shape wide round red"></div>
<div class="color-shape wide round blue"></div>
<div class="color-shape wide round yellow"></div>
<div class="color-shape wide square red"></div>
<div class="color-shape wide square blue"></div>
<div class="color-shape wide square yellow"></div>
<div class="color-shape big round red"></div>
<div class="color-shape big round blue"></div>
<div class="color-shape big round yellow"></div>
<div class="color-shape big square red"></div>
<div class="color-shape big square blue"></div>
<div class="color-shape big square yellow"></div>
<div class="color-shape tall round red"></div>
<div class="color-shape tall round blue"></div>
<div class="color-shape tall round yellow"></div>
<div class="color-shape tall square red"></div>
<div class="color-shape tall square blue"></div>
<div class="color-shape tall square yellow"></div>
</div>

The JS

$(document).ready( function() {
    // init Isotope
    var $grid = $('.grid').isotope({
        itemSelector: '.color-shape'
    });

    // store filter for each group
    var filters = {};

    $('.filters').on( 'click', '.button', function() {
        var $this = $(this);
        // get group key
        var $buttonGroup = $this.parents('.button-group');
        var filterGroup = $buttonGroup.attr('data-filter-group');
        // set filter for group
        filters[ filterGroup ] = $this.attr('data-filter');
        // combine filters
        var filterValue = concatValues( filters );
        // set filter for Isotope
        $grid.isotope({ filter: filterValue });
    });

    // change is-checked class on buttons
    $('.button-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;
}
0

There are 0 answers