Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions jquery.fastLiveFilter.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
/**
* fastLiveFilter jQuery plugin 1.0.3
*
* Copyright (c) 2011, Anthony Bush
* License: <http://www.opensource.org/licenses/bsd-license.php>
* Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/
**/

jQuery.fn.fastLiveFilter = function(list, options) {
// Options: input, list, timeout, callback
options = options || {};
list = jQuery(list);
var input = this;
var timeout = options.timeout || 0;
var callback = options.callback || function() {};

var keyTimeout;

// NOTE: because we cache lis & len here, users would need to re-init the plugin
// if they modify the list in the DOM later. This doesn't give us that much speed
// boost, so perhaps it's not worth putting it here.
var lis = list.children();
var len = lis.length;
var oldDisplay = len > 0 ? lis[0].style.display : "block";
callback(len); // do a one-time callback on initialization to make sure everything's in sync

input.change(function() {
// var startTime = new Date().getTime();
var filter = input.val().toLowerCase();
var li;
var numShown = 0;
for (var i = 0; i < len; i++) {
li = lis[i];
if ((li.textContent || li.innerText || "").toLowerCase().indexOf(filter) >= 0) {
if (li.style.display == "none") {
li.style.display = oldDisplay;
}
numShown++;
} else {
if (li.style.display != "none") {
li.style.display = "none";
}
}
}
callback(numShown);
// var endTime = new Date().getTime();
// console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)');
return false;
}).keydown(function() {
// TODO: one point of improvement could be in here: currently the change event is
// invoked even if a change does not occur (e.g. by pressing a modifier key or
// something)
clearTimeout(keyTimeout);
keyTimeout = setTimeout(function() { input.change(); }, timeout);
});
return this; // maintain jQuery chainability
}
/**
* fastLiveFilter jQuery plugin 1.1.0
*
* Copyright (c) 2011, Anthony Bush
* License: <http://www.opensource.org/licenses/bsd-license.php>
* Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/
**/
jQuery.fn.fastLiveFilter = function(list, options) {
// Options: input, list, timeout, callback
options = options || {};
list = jQuery(list);
var input = this;
var timeout = options.timeout || 0;
var callback = options.callback || function() {};
var ignore = options.ignore || 'filter-ignore';
var keyTimeout;
// NOTE: because we cache lis & len here, users would need to re-init the plugin
// if they modify the list in the DOM later. This doesn't give us that much speed
// boost, so perhaps it's not worth putting it here.
var lis = list.children();
var len = lis.length;
var oldDisplay = len > 0 ? lis[0].style.display : "block";
callback(len); // do a one-time callback on initialization to make sure everything's in sync
input.change(function() {
// var startTime = new Date().getTime();
var filter = input.val().toLowerCase();
var li;
var numShown = 0;
for (var i = 0; i < len; i++) {
li = lis[i];
if ((jQuery(li).contents(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) {
if (li.style.display == "none") {
li.style.display = oldDisplay;
}
numShown++;
} else {
if (li.style.display != "none") {
li.style.display = "none";
}
}
}
callback(numShown);
// var endTime = new Date().getTime();
// console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)');
return false;
}).keydown(function() {
// TODO: one point of improvement could be in here: currently the change event is
// invoked even if a change does not occur (e.g. by pressing a modifier key or
// something)
clearTimeout(keyTimeout);
keyTimeout = setTimeout(function() { input.change(); }, timeout);
});
return this; // maintain jQuery chainability
}