Im using the following snippet to facilitate a live search on my Isotope implementation.
It works great, but im wondering if there is a way to limit the scope of what it searches for.
Our isotope returns HTML like the following:
<div class="item">
<span class="title"></span>
<span calss="description"></span>
Right now it searches everything, how can i limit it to filter in results from just the "title" field?
jQuery(document).ready(function($) {
$(document).ready(function(){
var $container = $('#portfolio'),
// create a clone that will be used for measuring container width
$containerProxy = $container.clone().empty().css({ visibility: 'hidden' });
$container.after( $containerProxy );
// get the first item to use for measuring columnWidth
var $item = $container.find('.portfolio-item').eq(0);
$container.imagesLoaded(function(){
$(window).smartresize( function() {
// calculate columnWidth
var colWidth = Math.floor( $containerProxy.width() / 5 ); // Change this number to your desired amount of columns
// set width of container based on columnWidth
$container.css({
width: colWidth * 5 // Change this number to your desired amount of columns
})
.isotope({
// disable automatic resizing when window is resized
resizable: false,
// set columnWidth option for masonry
masonry: {
columnWidth: '.grid-sizer',
gutter: '.gutter-sizer'
}
});
// trigger smartresize for first time
}).smartresize();
});
$( function() {
// quick search regex
var qsRegex;
// init Isotope
var $grid = $('#portfolio').isotope({
itemSelector: '.portfolio-item',
layoutMode: 'fitRows',
filter: function() {
return qsRegex ? $(this).text().match( qsRegex ) : true;
}
});
// use value of search field to filter
var $quicksearch = $('.quicksearch').keyup( debounce( function() {
qsRegex = new RegExp( $quicksearch.val(), 'gi' );
$grid.isotope();
}, 200 ) );
// bind filter on select change
$('#filter-select').on( 'change', function() {
// get filter value from option value
var filterValue = this.value;
$grid.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;
}
timeout = setTimeout( delayed, threshold || 100 );
}
}
// filter items when filter link is clicked
$('#filters a').click(function(){
$('#filters a.active').removeClass('active');
var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector, animationEngine : "css" });
$(this).addClass('active');
return false;
});
});
});
#filter-select
element so we know what element is triggering the change event. – Stater