From 859a7aec45920ada5fd0df12a20198ddddd74b84 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 10 Sep 2013 22:16:52 -0400 Subject: [PATCH 1/4] Prevent change event from being invoked when a change hasn't actually been made --- jquery.fastLiveFilter.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index 141448e..405be84 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -11,6 +11,7 @@ jQuery.fn.fastLiveFilter = function(list, options) { options = options || {}; list = jQuery(list); var input = this; + var lastFilter = ''; var timeout = options.timeout || 0; var callback = options.callback || function() {}; @@ -47,11 +48,12 @@ jQuery.fn.fastLiveFilter = function(list, options) { // 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); + keyTimeout = setTimeout(function() { + if( input.val() === lastFilter ) return; + lastFilter = input.val(); + input.change(); + }, timeout); }); return this; // maintain jQuery chainability } From 01f76c56db3c12bcf660a5816bfb8646dcb59f10 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 10 Sep 2013 22:38:49 -0400 Subject: [PATCH 2/4] Added selector option --- jquery.fastLiveFilter.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index 405be84..a4a97ae 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -28,11 +28,15 @@ jQuery.fn.fastLiveFilter = function(list, options) { input.change(function() { // var startTime = new Date().getTime(); var filter = input.val().toLowerCase(); - var li; + var li, innerText; var numShown = 0; for (var i = 0; i < len; i++) { li = lis[i]; - if ((li.textContent || li.innerText || "").toLowerCase().indexOf(filter) >= 0) { + innerText = !options.selector ? + (li.textContent || li.innerText || "") : + $(li).addBack().find(options.selector).text(); + + if (innerText.toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") { li.style.display = oldDisplay; } From 5be95200961197a60bd93b31c42ce68994377c7a Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 10 Sep 2013 22:44:43 -0400 Subject: [PATCH 3/4] Updated readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 486c9c0..b80b046 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Available options: - timeout: How many milliseconds to wait after keydown before filtering the list. Default is 0. - callback: A callback method which will be given the number of items left in the list. +- selector: By default, the plugin will match the filter against the text of the `li`. If specifed, the selector will be applied to the `li` and the resulting text will be used instead. **WARNING:** Use of complex selectors may reduce performance significantly, especially in large lists! Example: From 7ad6fcde21a1e37c7410bd01056a121794dca9a2 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 10 Sep 2013 23:19:25 -0400 Subject: [PATCH 4/4] Removed addback() --- jquery.fastLiveFilter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index a4a97ae..bd53494 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -34,7 +34,7 @@ jQuery.fn.fastLiveFilter = function(list, options) { li = lis[i]; innerText = !options.selector ? (li.textContent || li.innerText || "") : - $(li).addBack().find(options.selector).text(); + $(li).find(options.selector).text(); if (innerText.toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") {