From 4db26a7d6600257d6c6616ba0280f6a14752a95e Mon Sep 17 00:00:00 2001 From: Kyo Nagashima Date: Wed, 22 Dec 2010 07:42:41 +0900 Subject: [PATCH] delayed keypress event 300ms --- jquery.table-filter.js | 25 ++++++++++++++++++++++--- jquery.table-filter.min.js | 12 ++---------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/jquery.table-filter.js b/jquery.table-filter.js index 042ff7e..928c6b2 100644 --- a/jquery.table-filter.js +++ b/jquery.table-filter.js @@ -34,8 +34,8 @@ $("

").addClass("formTableFilter").append(label).append(input).insertBefore(this); // Bind filtering function - $("#" + id).keyup(function (e) { - var words = this.value.toLowerCase().split(" "); + $("#" + id).delayBind("keyup", function (e) { + var words = $(this).val().toLowerCase().split(" "); $("#" + tgt + " tbody tr").each(function () { var s = $(this).html().toLowerCase().replace(/<.+?>/g, "").replace(/\s+/g, " "); var state = 0; @@ -47,7 +47,7 @@ }); state ? $(this).hide() : $(this).show(); }); - }); + }, 300); } return this; @@ -57,4 +57,23 @@ labelText: "Keyword(s): ", size: 32 }; + + $.fn.delayBind = function (type, data, func, timeout) { + if ($.isFunction(data)) { + timeout = func; + func = data; + data = undefined; + } + + var self = this; + var wait = null; + var handler = function (e) { + clearTimeout(wait); + wait = setTimeout(function() { + func.apply(self, [$.extend({}, e)]); + }, timeout); + }; + + return this.bind(type, data, handler); + }; })(jQuery); diff --git a/jquery.table-filter.min.js b/jquery.table-filter.min.js index 9351aa3..d210da5 100644 --- a/jquery.table-filter.min.js +++ b/jquery.table-filter.min.js @@ -1,10 +1,2 @@ -/* - * jQuery Plugin: Table Filter - version 0.1.1 - * http://github.com/hail2u/jquery.table-filter - * Insert a input form for filtering table rows dinamically. - * - * Copyright (c) 2009 Kyo Nagashima - * This library licensed under MIT license: - * http://opensource.org/licenses/mit-license.php - */ -(function(a){a.fn.addTableFilter=function(d){var e=a.extend({},a.fn.addTableFilter.defaults,d);if(this.is("table")){if(!this.attr("id")){this.attr({id:"t-"+Math.floor(Math.random()*99999999)})}var g=this.attr("id");var f=g+"-filtering";var c=a("

").addClass("formTableFilter").append(c).append(b).insertBefore(this);a("#"+f).keyup(function(h){var i=this.value.toLowerCase().split(" ");a("#"+g+" tbody tr").each(function(){var j=a(this).html().toLowerCase().replace(/<.+?>/g,"").replace(/\s+/g," ");var k=0;a.each(i,function(){if(j.indexOf(this)<0){k=1;return false}});k?a(this).hide():a(this).show()})})}return this};a.fn.addTableFilter.defaults={labelText:"Keyword(s): ",size:32}})(jQuery); \ No newline at end of file +(function(a){a.fn.addTableFilter=function(c){var b=a.extend({},a.fn.addTableFilter.defaults,c);if(this.is("table")){this.attr("id")||this.attr({id:"t-"+Math.floor(Math.random()*99999999)});var d=this.attr("id");c=d+"-filtering";var e=a("

").addClass("formTableFilter").append(e).append(b).insertBefore(this);a("#"+c).delayBind("keyup",function(){var h=a(this).val().toLowerCase().split(" ");a("#"+ +d+" tbody tr").each(function(){var f=a(this).html().toLowerCase().replace(/<.+?>/g,"").replace(/\s+/g," "),g=0;a.each(h,function(){if(f.indexOf(this)<0){g=1;return false}});g?a(this).hide():a(this).show()})},300)}return this};a.fn.addTableFilter.defaults={labelText:"Keyword(s): ",size:32};a.fn.delayBind=function(c,b,d,e){if(a.isFunction(b)){e=d;d=b;b=undefined}var h=this,f=null;return this.bind(c,b,function(g){clearTimeout(f);f=setTimeout(function(){d.apply(h,[a.extend({},g)])},e)})}})(jQuery); \ No newline at end of file