Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created April 27, 2012 14:53
Show Gist options
  • Select an option

  • Save stefanocudini/2509896 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/2509896 to your computer and use it in GitHub Desktop.
jquery plugin filter results
/*
<label class="count" id="countresults">Results<em></em></label>
<input id="findres" type="text" size="8" value="" />
<div id="actives">
<div id="activesempty">Nessun elemento trovato.</div>
</div>
*/
$(function() {
$('#findres').finder({
content: '#actives',
child: '.box',
childSearch: 'a.title',
initial: false
});
});
(function($){ //mini plugin per creare campo di ricerca nel riquadro tags e results
$.fn.extend({
finder: function(options) { //plugin name
var defaults = {
content: '',
child: '.box',
childSearch: '.title',
initial: true,//cerca iniziale o all'interno del testo
searchText: 'Search...',
inactiveClass: 'inactive'
};
var options = $.extend(defaults, options);
return this.each(function() {
var targetFind$ = $(options.content) || $(this).parent().next(),
findtagssize = $(this).attr('size');
$(this)
.on('keyup', function() {
var t = $(this).val();
if(t.length<1)
targetFind$.children(options.child).show();
else
{
if(t.length > parseInt(findtagssize))
$(this).attr('size',t.length+1);
var I = options.initial ? '^' : '';
var reg = new RegExp(I + t,'i');
targetFind$.find(options.childSearch).map(function() {
return reg.test( $(this).text() ) ? false : this;
}).parent(options.child).hide();
}
$(this).prev().children('em').text('('+ targetFind$.children(options.child+':visible').length +')');
})
.on('blur', function() {
tf = setTimeout(function() {//non togliere senno il click sul tag non produce risultati
targetFind$.children(options.child).show();
$(this).prev().children('em').text('('+ targetFind$.children(options.child+':visible').length +')');
},300);
$(this).addClass(options.inactiveClass).val(options.searchText).attr('size',findtagssize);
})
.on('mousedown',function(e) {
$(this).removeClass(options.inactiveClass).val('').focus();
})
.addClass(options.inactiveClass).val(options.searchText);
//USARE ATTRIBUTO PLACEHOLDER DI HTML5
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment