isotope icon indicating copy to clipboard operation
isotope copied to clipboard

is there anyway to filter using search ?

Open dimios opened this issue 15 years ago • 18 comments

Is there anyway that I can use search to submit key-words to filter results? thanks in advance

dimios avatar Mar 21 '11 15:03 dimios

Yes, I'm sure there is a way. As to a recommended way, I'm sorry to say I don't have one for you. I'll leave that up to you.

desandro avatar Mar 22 '11 20:03 desandro

Not sure if this is what you mean by "filter using search," but this is what I did:

$(":text").bind("keyup change", function(){
    var val = $(this).val().toLowerCase();
    $(".omit-me").removeClass("omit-me");
    if (val) {
        $list.children(":not(.blank)").filter(function(){
            return $(this).find("span").text().toLowerCase().indexOf(val) === -1;
        }).addClass("omit-me");
        $list.isotope({ filter: ':not(.omit-me)' });
    } else {
        $list.isotope({ filter: '*' });
    }
});

$("form").submit(function(){
    return false;
});

Basically, with each character you type into the text box, Isotope filters the results based on whether or not that character exists in the name of the item. I accomplished that by adding a class to all the items that returned false, then used Isotope to filter the remaining items.

Here's where it got tricky -- David, this may be a bug -- but I noticed that when all of the items returned false (e.g. the letter "g" didn't show up in any of the items), the Isotope filtering failed. How I got it to work was I appended a blank item to the end of the list of items, then styled it as such:

.blank {
display: none;
height: 0;
font-size: 0;
}

That's why I'm filtering against the items that aren't blank, rather than all items, guaranteeing that an item will be left so that if the keyword search fails, the user knows because there aren't any visible items.

Anyway, I hope this helps you, Dimios.

David, let me know if there's a better way to do it than this. And thanks for making such an insanely awesome plugin!

atacrawl avatar Apr 01 '11 18:04 atacrawl

thanks a lot atacrawl for sharing, I am going to give it a try now.

dimios avatar Apr 01 '11 18:04 dimios

@atacrawl : Today's commit 011db6 should resolve the issue with the filtering when nothing matches the filter.

desandro avatar Apr 01 '11 19:04 desandro

@atacrawl do you have some example link on how are you using it? cheers

dimios avatar Apr 01 '11 20:04 dimios

Here's something like what you're talking about: http://forrst.com/posts/isotope_js_keyword_based_filtering-V6K

Malm avatar Sep 01 '12 15:09 Malm

The post above was incredibly helpful. I was able to build a pretty robust searching function with the template it provided...

gabaum10 avatar Jan 25 '13 21:01 gabaum10

I am having trouble implementing this over my current layout here.

ghost avatar Aug 17 '13 23:08 ghost

Malm, thanks for the post! Very very helpful. I was able to build search functionality for my items based off the descriptions.

wohlstadterm avatar Apr 24 '14 14:04 wohlstadterm

Last week I put together this simple example http://codepen.io/desandro/pen/wfaGu

desandro avatar Apr 24 '14 14:04 desandro

Nice! I'll be reviewing your code desandro for some improvements. Btw, I love your isotope plugin. Very robust!

wohlstadterm avatar Apr 24 '14 14:04 wohlstadterm

Thanks for the search function. I'm trying to also add a reset button and I have tried setting the button click function to

$quicksearch.val('')

and

$container.isotope({ filter: '*' })

But they do not work. Is there a way to reset the search with a button click as well as deleting the search input? Sorry for my newbie question...

macsupport avatar Apr 25 '14 00:04 macsupport

@macsupport Good question. You were close, the problem was that the qsRegex needs to be updated. See http://codepen.io/desandro/pen/Jfbpc

desandro avatar Apr 25 '14 00:04 desandro

Thanks for taking the time to make the example!!

macsupport avatar Apr 25 '14 02:04 macsupport

It seems that using search this way interferes with the standard filter method. If I use this search function first it works as it should. If I use another, different filter based on the following code after I do a search, the search function is broken.

$('#filters').on( 'click', 'button', function() {
  var filterValue = $(this).attr('data-filter');
  $container.isotope({ filter: filterValue });
});

Is the solution to make a search function as you point out in this code?

// hash of functions that match data-filter values var filterFns = { // show if number is greater than 50 numberGreaterThan50: function() { var number = $(this).find('.number').text(); return parseInt( number, 10 ) > 50; }, // show if name ends with -ium ium: function() { var name = $(this).find('.name').text(); return name.match( /ium$/ ); } }; // filter items on button click $('#filters').on( 'click', 'button', function() { var filterValue = $(this).attr('data-filter'); // use filter function if value matches filterValue = filterFns[ filterValue ] || filterValue; $container.isotope({ filter: filterValue }); });

Related to https://github.com/metafizzy/isotope/issues/709

macsupport avatar May 22 '14 00:05 macsupport

Thanks for the update David. Been waiting for this one!

r3hab-media avatar Jun 06 '14 14:06 r3hab-media

Thanks for your codepen search example. Very nice!

doubletaketech avatar Jul 11 '14 13:07 doubletaketech

This example is now added to the docs for filtering: http://codepen.io/desandro/pen/wfaGu

I'm locking this issue as it's old.

desandro avatar Jul 11 '14 16:07 desandro