Filters + Search with Isotopes Breaks Search?
Asked Answered
P

2

10

I am using Isotopes (v1) and have created a search field following an example in a Pen.

Initially it works, however, if I filter the Isotope gallery then the search field stops working.

I believe the search function still runs just doesn't filter the gallery and I am unsure how to fix the problem. In fact I am unsure what the exact problem is as no errors are thrown.

Here is a Fiddle with a working example.

Here is the search, filter and isotope JavaScript:

var $container = $('.isotope'),
    qsRegex,
    filters = {};

$container.isotope({
  itemSelector : '.element',
  masonry : {
    columnWidth : 120
  },
  getSortData : {
    name : function ( $elem ) {
      return $elem.find('.name').text();
    }
  },
filter: function() {
  return qsRegex ? $(this).text().match( qsRegex ) : true;
}
});

function searchFilter() {
  qsRegex = new RegExp( $quicksearch.val(), 'gi' );
  $container.isotope();
}

// use value of search field to filter
var $quicksearch = $('#quicksearch').keyup( debounce( searchFilter ) );

$('#reset').on( 'click', function() {
  $quicksearch.val('');
  searchFilter()
});
    
     // store filter for each group
    
    
    $('#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 = '';
      for ( var prop in filters ) {
        filterValue += filters[ prop ];
      }
      // set filter for Isotope
      $container.isotope({ filter: filterValue });
        
    });
    
    // debounce so filtering doesn't happen every millisecond
    function debounce( fn, threshold ) {
    var timeout;
    return function debounced() {
      if ( timeout ) {
        clearTimeout( timeout );
      }
      function delayed() {
        fn();
        timeout = null;
      }
      setTimeout( delayed, threshold || 100 );
    }
    }

How do I solve the problem?

Note: I am using jQuery 2.1.1.

Pygidium answered 23/5, 2014 at 1:5 Comment(3)
can you make a Fiddle?Downcomer
@DaveA - There is a fiddle in my question. Just above my code is the link.Pygidium
@MohdDhiyaulhaq - You edited the question and added the tag jsFiddle. You should not add that tag, from jsFiddle tag wiki: This tag should be used when asking a question about jsFiddle, not because your question merely contains an example hosted on jsFiddle.Pygidium
L
4

In you example $('#filters').on('click', '.button', function () stoping the search function and you reset buton placed inside #filters div so when you click it search engine is stoped too.

I have not the best solution, but it solve some problems:

Idea in using function to call engine back:

var iso = function() {

//engine here

}

and

$(function () {
   iso();
   $('.iso').click(function(){
      setTimeout(iso, 500);
});
});

without setTimeout it can't work.

But it don't solve the main problem

look at FIDDLE and you'll understand what I mean

Or you just can place reset and Show All buttons outside #filters div

Lxx answered 11/6, 2014 at 12:40 Comment(0)
C
4

I faced the same problem implementing Filters + Search functionality.

I solved this problem passing the filter function to the Isotope call ($container.isotope();) in the search function (function searchFilter(){...}) instead of when initializing the Isotope instance.

So, in your code it should be like this:

// No filter specified when initializing the Isotope instance
$container.isotope({
  itemSelector : '.element',
  masonry : {
    columnWidth : 120
  },
  getSortData : {
    name : function ( $elem ) {
      return $elem.find('.name').text();
    }
  }
});
// Instead, the filter is specified here
function searchFilter() {
  qsRegex = new RegExp( $quicksearch.val(), 'gi' );
  $container.isotope({
    filter: function() {
     return qsRegex ? $(this).text().match( qsRegex ) : true;
    }
  });
}
Cardiganshire answered 10/10, 2014 at 15:53 Comment(2)
Would it be possible to put your solution in a fiddle? I'm still kind of confused as to how your solution works. Thank you for your time, either way.Agile
Sure! jsfiddle.net/vD62w/3 The important part is adding the filter function on the isotope initialization on ´searchFilter´ instead of at the beginning.Cardiganshire

© 2022 - 2024 — McMap. All rights reserved.