From 43032518b74b049dc2c0485b2df60cd5c25762f6 Mon Sep 17 00:00:00 2001 From: dutiyesh Date: Sun, 14 Aug 2016 12:35:32 +0530 Subject: [PATCH] Add 'No result found' feature if no match found --- jquery.fastLiveFilter.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index bd53494..5ade7ad 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -1,6 +1,6 @@ /** * fastLiveFilter jQuery plugin 1.0.3 - * + * * Copyright (c) 2011, Anthony Bush * License: * Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/ @@ -14,9 +14,12 @@ jQuery.fn.fastLiveFilter = function(list, options) { var lastFilter = ''; var timeout = options.timeout || 0; var callback = options.callback || function() {}; - + var keyTimeout; - + + // append 'No result found' item to the list + list.append("
  • No result found!
  • "); + // 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. @@ -24,7 +27,7 @@ jQuery.fn.fastLiveFilter = function(list, options) { 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(); @@ -32,10 +35,10 @@ jQuery.fn.fastLiveFilter = function(list, options) { var numShown = 0; for (var i = 0; i < len; i++) { li = lis[i]; - innerText = !options.selector ? - (li.textContent || li.innerText || "") : + innerText = !options.selector ? + (li.textContent || li.innerText || "") : $(li).find(options.selector).text(); - + if (innerText.toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") { li.style.display = oldDisplay; @@ -47,6 +50,13 @@ jQuery.fn.fastLiveFilter = function(list, options) { } } } + + // if no match is found i.e. when var numShown count is 0, display 'No result found' list item + if (!numShown) { + li = lis[len-1]; + li.style.display = "block"; + } + callback(numShown); // var endTime = new Date().getTime(); // console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)');