├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── jquery.fastLiveFilter.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | v1.0.3 / 2012-06-11: 2 | 3 | - Make options arg optional (fixes #4) 4 | - Fix accidental global 5 | 6 | v1.0.2 / 2012-01-24: 7 | 8 | - Add package.json for new/upcoming plugins.jquery.com 9 | 10 | v1.0.1 / 2011-12-07: 11 | 12 | - Add support for Win/IE (fixes #3) 13 | 14 | v1.0.0 / 2011-11-05: 15 | 16 | - Initial version with support for lists and similar DOM elements. 17 | - Customizable timeout option. 18 | - Customizable callback function. 19 | - Speed: ~20-600x faster than the competition. 20 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011, Anthony Bush 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What 2 | ---- 3 | 4 | This is a live filter plugin for jQuery that is built for speed and ease of use. It was made because existing tools were inadequate (too slow, wrong feature set). Check out the [demo](http://anthonybush.com/projects/jquery_fast_live_filter/demo/) and [comparison](http://anthonybush.com/projects/jquery_fast_live_filter/comparison/) pages. 5 | 6 | Usage 7 | ----- 8 | 9 | Include jQuery, the plugin, then initialize the plugin: 10 | 11 | 12 | 13 | 18 | 19 | The above would work with this HTML: 20 | 21 | 22 | 27 | 28 | Options 29 | ------- 30 | 31 | Options are given as the second argument. Synopsis: 32 | 33 | $(INPUT_SELECTOR).fastLiveFilter(LIST_SELECTOR, options); 34 | 35 | Available options: 36 | 37 | - timeout: How many milliseconds to wait after keydown before filtering the list. Default is 0. 38 | - callback: A callback method which will be given the number of items left in the list. 39 | - 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! 40 | 41 | Example: 42 | 43 | $('#search_input').fastLiveFilter('#search_list', { 44 | timeout: 200, 45 | callback: function(total) { $('#num_results').html(total); } 46 | }); 47 | 48 | Problems? Want to contribute? 49 | ----------------------------- 50 | 51 | [Report issues](https://github.com/awbush/jquery-fastLiveFilter/issues) on github. Use [pull requests](http://help.github.com/send-pull-requests/) to contribute code. Versioning will be done as defined by [Semantic Versioning](http://semver.org/). 52 | -------------------------------------------------------------------------------- /jquery.fastLiveFilter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * fastLiveFilter jQuery plugin 1.0.3 3 | * 4 | * Copyright (c) 2011, Anthony Bush 5 | * License: 6 | * Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/ 7 | **/ 8 | 9 | jQuery.fn.fastLiveFilter = function(list, options) { 10 | // Options: input, list, timeout, callback 11 | options = options || {}; 12 | list = jQuery(list); 13 | var input = this; 14 | var lastFilter = ''; 15 | var timeout = options.timeout || 0; 16 | var callback = options.callback || function() {}; 17 | 18 | var keyTimeout; 19 | 20 | // NOTE: because we cache lis & len here, users would need to re-init the plugin 21 | // if they modify the list in the DOM later. This doesn't give us that much speed 22 | // boost, so perhaps it's not worth putting it here. 23 | var lis = list.children(); 24 | var len = lis.length; 25 | var oldDisplay = len > 0 ? lis[0].style.display : "block"; 26 | callback(len); // do a one-time callback on initialization to make sure everything's in sync 27 | 28 | input.change(function() { 29 | // var startTime = new Date().getTime(); 30 | var filter = input.val().toLowerCase(); 31 | var li, innerText; 32 | var numShown = 0; 33 | for (var i = 0; i < len; i++) { 34 | li = lis[i]; 35 | innerText = !options.selector ? 36 | (li.textContent || li.innerText || "") : 37 | $(li).find(options.selector).text(); 38 | 39 | if (innerText.toLowerCase().indexOf(filter) >= 0) { 40 | if (li.style.display == "none") { 41 | li.style.display = oldDisplay; 42 | } 43 | numShown++; 44 | } else { 45 | if (li.style.display != "none") { 46 | li.style.display = "none"; 47 | } 48 | } 49 | } 50 | callback(numShown); 51 | // var endTime = new Date().getTime(); 52 | // console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)'); 53 | return false; 54 | }).keydown(function() { 55 | clearTimeout(keyTimeout); 56 | keyTimeout = setTimeout(function() { 57 | if( input.val() === lastFilter ) return; 58 | lastFilter = input.val(); 59 | input.change(); 60 | }, timeout); 61 | }); 62 | return this; // maintain jQuery chainability 63 | } 64 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-live-filter", 3 | "version": "1.0.3", 4 | "title": "jQuery.fastLiveFilter()", 5 | "author": { 6 | "name": "Anthony Bush", 7 | "url": "https://github.com/awbush" 8 | }, 9 | "licenses": [ 10 | { 11 | "type": "BSD 2-Clause", 12 | "url": "LICENSE.txt" 13 | } 14 | ], 15 | "dependencies": { 16 | "jquery": "1.x.x" 17 | }, 18 | "description": "A live filter plugin for jQuery built for speed and ease of use", 19 | "keywords": [ 20 | "live", 21 | "filter" 22 | ], 23 | "homepage": "http://anthonybush.com/projects/jquery_fast_live_filter/", 24 | "docs": "http://anthonybush.com/projects/jquery_fast_live_filter/", 25 | "demo": "http://anthonybush.com/projects/jquery_fast_live_filter/demo/", 26 | "files": [ 27 | "jquery.fastLiveFilter.js" 28 | ] 29 | } 30 | --------------------------------------------------------------------------------