forked from svivian/sv-filtable-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltable.min.js
6 lines (6 loc) · 2.64 KB
/
filtable.min.js
1
2
3
4
5
6
(function(d){var h=["odd","even"],k=[],l={text:{selector:'input[type="text"]',event:"keyup"},select:{selector:"select",event:"change"},checkbox:{selector:'input[type="checkbox"]',event:"click"}},f={init:function(a){a=d.extend({controlPanel:null,handleSort:!0},a);var c=d(this);return this.each(function(){for(var b in l)d(l[b].selector,a.controlPanel).each(function(){var a=b,g=d(this);k.push(g);g.on(l[a].event,function(){if("select"===a){var b=g.data("filter-hash");b&&f.updateFilterHash(b,g.val())}f.createAndRunFilters(c)})});
d(window).on("hashchange",function(){f.applyHashFilters(a.controlPanel);f.createAndRunFilters(c)});f.applyHashFilters(a.controlPanel);f.createAndRunFilters(c)})},filter:function(a){a=d.extend({filters:[],handleSort:!0},a);return this.each(function(){var c=d(this);c.trigger("beforetablefilter");if(a.handleSort)c.on("aftertablesort",function(a,e){f.handleRowsThenStripe(c,function(a){return!a.hasClass("hidden")})});setTimeout(function(){f.handleRowsThenStripe(c,function(b){for(var c=0,g=a.filters.length;c<
g;c++){for(var f=a.filters[c].columns,d=a.filters[c].value.toString().toLowerCase(),k=!1,l=0,h=f.length;l<h;l++){var m=b.find("td").eq(f[l]),n=m.data("filter-val");0<=(void 0===n?m.text():n).toString().toLowerCase().indexOf(d)&&(k=!0)}if(!k)return!1}return!0});c.trigger("aftertablefilter")},10)})},handleRowsThenStripe:function(a,c){var b=0,e=a.find("> tbody");e.each(function(){var a=e.clone();a.find("> tr").each(function(){var a=d(this),e=c(a);a.removeClass(h.join(" "));e?(a.removeClass("hidden"),
a.addClass(h[b]),b=1-b):a.addClass("hidden")});e.replaceWith(a)})},createAndRunFilters:function(a){for(var c=[],b=0,e=k.length;b<e;b++){var g=k[b],d=g.data("filter-col");if(void 0!==d){d=d.toString().split(",");var h="";k[b].is(l.checkbox.selector)?k[b].is(":checked")&&(h=g.data("filter-val")):h=g.val();c.push({columns:d,value:h})}}f.filter.apply(a,[{filters:c}])},parseFilterHash:function(){var a=window.location.hash.replace("#","");if(0===a.length)return{};var c={};a=a.split("&");for(var b in a){var e=
a[b].split("=");2===e.length&&(c[e[0]]=e[1])}return c},applyHashFilters:function(a){var c=f.parseFilterHash(),b;for(b in c)d('[data-filter-hash="'+b+'"]',a).val(c[b])},updateFilterHash:function(a,c){var b=f.parseFilterHash();b[a]=c;var e="#",d;for(d in b)0!==b[d].length&&("#"!==e&&(e+="&"),e+=d+"="+b[d]);"#"===e&&(e=window.location.pathname+window.location.search);window.history.replaceState(void 0,void 0,e)}};d.fn.filtable=function(a){if("filter"===a)return f.filter.apply(this,Array.prototype.slice.call(arguments,
1));if("object"!==typeof a&&a)d.error("Unknown method `"+a+"` on jQuery.filtable");else return f.init.apply(this,arguments)}})(jQuery);