javascriptjqueryjquery-isotope

jQuery Isotope filter to no items?


I'm using isotope to filter a list with multiple filters where it is possible that based on a combination of certain filters no items will be displayed. In this case I want to display a message to the user that based on their filter parameters, no results exist. How would I go about that and does isotope have something built in to handle this? Here is a jsfiddle example. should be displayed if no items match filter set...

http://jsfiddle.net/cssguru/e4vA3/

   $(function(){

   var $container = $('#container'),
      $checkboxes = $('#filters input');

   $container.isotope({
    itemSelector: '.item'
   });

   $checkboxes.change(function(){
    var filters = [];
    // get checked checkboxes values
    $checkboxes.filter(':checked').each(function(){
      filters.push( this.value );
    });
    // ['.red', '.blue'] -> '.red, .blue'
    filters = filters.join('');
    $container.isotope({ filter: filters });
   });

   });

Solution

  • You can could to see how many isotope items do not have the "isotope-hidden" class added to it. When the result is 0, it means that all of your elements will be hidden and you can trigger something to happen. You could use a callback function, like reLayout to run every time the isotope gets filtered.

    function noResultsCheck() {
        var numItems = $('.item:not(.isotope-hidden)').length;
        if (numItems == 0) {
            //do something here, like turn on a div, or insert a msg with jQuery's .html() function
            alert("There are no results");
        }
    }
    

    Add this to your change function:

    $container.isotope( 'reLayout', noResultsCheck );
    

    http://jsfiddle.net/xvU8D/