|
|
|
/*!
|
|
|
|
SearchPane 0.0.1
|
|
|
|
2017 SpryMedia Ltd - datatables.net/license
|
|
|
|
*/
|
|
|
|
(function(b){"function"===typeof define&&define.amd?define(["jquery","datatables.net"],function(d){return b(d,window,document)}):"object"===typeof exports?module.exports=function(d,h){d||(d=window);if(!h||!h.fn.dataTable)h=require("datatables.net")(d,h).$;return b(h,d,d.document)}:b(jQuery,window,document)})(function(b,d,h,k){function f(a,c){var g=this,e=new j.Api(a);this.classes=b.extend(!0,{},f.classes);this.dom={container:b("<div/>").addClass(this.classes.container)};this.c=b.extend(!0,{},f.defaults,
|
|
|
|
c);this.s={dt:e};e.settings()[0].searchPane=this;e.columns(this.c.columns).eq(0).each(function(a){g._pane(a)});b(this.dom.container).on("click","li",function(){g._toggle(this)}).on("click","button."+this.classes.clear,function(){g._clear(b(this).closest("div."+g.classes.pane.container))});this._attach()}var j=b.fn.dataTable;b.extend(f.prototype,{rebuild:function(){var a=this;this.s.dt.columns(this.c.columns).eq(0).each(function(c){a._pane(c)})},_attach:function(){var a=this.c.container,a="function"===
|
|
|
|
typeof a?a(this.s.dt):a;"prepend"===this.c.insert?b(this.dom.container).prependTo(a):b(this.dom.container).appendTo(a)},_binData:function(a){var c={};a.each(function(a){a&&(c[a]?c[a]++:c[a]=1)});return c},_clear:function(a){var c=this.classes,b=c.item.selected;a.find("li."+b).removeClass(b);a.removeClass(c.pane.active);this.s.dt.column(a.data("column")).search("").draw()},_pane:function(a){var c=this.classes,g=c.item,c=c.pane,e=this.s.dt.column(a),l=b("<ul/>"),m=this._binData(e.data().flatten());
|
|
|
|
if(!(this._variance(m)<this.c.threshold)){for(var f=e.search(),f=f?f.substr(1,f.length-2).split("|"):[],d=e.data().unique().sort().toArray(),i=0,h=d.length;i<h;i++)if(d[i]){var j=b("<li/>").html('<span class="'+g.label+'">'+d[i]+"</span>").data("filter",d[i]).append(b("<span/>").addClass(g.count).html(m[d[i]]));if(f.length){var k=d[i].replace?b.fn.dataTable.util.escapeRegex(d[i]):d[i];-1!==b.inArray(k,f)&&j.addClass(g.selected)}l.append(j)}g=b("<div/>").data("column",a).addClass(c.container).addClass(f.length?
|
|
|
|
c.active:"").append(b('<button type="button">×</button>').addClass(this.classes.clear)).append(b("<div/>").addClass(c.title).html(b(e.header()).text())).append(b("<div/>").addClass(c.scroller).append(l));c=this.dom.container;e=c.children().map(function(){if(b(this).data("column")==a)return this});e.length?e.replaceWith(g):b(c).append(g)}},_toggle:function(a){var c=this.classes,g=c.item.selected,e=this.s.dt,a=b(a),d=a.closest("div."+c.pane.container);a.toggleClass(g,!a.hasClass(g));a=d.find("li."+
|
|
|
|
g);0===a.length?(d.removeClass(c.pane.active),e.column(d.data("column")).search("").draw()):(d.addClass(c.pane.active),e.column(d.data("column")).search("^("+b.map(a,function(a){a=b(a).data("filter").toString();return b.fn.dataTable.util.escapeRegex(a)}).join("|")+")$",!0,!1).draw())},_variance:function(a){for(var a=b.map(a,function(a){return a}),c=a.length,g=0,e=0,d=c;e<d;e++)g+=a[e];for(var g=g/c,f=0,e=0,d=c;e<d;e++)f+=Math.pow(g-a[e],2);return f/(c-1)}});f.classes={container:"dt-searchPanes",clear:"clear",
|
|
|
|
pane:{active:"filtering",container:"pane",title:"title",scroller:"scroller"},item:{selected:"selected",label:"label",count:"count"}};f.defaults={container:function(a){return a.table().container()},columns:k,insert:"prepend",threshold:0.5};f.version="0.0.1";b.fn.dataTable.SearchPanes=f;b.fn.DataTable.SearchPanes=f;j.Api.register("searchPanes.rebuild()",function(){return this.iterator("table",function(a){a.searchPane&&a.searchPane.rebuild()})});b(h).on("init.dt",function(a,c){if("dt"===a.namespace){var d=
|
|
|
|
c.oInit.searchPane,e=j.defaults.searchPane;if(d||e)e=b.extend({},d,e),!1!==d&&new f(c,e)}})});
|