New - SearchPane: Add the ability to specify `options` as a function in the column options for `searchPane` which will be used as a callback to get the data to display.

Function passes in:
 1. DT instance
 2. Column index
Expects return:
 Of a DT instance with the data to be displayed in its data set
pull/409/head
Allan Jardine 6 years ago
parent b14c501aa1
commit d1d63afb57

@ -1,13 +1,13 @@
/*! SearchPane 0.0.1
* 2017 SpryMedia Ltd - datatables.net/license
/*! SearchPane 0.0.2
* 2018 SpryMedia Ltd - datatables.net/license
*/
/**
* @summary SearchPane
* @description Search Panes for DataTables columns
* @version 0.0.1
* @version 0.0.2
* @author SpryMedia Ltd (www.sprymedia.co.uk)
* @copyright Copyright 2017 SpryMedia Ltd.
* @copyright Copyright 2018 SpryMedia Ltd.
*
* This source file is free software, available under the following license:
* MIT license - http://datatables.net/license/mit
@ -145,7 +145,11 @@
var column = table.column(idx);
var colOpts = this._getOptions(idx);
var list = $('<ul/>');
var binData = colOpts.options ? new DataTable.Api(null, colOpts.options) : column.data();
var binData = typeof colOpts.options === 'function' ?
colOpts.options( table, idx ) :
colOpts.options ?
new DataTable.Api(null, colOpts.options) :
column.data();
var bins = this._binData(binData.flatten());
// Don't show the pane if there isn't enough variance in the data
@ -326,7 +330,7 @@
threshold: 0.5
};
SearchPanes.version = '0.0.1';
SearchPanes.version = '0.0.2';
$.fn.dataTable.SearchPanes = SearchPanes;
$.fn.DataTable.SearchPanes = SearchPanes;

@ -1,5 +1,5 @@
/*!
Copyright 2017 SpryMedia Ltd.
Copyright 2018 SpryMedia Ltd.
This source file is free software, available under the following license:
MIT license - http://datatables.net/license/mit
@ -9,18 +9,18 @@
or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
For details please refer to: http://www.datatables.net
SearchPane 0.0.1
2017 SpryMedia Ltd - datatables.net/license
SearchPane 0.0.2
2018 SpryMedia Ltd - datatables.net/license
*/
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.findInternal=function(a,f,c){a instanceof String&&(a=String(a));for(var g=a.length,d=0;d<g;d++){var k=a[d];if(f.call(c,k,d,a))return{i:d,v:k}}return{i:-1,v:void 0}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,f,c){a!=Array.prototype&&a!=Object.prototype&&(a[f]=c.value)};
$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(a,f,c,g){if(f){c=$jscomp.global;a=a.split(".");for(g=0;g<a.length-1;g++){var d=a[g];d in c||(c[d]={});c=c[d]}a=a[a.length-1];g=c[a];f=f(g);f!=g&&null!=f&&$jscomp.defineProperty(c,a,{configurable:!0,writable:!0,value:f})}};
$jscomp.polyfill("Array.prototype.find",function(a){return a?a:function(a,c){return $jscomp.findInternal(this,a,c).v}},"es6-impl","es3");
(function(a){"function"===typeof define&&define.amd?define(["jquery","datatables.net"],function(f){return a(f,window,document)}):"object"===typeof exports?module.exports=function(f,c){f||(f=window);c&&c.fn.dataTable||(c=require("datatables.net")(f,c).$);return a(c,f,f.document)}:a(jQuery,window,document)})(function(a,f,c,g){function d(b,h){var e=this;b=new k.Api(b);this.classes=a.extend(!0,{},d.classes);this.dom={container:a("<div/>").addClass(this.classes.container)};this.c=a.extend(!0,{},d.defaults,
h);this.s={dt:b};b.settings()[0].searchPane=this;b.columns(this.c.columns).eq(0).each(function(a){e._pane(a)});a(this.dom.container).on("click","li",function(){e._toggle(this)}).on("click","button."+this.classes.clear,function(){e._clear(a(this).closest("div."+e.classes.pane.container))});this._attach()}var k=a.fn.dataTable;a.extend(d.prototype,{rebuild:function(){var a=this;this.s.dt.columns(this.c.columns).eq(0).each(function(b){a._pane(b)})},_attach:function(){var b=this.c.container,b="function"===
typeof b?b(this.s.dt):b;"prepend"===this.c.insert?a(this.dom.container).prependTo(b):a(this.dom.container).appendTo(b)},_binData:function(a){for(var b={},e=0,c=a.length;e<c;e++){var l=a[e];l&&(b[l]?b[l]++:b[l]=1)}return b},_clear:function(a){var b=this.classes,e=b.item.selected;a.find("li."+e).removeClass(e);a.removeClass(b.pane.active);this.s.dt.column(a.data("column")).search("").draw()},_pane:function(b){var h=this.classes,e=h.item,h=h.pane,c=this.s.dt.column(b),l=this._getOptions(b),f=a("<ul/>"),
d=l.options?new k.Api(null,l.options):c.data(),l=this._binData(d.flatten());if(!(this._variance(l)<this.c.threshold)){for(var m=c.search(),m=m?m.substr(1,m.length-2).split("|"):[],d=d.unique().sort().toArray(),g=0,p=d.length;g<p;g++)if(d[g]){var n=a("<li/>").html('<span class="'+e.label+'">'+d[g]+"</span>").data("filter",d[g]).append(a("<span/>").addClass(e.count).html(l[d[g]]));if(m.length){var q=d[g].replace?a.fn.dataTable.util.escapeRegex(d[g]):d[g];-1!==a.inArray(q,m)&&n.addClass(e.selected)}f.append(n)}e=
a("<div/>").data("column",b).addClass(h.container).addClass(m.length?h.active:"").append(a('<button type="button">&times;</button>').addClass(this.classes.clear)).append(a("<div/>").addClass(h.title).html(a(c.header()).text())).append(a("<div/>").addClass(h.scroller).append(f));h=this.dom.container;c=h.children().map(function(){if(a(this).data("column")==b)return this});c.length?c.replaceWith(e):a(h).append(e)}},_getOptions:function(a){return this.s.dt.settings()[0].aoColumns[a].searchPane||{}},_toggle:function(b){var d=
this.classes,e=d.item.selected,c=this.s.dt;b=a(b);var f=b.closest("div."+d.pane.container),g=f.data("column"),k=this._getOptions(g);b.toggleClass(e,!b.hasClass(e));b=f.find("li."+e);0===b.length?(f.removeClass(d.pane.active),c.column(g).search("").draw()):"any"===k.match?(f.addClass(d.pane.active),c.column(g).search("("+a.map(b,function(b){b=a(b).data("filter").toString();return a.fn.dataTable.util.escapeRegex(b)}).join("|")+")",!0,!1).draw()):(f.addClass(d.pane.active),c.column(g).search("^("+a.map(b,
function(b){b=a(b).data("filter").toString();return a.fn.dataTable.util.escapeRegex(b)}).join("|")+")$",!0,!1).draw())},_variance:function(b){b=a.map(b,function(a,b){return a});for(var d=b.length,e=0,c=0,f=d;c<f;c++)e+=b[c];for(var e=e/d,g=0,c=0,f=d;c<f;c++)g+=Math.pow(e-b[c],2);return g/(d-1)}});d.classes={container:"dt-searchPanes",clear:"clear",pane:{active:"filtering",container:"pane",title:"title",scroller:"scroller"},item:{selected:"selected",label:"label",count:"count"}};d.defaults={container:function(a){return a.table().container()},
columns:g,insert:"prepend",threshold:.5};d.version="0.0.1";a.fn.dataTable.SearchPanes=d;a.fn.DataTable.SearchPanes=d;k.Api.register("searchPanes.rebuild()",function(){return this.iterator("table",function(a){a.searchPane&&a.searchPane.rebuild()})});k.Api.register("column().paneOptions()",function(a){return this.iterator("column",function(b,c){c=b.aoColumns[c];c.searchPane||(c.searchPane={});c.searchPane.options=a;b.searchPane&&b.searchPane.rebuild()})});a(c).on("init.dt",function(b,c,e){"dt"===b.namespace&&
(b=c.oInit.searchPane,e=k.defaults.searchPane,b||e)&&(e=a.extend({},b,e),!1!==b&&new d(c,e))})});
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.findInternal=function(a,c,d){a instanceof String&&(a=String(a));for(var f=a.length,e=0;e<f;e++){var m=a[e];if(c.call(d,m,e,a))return{i:e,v:m}}return{i:-1,v:void 0}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,d){a!=Array.prototype&&a!=Object.prototype&&(a[c]=d.value)};
$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(a,c,d,f){if(c){d=$jscomp.global;a=a.split(".");for(f=0;f<a.length-1;f++){var e=a[f];e in d||(d[e]={});d=d[e]}a=a[a.length-1];f=d[a];c=c(f);c!=f&&null!=c&&$jscomp.defineProperty(d,a,{configurable:!0,writable:!0,value:c})}};
$jscomp.polyfill("Array.prototype.find",function(a){return a?a:function(a,d){return $jscomp.findInternal(this,a,d).v}},"es6","es3");
(function(a){"function"===typeof define&&define.amd?define(["jquery","datatables.net"],function(c){return a(c,window,document)}):"object"===typeof exports?module.exports=function(c,d){c||(c=window);d&&d.fn.dataTable||(d=require("datatables.net")(c,d).$);return a(d,c,c.document)}:a(jQuery,window,document)})(function(a,c,d,f){function e(b,h){var g=this;b=new m.Api(b);this.classes=a.extend(!0,{},e.classes);this.dom={container:a("<div/>").addClass(this.classes.container)};this.c=a.extend(!0,{},e.defaults,
h);this.s={dt:b};b.settings()[0].searchPane=this;b.columns(this.c.columns).eq(0).each(function(a){g._pane(a)});a(this.dom.container).on("click","li",function(){g._toggle(this)}).on("click","button."+this.classes.clear,function(){g._clear(a(this).closest("div."+g.classes.pane.container))});this._attach()}var m=a.fn.dataTable;a.extend(e.prototype,{rebuild:function(){var a=this;this.s.dt.columns(this.c.columns).eq(0).each(function(b){a._pane(b)})},_attach:function(){var b=this.c.container;b="function"===
typeof b?b(this.s.dt):b;"prepend"===this.c.insert?a(this.dom.container).prependTo(b):a(this.dom.container).appendTo(b)},_binData:function(a){for(var b={},g=0,d=a.length;g<d;g++){var k=a[g];k&&(b[k]?b[k]++:b[k]=1)}return b},_clear:function(a){var b=this.classes,g=b.item.selected;a.find("li."+g).removeClass(g);a.removeClass(b.pane.active);this.s.dt.column(a.data("column")).search("").draw()},_pane:function(b){var h=this.classes,g=h.item;h=h.pane;var d=this.s.dt,k=d.column(b),c=this._getOptions(b),e=
a("<ul/>"),l="function"===typeof c.options?c.options(d,b):c.options?new m.Api(null,c.options):k.data();d=this._binData(l.flatten());if(!(this._variance(d)<this.c.threshold)){c=(c=k.search())?c.substr(1,c.length-2).split("|"):[];l=l.unique().sort().toArray();for(var f=0,p=l.length;f<p;f++)if(l[f]){var n=a("<li/>").html('<span class="'+g.label+'">'+l[f]+"</span>").data("filter",l[f]).append(a("<span/>").addClass(g.count).html(d[l[f]]));if(c.length){var q=l[f].replace?a.fn.dataTable.util.escapeRegex(l[f]):
l[f];-1!==a.inArray(q,c)&&n.addClass(g.selected)}e.append(n)}g=a("<div/>").data("column",b).addClass(h.container).addClass(c.length?h.active:"").append(a('<button type="button">&times;</button>').addClass(this.classes.clear)).append(a("<div/>").addClass(h.title).html(a(k.header()).text())).append(a("<div/>").addClass(h.scroller).append(e));h=this.dom.container;k=h.children().map(function(){if(a(this).data("column")==b)return this});k.length?k.replaceWith(g):a(h).append(g)}},_getOptions:function(a){return this.s.dt.settings()[0].aoColumns[a].searchPane||
{}},_toggle:function(b){var c=this.classes,g=c.item.selected,d=this.s.dt;b=a(b);var e=b.closest("div."+c.pane.container),f=e.data("column"),m=this._getOptions(f);b.toggleClass(g,!b.hasClass(g));b=e.find("li."+g);0===b.length?(e.removeClass(c.pane.active),d.column(f).search("").draw()):"any"===m.match?(e.addClass(c.pane.active),d.column(f).search("("+a.map(b,function(b){b=a(b).data("filter").toString();return a.fn.dataTable.util.escapeRegex(b)}).join("|")+")",!0,!1).draw()):(e.addClass(c.pane.active),
d.column(f).search("^("+a.map(b,function(b){b=a(b).data("filter").toString();return a.fn.dataTable.util.escapeRegex(b)}).join("|")+")$",!0,!1).draw())},_variance:function(b){b=a.map(b,function(a,b){return a});for(var c=b.length,g=0,d=0,e=c;d<e;d++)g+=b[d];g/=c;var f=0;d=0;for(e=c;d<e;d++)f+=Math.pow(g-b[d],2);return f/(c-1)}});e.classes={container:"dt-searchPanes",clear:"clear",pane:{active:"filtering",container:"pane",title:"title",scroller:"scroller"},item:{selected:"selected",label:"label",count:"count"}};
e.defaults={container:function(a){return a.table().container()},columns:f,insert:"prepend",threshold:.5};e.version="0.0.2";a.fn.dataTable.SearchPanes=e;a.fn.DataTable.SearchPanes=e;m.Api.register("searchPanes.rebuild()",function(){return this.iterator("table",function(a){a.searchPane&&a.searchPane.rebuild()})});m.Api.register("column().paneOptions()",function(a){return this.iterator("column",function(b,c){c=b.aoColumns[c];c.searchPane||(c.searchPane={});c.searchPane.options=a;b.searchPane&&b.searchPane.rebuild()})});
a(d).on("init.dt",function(b,c,d){"dt"===b.namespace&&(b=c.oInit.searchPane,d=m.defaults.searchPane,b||d)&&(d=a.extend({},b,d),!1!==b&&new e(c,d))})});

Loading…
Cancel
Save