diff --git a/app/views/shared/_table_filter.html.erb b/app/views/shared/_table_filter.html.erb index b74a1ba..dae9f28 100644 --- a/app/views/shared/_table_filter.html.erb +++ b/app/views/shared/_table_filter.html.erb @@ -1,4 +1,4 @@ -
+
<%= label_tag :filter, _('Filter plans'), class: "sr-only" %> <%= search_field_tag(:filter, params[:filter], placeholder: placeholder, class: "form-control") %> diff --git a/lib/assets/javascripts/utils/filteriseTable.js b/lib/assets/javascripts/utils/filteriseTable.js index c9668d2..ce1e8cd 100644 --- a/lib/assets/javascripts/utils/filteriseTable.js +++ b/lib/assets/javascripts/utils/filteriseTable.js @@ -9,18 +9,13 @@ var query = $(el).val(), regex = new RegExp(query, 'i'); - if(query.length < 3){ - $(el).closest("table").find("tbody tr").show(); - - }else{ - $.each($(el).closest("table").find("tbody tr"), function(idx, tr){ - if($(tr).text().match(regex)){ - $(tr).show(); - }else{ - $(tr).hide(); - } - }); - } + $.each($(el).closest("table").find("tbody tr"), function(idx, tr){ + if(regex.test($(tr).text())){ + $(tr).show(); + }else{ + $(tr).hide(); + } + }); }); var clear = (function(el){ @@ -30,42 +25,20 @@ ctx.init = ctx.init || (function(options){ if($ && options && options.selector){ - var id = $(this).attr("id"); + var id = $(this).attr("id"); /* initialize a debounced listener for the filter box */ - var debounced = (function(){ - var funcs = {}; - return { - has: function(id){ - return funcs[id] !== undefined; - }, - get: function(id){ - return funcs[id]; - }, - set: function(id, func){ - funcs[id] = dmproadmap.utils.debounce(func); - } - } - })(); - + var debounced = dmproadmap.utils.debounce(filter); + /* Bind the clear function to the clear icon's click event */ - $(options.selector).focus(function(){ - if(!debounced.has(id)){ - debounced.set(id, filter); - } - }).blur(function(){ - if(debounced.has(id)){ - debounced.get(id).cancel(); - } - }) - - $(options.selector).siblings("#clear_filter").click(function(e){ + $(options.selector).keyup(function(){ + debounced(this, 250); + }); + + $(options.selector).siblings("#clear_filter").click(function(e){ e.preventDefault(); clear(options.selector); - - if(debounced.has(id)){ - debounced.get(id).cancel(); - } + debounced.cancel(); }); } });