-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterForTable.min.js
More file actions
13 lines (13 loc) · 1.54 KB
/
filterForTable.min.js
File metadata and controls
13 lines (13 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* filterForTable
*
* This plugin will add a searching filter to the table.
* If cell has formated number values, plugin will transform them to simple numbers and
* will compare with filter value. So if enter in filter input 1500,
* script will show 1 500, 1500, 1,500, $1 500, %1 500 and so on.
*
* @version v1.0.0
* @author Alex Ya, alex.yaromenko@gmail.com
* @license MIT
*/
!function(e){e.fn.filterForTable=function(n){var r={searchSelector:"#searchInput",minChars:1,minRows:1,emptyMsg:"No data found",emptyTrId:"filterForTableEmptyRow",exceptClass:""},a=e.extend({},r,n),i=e(a.searchSelector),s=function(t,n){u();var r=t.find("tbody"),i=r.find("tr"),s=0;i.each(function(){tr=e(this);var t=!1;""!=a.exceptClass&&tr.hasClass(a.exceptClass)?t=!0:tr.find("td").each(function(){td=e(this);var r=td.text();td.attr("data-filter")&&(r=td.data("filter")),o(r,n)&&(t=!0)}),t?(tr.show(),s++):tr.hide()}),0==s&&f(t)},o=function(t,e){return t.toUpperCase().indexOf(e.toUpperCase())>=0?!0:(t=d(t),e=d(e),t.toUpperCase().indexOf(e.toUpperCase())>=0?!0:!1)},d=function(t){return query=t.replace(/[-+$%, ]/g,""),c(query)||(query=t),query},u=function(){e("#"+a.emptyTrId).remove()},f=function(t){var e=t.find("thead tr td").length;t.find("tbody").append('<tr id="'+a.emptyTrId+'"><td colspan="'+e+'">'+a.emptyMsg+"</td></tr>")},c=function(t){return!isNaN(parseFloat(t))&&isFinite(t)};return this.each(function(){t=e(this),i.on("keyup",function(){t.find("tbody tr").length>=a.minRows&&(0==this.value.length||this.value.length>=a.minChars)&&s(t,this.value)})})}}(jQuery);