Created
May 17, 2013 14:23
-
-
Save first-developer/5599352 to your computer and use it in GitHub Desktop.
Live filter : jQuery helper to filter list item by item selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !(function($) { | |
| $.fn.live_filter = function (list_selector, opts) { | |
| var default_options = { | |
| item_selector : null, | |
| duration : 200 , | |
| filter: function(el, val){ | |
| return $(el).text().toUpperCase().indexOf(val.toUpperCase()) >= 0; | |
| } | |
| } | |
| ,options = $.extend(default_options, opts) | |
| ,$inputs = $(this) | |
| ,$list = $(list_selector) | |
| ,$elements = $list.find(options.item_selector) | |
| var filter = default_options.filter | |
| var init = function() { | |
| $inputs.each(function() { | |
| $input = $(this); | |
| $input.keyup(function(){ | |
| var val = $input.val(); | |
| console.log(this); | |
| var filtered_elements = $elements.filter(function() { | |
| return filter(this, val); | |
| }); | |
| var ignored_elements = $elements.not(filtered_elements); | |
| filtered_elements.show(options.duration); | |
| ignored_elements.hide(options.duration); | |
| if (!val) { | |
| $elements.show(); | |
| } | |
| }); | |
| }); | |
| }; | |
| init(); | |
| return this; | |
| }; | |
| $("input[type=text][data-filter=true]").live_filter(".section_list", { item_selector: "li" }); | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment