user1168176
user1168176

Reputation: 31

jquery isotope - sort, layout AND multiple filters

I'm trying to create a page with sorting, layout buttons and multiple filters. The isotope homepage is a good example, but there's only one filter there. Using that page I can't figure out how to add more filters that will work together (i.e. colour and size, working together, not one at a time). Below is the code... How do I add another filter?

    <div class="option-combo">
  <h2>Filter:</h2>
  <ul id="filter" class="option-set clearfix" data-option-key="filter">
    <li><a href="#show-all" data-option-value="*" class="selected">show all</a></li>
    <li><a href="#elements" data-option-value=".element:not(.feature)">elements</a></li>
    <li><a href="#features" data-option-value=".feature">features</a></li>
    <li><a href="#examples" data-option-value=".example">examples</a></li>
  </ul>
</div>

<div class="option-combo">
  <h2>Sort:</h2>
  <ul id="sort" class="option-set clearfix" data-option-key="sortBy">
  <li><a href="#sortBy=original-order" data-option-value="original-order" data>original-order</a></li>
  <li><a href="#sortBy=name" data-option-value="name">name</a></li>
  <li><a href="#sortBy=year" data-option-value="year" class="selected">year</a></li>
  <li><a href="#sortBy=size" data-option-value="size">size</a></li>
  <li><a href="#sortBy=random" data-option-value="random">random</a></li>
  </ul>
</div>

<div class="option-combo">
  <h2>Layout: </h2>
  <ul id="layouts" class="option-set clearfix" data-option-key="layoutMode">
    <li><a href="#masonry" data-option-value="masonry" class="selected">masonry</a></li>
    <li><a href="#fitRows" data-option-value="fitRows">fitRows</a></li>
    <li><a href="#straightDown" data-option-value="straightDown">straightDown</a></li>
  </ul>
</div>

And this is the javascript:

$(function(){

  var $container = $('#container');

  $container.isotope({
    masonry: {
      columnWidth: 70
    },
    sortBy: 'year',
    sortAscending : false,
    getSortData: {
      name : function ( $elem ) {
        return $elem.find('.name').text();
      },
      size : function ( $elem ) {
        return parseInt( $elem.find('.Size').text().replace( /,/g, ''), 10 );
      },
      year : function ( $elem ) {
        return parseInt( $elem.find('.year').text().replace( /,/g, ''), 10 );
      }
    }
  });


  var $optionSets = $('#options .option-set'),
      $optionLinks = $optionSets.find('a');

  $optionLinks.click(function(){
    var $this = $(this);
    // don't proceed if already selected
    if ( $this.hasClass('selected') ) {
      return false;
    }
    var $optionSet = $this.parents('.option-set');
    $optionSet.find('.selected').removeClass('selected');
    $this.addClass('selected');

    // make option object dynamically, i.e. { filter: '.my-filter-class' }
    var options = {},
        key = $optionSet.attr('data-option-key'),
        value = $this.attr('data-option-value');
    // parse 'false' as false boolean
    value = value === 'false' ? false : value;
    options[ key ] = value;
    if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
      // changes in layout modes need extra logic
      changeLayoutMode( $this, options )
    } else {
      // otherwise, apply new options
      $container.isotope( options );
    }

    return false;
  });

});

Upvotes: 3

Views: 3549

Answers (2)

Joar
Joar

Reputation: 224

I tried to explain my approach on this question here.
Basically the idea is to look for a restrictive AND condition using the isotope function properly in your onclick event.

Upvotes: 0

Tristan
Tristan

Reputation: 3232

Multiple filters as I understand means selection elements that share multiple properties.

So for example if we have the following isotope items

<div class="isotope-item green big"></div>
<div class="isotope-item green small"></div>
<div class="isotope-item red big"></div>
<div class="isotope-item red small"></div>
<div class="isotope-item yellow big"></div>

If you want to as a filter all the elements that are

  • red AND big
  • red OR big

then you can use with the same JavaScript that you have :

<h2>Filter:</h2>
<ul id="filter" class="option-set clearfix" data-option-key="filter">
   <li><a data-option-value="*" class="selected">Show All</a></li>
   <li><a data-option-value=".red">Show Red Elements</a></li>
   <li><a data-option-value=".big">Show Big Elements</a></li>
   <li><a data-option-value=".red, big">Show Elements Red OR Big</a></li>
   <li><a data-option-value=".red.big">Show Elements Red AND Big</a></li>
</ul>

The filter works with a simple jQuery selector. Everything that matches the selection is shown, the rest is hidden.

Upvotes: 1

Related Questions