Filters + Search with Isotopes Breaks Search?

大憨熊 提交于 2020-02-03 03:58:06

问题


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.


回答1:


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




回答2:


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;
    }
  });
}


来源:https://stackoverflow.com/questions/23819517/filters-search-with-isotopes-breaks-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!