6

This is regarding input checkbox for filtering jQuery isotope itemselector.

Refer to demo here, checkboxes are checked when the page is load. However I stumble upon the problem when all is unchecked by user, it will just show all .items instead of an empty container.

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

Many thanks in advance, cheers

Mars Mellow
  • 228
  • 2
  • 14

2 Answers2

5

The isotope library is working as intended.

filter - Setting a filter with show item elements that match the selector, and hide elements that do not match.

Values '*' or '' (an empty string): Shows all item elements

If you do not want this behaviour, and instead want to not show any elements when every filter is selected, you should change the filter string to be something that does not exist e.g.

if(filters.length == 0){
    filters = 'purplemonkyeydishwasher';
}
else{
    filters = filters.join(', ');
}
$container.isotope({ filter: filters });

This will then have the behaviour you want. I updated the fiddle. and it now hides all the elements when all the filters are selected.

Danack
  • 24,939
  • 16
  • 90
  • 122
1

You can simply do this:

$container.isotope({ filter: ( filters == null || filters.length == 0 ) ? 'default' : filters });`

Where default is always unused. See the demo http://jsfiddle.net/G6LRL/

Nix
  • 57,072
  • 29
  • 149
  • 198
user916011
  • 505
  • 5
  • 8