Filtrar resultados en jQuery
Hoy les presentamos un script realmente útil a la hora de darle al usuario la posibilidad de filtrar resultados en una lista...

El código tras el salto.
El código:
El siguiente código funciona buscando el texto vayamos generando, en cada elemento de la lista, y de esa manera elimina los elementos que no coincidan.
JAVASCRIPT:
-
(function ($) {
-
jQuery.expr[':'].Contains = function(a,i,m){
-
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
-
};
-
-
function filterList(header, list) {
-
var form = $("
-
<form>").attr({"class":"filterform","action":"#"}),
-
input = $("
-
<input />").attr({"class":"filterinput","type":"text"});
-
$(form).append(input).appendTo(header);
-
-
$(input)
-
.change( function () {
-
var filter = $(this).val();
-
if(filter) {
-
-
$matches = $(list).find('a:Contains(' + filter + ')').parent();
-
$('li', list).not($matches).slideUp();
-
$matches.slideDown();
-
-
} else {
-
$(list).find("li").slideDown();
-
}
-
return false;
-
})
-
.keyup( function () {
-
$(this).change();
-
});
-
}
-
-
$(function () {
-
filterList($("#form"), $("#list"));
-
});
-
}(jQuery));
El HTML:
HTML:


Deja un comentario