diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..5562411 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,10 @@ +{ + "arrowParens": "always", + "printWidth": 80, + "proseWrap": "preserve", + "quoteProps": "as-needed", + "semi": true, + "singleQuote": true, + "trailingComma": "es5", + "useTabs": true +} diff --git a/api/average().d.ts b/api/average().d.ts new file mode 100644 index 0000000..2e27712 --- /dev/null +++ b/api/average().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface Api { + /** Average the values in a data set. */ + average(): Number; + } +} +export {}; diff --git a/api/average().js b/api/average().js index cb400d6..bc0eeb3 100644 --- a/api/average().js +++ b/api/average().js @@ -1,32 +1,67 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * It can sometimes be useful to get the average of data in an API result set, * be it from a column, or a collection of cells. This method provides exactly * that ability. * - * @name average() - * @summary Average the values in a data set. - * @author [Allan Jardine](http://sprymedia.co.uk) - * @requires DataTables 1.10+ + * @name average() + * @summary Average the values in a data set. + * @author [Allan Jardine](http://sprymedia.co.uk) + * @requires DataTables 1.10+ * * @returns {Number} Calculated average * - * @example + * @example * // Average a column * var table = $('#example').DataTable(); * table.column( 3 ).data().average(); * - * @example + * @example * // Average two cells * var table = $('#example').DataTable(); * table.cells( 0, [3,4] ).data().average(); */ - -jQuery.fn.dataTable.Api.register( 'average()', function () { +DataTable.Api.register('average()', function () { var data = this.flatten(); - var sum = data.reduce( function ( a, b ) { - return (a*1) + (b*1); // cast values in-case they are strings - }, 0 ); - + var sum = data.reduce(function (a, b) { + return a * 1 + b * 1; // cast values in-case they are strings + }, 0); return sum / data.length; -} ); +}); + +return DataTable; +})); diff --git a/api/average().min.js b/api/average().min.js new file mode 100644 index 0000000..d5f898a --- /dev/null +++ b/api/average().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(e,t,n,r){"use strict";e=e.fn.dataTable;return e.Api.register("average()",function(){var e=this.flatten();return e.reduce(function(e,t){return+e+ +t},0)/e.length}),e}); \ No newline at end of file diff --git a/api/average().min.mjs b/api/average().min.mjs new file mode 100644 index 0000000..d87c260 --- /dev/null +++ b/api/average().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("average()",function(){var t=this.flatten();return t.reduce(function(t,a){return+t+ +a},0)/t.length});export default DataTable; \ No newline at end of file diff --git a/api/average().mjs b/api/average().mjs new file mode 100644 index 0000000..0eefc93 --- /dev/null +++ b/api/average().mjs @@ -0,0 +1,37 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * It can sometimes be useful to get the average of data in an API result set, + * be it from a column, or a collection of cells. This method provides exactly + * that ability. + * + * @name average() + * @summary Average the values in a data set. + * @author [Allan Jardine](http://sprymedia.co.uk) + * @requires DataTables 1.10+ + * + * @returns {Number} Calculated average + * + * @example + * // Average a column + * var table = $('#example').DataTable(); + * table.column( 3 ).data().average(); + * + * @example + * // Average two cells + * var table = $('#example').DataTable(); + * table.cells( 0, [3,4] ).data().average(); + */ +DataTable.Api.register('average()', function () { + var data = this.flatten(); + var sum = data.reduce(function (a, b) { + return a * 1 + b * 1; // cast values in-case they are strings + }, 0); + return sum / data.length; +}); + + +export default DataTable; diff --git a/api/column().title().d.ts b/api/column().title().d.ts new file mode 100644 index 0000000..074bf15 --- /dev/null +++ b/api/column().title().d.ts @@ -0,0 +1,8 @@ +/*! © Alejandro Navarro - datatables.net/license */ +declare module 'datatables.net' { + interface ApiColumnMethods { + /** Get the title of a column */ + title(): string; + } +} +export {}; diff --git a/api/column().title().js b/api/column().title().js index 56394ea..28b96fc 100644 --- a/api/column().title().js +++ b/api/column().title().js @@ -1,3 +1,37 @@ +/*! © Alejandro Navarro - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * This plug-in will read the text from the header cell of a column, returning * that value. @@ -9,13 +43,16 @@ * * @returns {String} Column title * - * @example + * @example * // Read the title text of column index 3 * var table = $('#example').DataTable(); * table.column( 3 ).title(); */ +DataTable.Api.register('column().title()', function () { + var title = this.header(); + return $(title).text().trim(); +}); + -$.fn.dataTable.Api.register( 'column().title()', function () { - var colheader = this.header(); - return $(colheader).text().trim(); -} ); \ No newline at end of file +return DataTable; +})); diff --git a/api/column().title().min.js b/api/column().title().min.js new file mode 100644 index 0000000..f98ea2f --- /dev/null +++ b/api/column().title().min.js @@ -0,0 +1,2 @@ +/*! © Alejandro Navarro - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(t,e,n,r){"use strict";var i=t.fn.dataTable;return i.Api.register("column().title()",function(){var e=this.header();return t(e).text().trim()}),i}); \ No newline at end of file diff --git a/api/column().title().min.mjs b/api/column().title().min.mjs new file mode 100644 index 0000000..473090b --- /dev/null +++ b/api/column().title().min.mjs @@ -0,0 +1,2 @@ +/*! © Alejandro Navarro - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("column().title()",function(){var t=this.header();return $(t).text().trim()});export default DataTable; \ No newline at end of file diff --git a/api/column().title().mjs b/api/column().title().mjs new file mode 100644 index 0000000..e60efc6 --- /dev/null +++ b/api/column().title().mjs @@ -0,0 +1,28 @@ +/*! © Alejandro Navarro - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * This plug-in will read the text from the header cell of a column, returning + * that value. + * + * @name column().title() + * @summary Get the title of a column + * @author Alejandro Navarro + * @requires DataTables 1.10+ + * + * @returns {String} Column title + * + * @example + * // Read the title text of column index 3 + * var table = $('#example').DataTable(); + * table.column( 3 ).title(); + */ +DataTable.Api.register('column().title()', function () { + var title = this.header(); + return $(title).text().trim(); +}); + + +export default DataTable; diff --git a/api/columns().order().d.ts b/api/columns().order().d.ts new file mode 100644 index 0000000..432860f --- /dev/null +++ b/api/columns().order().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface ApiColumnsMethods { + /** pply multi-column ordering through the columns() */ + order(dir: 'asc' | 'desc'): Api; + } +} +export {}; diff --git a/api/columns().order().js b/api/columns().order().js index f169848..780a748 100644 --- a/api/columns().order().js +++ b/api/columns().order().js @@ -1,3 +1,37 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * The DataTables core library provides the ability to set the ordering via the * `dt-api column().order()` method, but there is no plural equivalent. While @@ -8,39 +42,40 @@ * multi-column ordering, while retaining the benefits of the `dt-api columns()` * selector options. * - * @name columns().order() - * @summary Apply multi-column ordering through the columns() API method. - * @author [Allan Jardine](http://sprymedia.co.uk) - * @requires DataTables 1.10+ - * @param {string|array} dir The order to apply to the columns selected. This - * can be a string (`asc` or `desc`) which will be applied to all columns, - * or an array (again `asc` or `desc` as the elements in the array) which is - * the same length as the number of columns selected, and will be applied to - * the columns in sequence. + * @name columns().order() + * @summary Apply multi-column ordering through the columns() API method. + * @author [Allan Jardine](http://sprymedia.co.uk) + * @requires DataTables 1.10+ + * @param {string|array} dir The order to apply to the columns selected. This + * can be a string (`asc` or `desc`) which will be applied to all columns, + * or an array (again `asc` or `desc` as the elements in the array) which is + * the same length as the number of columns selected, and will be applied to + * the columns in sequence. * * @returns {DataTables.Api} DataTables API instance * - * @example - * // Apply multi-column sorting with a common direction - * table.columns( [ 1, 2 ] ).order( 'desc' ).draw(); + * @example + * // Apply multi-column sorting with a common direction + * table.columns( [ 1, 2 ] ).order( 'desc' ).draw(); * - * @example - * // Multi-column sorting with individual direction for the columns - * table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw(); + * @example + * // Multi-column sorting with individual direction for the columns + * table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw(); * - * @example - * // Multi-column sorting based on a name selector - * table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw(); + * @example + * // Multi-column sorting based on a name selector + * table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw(); */ +DataTable.Api.register('columns().order()', function (dir) { + return this.iterator('columns', function (settings, columns) { + var a = []; + for (var i = 0, ien = columns.length; i < ien; i++) { + a.push([columns[i], Array.isArray(dir) ? dir[i] : dir]); + } + new DataTable.Api(settings).order(a); + }); +}); + -$.fn.dataTable.Api.register( 'columns().order()', function ( dir ) { - return this.iterator( 'columns', function ( settings, columns ) { - var a = []; - - for ( var i=0, ien=columns.length ; i; + } +} +export {}; diff --git a/api/order.neutral().js b/api/order.neutral().js index 1adae3b..5bf905e 100644 --- a/api/order.neutral().js +++ b/api/order.neutral().js @@ -1,10 +1,44 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * This function will restore the order in which data was read into a DataTable * (for example from an HTML source). Although you can set `dt-api order()` to * be an empty array (`[]`) in order to prevent sorting during initialisation, * it can sometimes be useful to restore the original order after sorting has * already occurred - which is exactly what this function does. - * + * * Please note that this plug-in can only be used for client-side processing * tables (i.e. without `serverSide: true`). * @@ -19,15 +53,18 @@ * // Return table to the loaded data order * table.order.neutral().draw(); */ +DataTable.Api.register('order.neutral()', function () { + return this.iterator('table', function (s) { + s.aaSorting.length = 0; + s.aiDisplay.sort(function (a, b) { + return a - b; + }); + s.aiDisplayMaster.sort(function (a, b) { + return a - b; + }); + }); +}); -$.fn.dataTable.Api.register( 'order.neutral()', function () { - return this.iterator( 'table', function ( s ) { - s.aaSorting.length = 0; - s.aiDisplay.sort( function (a,b) { - return a-b; - } ); - s.aiDisplayMaster.sort( function (a,b) { - return a-b; - } ); - } ); -} ); + +return DataTable; +})); diff --git a/api/order.neutral().min.js b/api/order.neutral().min.js new file mode 100644 index 0000000..340a1a6 --- /dev/null +++ b/api/order.neutral().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(e){"function"==typeof define&&define.amd?define(["datatables.net"],function(t){return e(t,window,document)}):"object"==typeof exports?module.exports=function(t,n){return t=t||window,n.fn.dataTable||require("datatables.net")(t,n),e(n,0,t.document)}:e(jQuery,window,document)}(function(t,n,e,r){"use strict";t=t.fn.dataTable;return t.Api.register("order.neutral()",function(){return this.iterator("table",function(t){t.aaSorting.length=0,t.aiDisplay.sort(function(t,n){return t-n}),t.aiDisplayMaster.sort(function(t,n){return t-n})})}),t}); \ No newline at end of file diff --git a/api/order.neutral().min.mjs b/api/order.neutral().min.mjs new file mode 100644 index 0000000..8925d30 --- /dev/null +++ b/api/order.neutral().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("order.neutral()",function(){return this.iterator("table",function(t){t.aaSorting.length=0,t.aiDisplay.sort(function(t,a){return t-a}),t.aiDisplayMaster.sort(function(t,a){return t-a})})});export default DataTable; \ No newline at end of file diff --git a/api/order.neutral().mjs b/api/order.neutral().mjs new file mode 100644 index 0000000..1946644 --- /dev/null +++ b/api/order.neutral().mjs @@ -0,0 +1,40 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * This function will restore the order in which data was read into a DataTable + * (for example from an HTML source). Although you can set `dt-api order()` to + * be an empty array (`[]`) in order to prevent sorting during initialisation, + * it can sometimes be useful to restore the original order after sorting has + * already occurred - which is exactly what this function does. + * + * Please note that this plug-in can only be used for client-side processing + * tables (i.e. without `serverSide: true`). + * + * @name order.neutral() + * @summary Change ordering of the table to its data load order + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {DataTables.Api} DataTables API instance + * + * @example + * // Return table to the loaded data order + * table.order.neutral().draw(); + */ +DataTable.Api.register('order.neutral()', function () { + return this.iterator('table', function (s) { + s.aaSorting.length = 0; + s.aiDisplay.sort(function (a, b) { + return a - b; + }); + s.aiDisplayMaster.sort(function (a, b) { + return a - b; + }); + }); +}); + + +export default DataTable; diff --git a/api/page.jumpToData().d.ts b/api/page.jumpToData().d.ts new file mode 100644 index 0000000..d05a8a5 --- /dev/null +++ b/api/page.jumpToData().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface ApiPage { + /** Change ordering of the table to its data load order */ + jumpToData(data: any): Api; + } +} +export {}; diff --git a/api/page.jumpToData().js b/api/page.jumpToData().js index 8d29e7e..11fc960 100644 --- a/api/page.jumpToData().js +++ b/api/page.jumpToData().js @@ -1,3 +1,37 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * It can be quite useful to jump straight to a page which contains a certain * piece of data (a user name for example). This plug-in provides exactly that @@ -12,27 +46,28 @@ * automatically call `dt-api draw()` to redraw the table with the current page * shown. * - * @name page.JumpToData() - * @summary Jump to a page by searching for data from a column - * @author [Allan Jardine](http://sprymedia.co.uk) - * @requires DataTables 1.10+ + * @name page.JumpToData() + * @summary Jump to a page by searching for data from a column + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ * - * @param {*} data Data to search for - * @param {integer} column Column index - * @returns {Api} DataTables API instance + * @param {*} data Data to search for + * @param {integer} column Column index + * @returns {Api} DataTables API instance * - * @example + * @example * var table = $('#example').DataTable(); * table.page.jumpToData( "Allan Jardine", 0 ); */ +DataTable.Api.register('page.jumpToData()', function (data, column) { + var pos = this.column(column, { order: 'current' }).data().indexOf(data); + if (pos >= 0) { + var page = Math.floor(pos / this.page.info().length); + this.page(page).draw(false); + } + return this; +}); -jQuery.fn.dataTable.Api.register( 'page.jumpToData()', function ( data, column ) { - var pos = this.column(column, {order:'current'}).data().indexOf( data ); - - if ( pos >= 0 ) { - var page = Math.floor( pos / this.page.info().length ); - this.page( page ).draw( false ); - } - return this; -} ); \ No newline at end of file +return DataTable; +})); diff --git a/api/page.jumpToData().min.js b/api/page.jumpToData().min.js new file mode 100644 index 0000000..38b01e7 --- /dev/null +++ b/api/page.jumpToData().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(e,t,n,o){"use strict";e=e.fn.dataTable;return e.Api.register("page.jumpToData()",function(e,t){t=this.column(t,{order:"current"}).data().indexOf(e);return 0<=t&&(e=Math.floor(t/this.page.info().length),this.page(e).draw(!1)),this}),e}); \ No newline at end of file diff --git a/api/page.jumpToData().min.mjs b/api/page.jumpToData().min.mjs new file mode 100644 index 0000000..e26d083 --- /dev/null +++ b/api/page.jumpToData().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("page.jumpToData()",function(a,t){t=this.column(t,{order:"current"}).data().indexOf(a);return 0<=t&&(a=Math.floor(t/this.page.info().length),this.page(a).draw(!1)),this});export default DataTable; \ No newline at end of file diff --git a/api/page.jumpToData().mjs b/api/page.jumpToData().mjs new file mode 100644 index 0000000..454131d --- /dev/null +++ b/api/page.jumpToData().mjs @@ -0,0 +1,43 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * It can be quite useful to jump straight to a page which contains a certain + * piece of data (a user name for example). This plug-in provides exactly that + * ability, searching for a given data parameter from a given column and + * immediately shifting the paging of the table to jump to that point. + * + * If multiple data points match the requested data, the paging will be shifted + * to show the first instance. If there are no matches, the paging will not + * change. + * + * Note that unlike the core DataTables API methods, this plug-in will + * automatically call `dt-api draw()` to redraw the table with the current page + * shown. + * + * @name page.JumpToData() + * @summary Jump to a page by searching for data from a column + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @param {*} data Data to search for + * @param {integer} column Column index + * @returns {Api} DataTables API instance + * + * @example + * var table = $('#example').DataTable(); + * table.page.jumpToData( "Allan Jardine", 0 ); + */ +DataTable.Api.register('page.jumpToData()', function (data, column) { + var pos = this.column(column, { order: 'current' }).data().indexOf(data); + if (pos >= 0) { + var page = Math.floor(pos / this.page.info().length); + this.page(page).draw(false); + } + return this; +}); + + +export default DataTable; diff --git a/api/processing().d.ts b/api/processing().d.ts new file mode 100644 index 0000000..00874ff --- /dev/null +++ b/api/processing().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface Api { + /** Show / hide the processing indicator. */ + processing(boolean: any): Api; + } +} +export {}; diff --git a/api/processing().js b/api/processing().js index 84164aa..628744b 100644 --- a/api/processing().js +++ b/api/processing().js @@ -1,33 +1,70 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * Externally trigger the display of DataTables' "processing" indicator. - * + * * Please note that of DataTables 2.0.0 this functionality is now built into * DataTables core and this plug-in is no longer required. * - * @name processing() - * @summary Show / hide the processing indicator via the API - * @author [Allan Jardine](http://datatables.net) - * @requires DataTables 1.10+ - * @param {boolean} show `true` to show the processing indicator, `false` to - * hide it. + * @name processing() + * @summary Show / hide the processing indicator via the API + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * @param {boolean} show `true` to show the processing indicator, `false` to + * hide it. * * @returns {DataTables.Api} Unmodified API instance * - * @example + * @example * // Show a processing indicator for two seconds on initialisation * var table = $('#example').DataTable( { * processing: true * } ); - * + * * table.processing( true ); - * + * * setTimeout( function () { * table.processing( false ); * }, 2000 ); */ +DataTable.Api.register('processing()', function (show) { + return this.iterator('table', function (ctx) { + ctx.oApi._fnProcessingDisplay(ctx, show); + }); +}); + -jQuery.fn.dataTable.Api.register( 'processing()', function ( show ) { - return this.iterator( 'table', function ( ctx ) { - ctx.oApi._fnProcessingDisplay( ctx, show ); - } ); -} ); +return DataTable; +})); diff --git a/api/processing().min.js b/api/processing().min.js new file mode 100644 index 0000000..346d365 --- /dev/null +++ b/api/processing().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(t){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return t(e,window,document)}):"object"==typeof exports?module.exports=function(e,n){return e=e||window,n.fn.dataTable||require("datatables.net")(e,n),t(n,0,e.document)}:t(jQuery,window,document)}(function(e,n,t,i){"use strict";e=e.fn.dataTable;return e.Api.register("processing()",function(n){return this.iterator("table",function(e){e.oApi._fnProcessingDisplay(e,n)})}),e}); \ No newline at end of file diff --git a/api/processing().min.mjs b/api/processing().min.mjs new file mode 100644 index 0000000..c9e19bc --- /dev/null +++ b/api/processing().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("processing()",function(a){return this.iterator("table",function(t){t.oApi._fnProcessingDisplay(t,a)})});export default DataTable; \ No newline at end of file diff --git a/api/processing().mjs b/api/processing().mjs new file mode 100644 index 0000000..68d5628 --- /dev/null +++ b/api/processing().mjs @@ -0,0 +1,40 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * Externally trigger the display of DataTables' "processing" indicator. + * + * Please note that of DataTables 2.0.0 this functionality is now built into + * DataTables core and this plug-in is no longer required. + * + * @name processing() + * @summary Show / hide the processing indicator via the API + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * @param {boolean} show `true` to show the processing indicator, `false` to + * hide it. + * + * @returns {DataTables.Api} Unmodified API instance + * + * @example + * // Show a processing indicator for two seconds on initialisation + * var table = $('#example').DataTable( { + * processing: true + * } ); + * + * table.processing( true ); + * + * setTimeout( function () { + * table.processing( false ); + * }, 2000 ); + */ +DataTable.Api.register('processing()', function (show) { + return this.iterator('table', function (ctx) { + ctx.oApi._fnProcessingDisplay(ctx, show); + }); +}); + + +export default DataTable; diff --git a/api/row().show().d.ts b/api/row().show().d.ts new file mode 100644 index 0000000..4d47c72 --- /dev/null +++ b/api/row().show().d.ts @@ -0,0 +1,8 @@ +/*! © Edouard Labre - datatables.net/license */ +declare module 'datatables.net' { + interface ApiRowMethods { + /** See the row in datable by display the right pagination page */ + show(): Api; + } +} +export {}; diff --git a/api/row().show().js b/api/row().show().js index da93c42..902eb2d 100644 --- a/api/row().show().js +++ b/api/row().show().js @@ -1,15 +1,49 @@ +/*! © Edouard Labre - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** - * This plugin jumps to the right page of the DataTable to show the required row + * This plugin jumps to the right page of the DataTable to show the required row * - * @version 1.0 - * @name row().show() - * @summary See the row in datable by display the right pagination page - * @author [Edouard Labre](http://www.edouardlabre.com) + * @version 1.0 + * @name row().show() + * @summary See the row in datable by display the right pagination page + * @author [Edouard Labre](http://www.edouardlabre.com) * - * @param {void} a row must be selected - * @returns {DataTables.Api.Rows} DataTables Rows API instance + * @param {void} a row must be selected + * @returns {DataTables.Api.Rows} DataTables Rows API instance * - * @example + * @example * // Add an element to a huge table and go to the right pagination page * var table = $('#example').DataTable(); * var new_row = { @@ -20,23 +54,25 @@ * * table.row.add( new_row ).draw().show().draw(false); */ -$.fn.dataTable.Api.register('row().show()', function() { - var page_info = this.table().page.info(); - // Get row index - var new_row_index = this.index(); - // Row position - var row_position = this.table() - .rows({ search: 'applied' })[0] - .indexOf(new_row_index); - // Already on right page ? - if ((row_position >= page_info.start && row_position < page_info.end) || row_position < 0) { - // Return row object - return this; - } - // Find page number - var page_to_display = Math.floor(row_position / this.table().page.len()); - // Go to that page - this.table().page(page_to_display); - // Return row object - return this; +DataTable.Api.register('row().show()', function () { + var page_info = this.table().page.info(); + // Get row index + var new_row_index = this.index(); + // Row position + var row_position = this.table().rows({ search: 'applied' })[0].indexOf(new_row_index); + // Already on right page ? + if ((row_position >= page_info.start && row_position < page_info.end) || row_position < 0) { + // Return row object + return this; + } + // Find page number + var page_to_display = Math.floor(row_position / this.table().page.len()); + // Go to that page + this.table().page(page_to_display); + // Return row object + return this; }); + + +return DataTable; +})); diff --git a/api/row().show().min.js b/api/row().show().min.js new file mode 100644 index 0000000..91e11ae --- /dev/null +++ b/api/row().show().min.js @@ -0,0 +1,2 @@ +/*! © Edouard Labre - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(e,t,n,i){"use strict";e=e.fn.dataTable;return e.Api.register("row().show()",function(){var e=this.table().page.info(),t=this.index(),t=this.table().rows({search:"applied"})[0].indexOf(t);return t>=e.start&&t=a.start&&t= page_info.start && row_position < page_info.end) || row_position < 0) { + // Return row object + return this; + } + // Find page number + var page_to_display = Math.floor(row_position / this.table().page.len()); + // Go to that page + this.table().page(page_to_display); + // Return row object + return this; +}); + + +export default DataTable; diff --git a/api/rows().generate().d.ts b/api/rows().generate().d.ts new file mode 100644 index 0000000..49f36fa --- /dev/null +++ b/api/rows().generate().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface ApiRowsMethods { + /** Create tr elements for rows which have not yet had their nodes created. */ + generate(): Api; + } +} +export {}; diff --git a/api/rows().generate().js b/api/rows().generate().js index ba4475f..2913353 100644 --- a/api/rows().generate().js +++ b/api/rows().generate().js @@ -1,22 +1,59 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * When using `-init deferRender` you might find that under a specific set of circumstances you * need the `-tag tr` element for a row which hasn't yet been drawn. This method can be used to * create the nodes for the rows which haven't yet been drawn. * - * @name rows().generate() - * @summary Create tr elements for rows which have not yet had their nodes created. - * @author [Allan Jardine](http://datatables.net) - * @requires DataTables 1.10+ + * @name rows().generate() + * @summary Create tr elements for rows which have not yet had their nodes created. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ * * @returns {DataTable.Api} DataTables API instance * - * @example + * @example * // Create nodes for all rows * table.rows().generate(); */ +DataTable.Api.register('rows().generate()', function () { + return this.iterator('row', function (context, index) { + context.oApi._fnCreateTr(context, index); + }); +}); + - $.fn.dataTable.Api.register( 'rows().generate()', function () { - return this.iterator( 'row', function ( context, index ) { - context.oApi._fnCreateTr( context, index ); - } ); -} ); \ No newline at end of file +return DataTable; +})); diff --git a/api/rows().generate().min.js b/api/rows().generate().min.js new file mode 100644 index 0000000..332a19b --- /dev/null +++ b/api/rows().generate().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(e,t,n,o){"use strict";e=e.fn.dataTable;return e.Api.register("rows().generate()",function(){return this.iterator("row",function(e,t){e.oApi._fnCreateTr(e,t)})}),e}); \ No newline at end of file diff --git a/api/rows().generate().min.mjs b/api/rows().generate().min.mjs new file mode 100644 index 0000000..66c3dc2 --- /dev/null +++ b/api/rows().generate().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("rows().generate()",function(){return this.iterator("row",function(t,a){t.oApi._fnCreateTr(t,a)})});export default DataTable; \ No newline at end of file diff --git a/api/rows().generate().mjs b/api/rows().generate().mjs new file mode 100644 index 0000000..76e5c37 --- /dev/null +++ b/api/rows().generate().mjs @@ -0,0 +1,29 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * When using `-init deferRender` you might find that under a specific set of circumstances you + * need the `-tag tr` element for a row which hasn't yet been drawn. This method can be used to + * create the nodes for the rows which haven't yet been drawn. + * + * @name rows().generate() + * @summary Create tr elements for rows which have not yet had their nodes created. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {DataTable.Api} DataTables API instance + * + * @example + * // Create nodes for all rows + * table.rows().generate(); + */ +DataTable.Api.register('rows().generate()', function () { + return this.iterator('row', function (context, index) { + context.oApi._fnCreateTr(context, index); + }); +}); + + +export default DataTable; diff --git a/api/src/average().ts b/api/src/average().ts new file mode 100644 index 0000000..1296d7f --- /dev/null +++ b/api/src/average().ts @@ -0,0 +1,42 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * It can sometimes be useful to get the average of data in an API result set, + * be it from a column, or a collection of cells. This method provides exactly + * that ability. + * + * @name average() + * @summary Average the values in a data set. + * @author [Allan Jardine](http://sprymedia.co.uk) + * @requires DataTables 1.10+ + * + * @returns {Number} Calculated average + * + * @example + * // Average a column + * var table = $('#example').DataTable(); + * table.column( 3 ).data().average(); + * + * @example + * // Average two cells + * var table = $('#example').DataTable(); + * table.cells( 0, [3,4] ).data().average(); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface Api { + /** Average the values in a data set. */ + average(): Number; + } +} + +DataTable.Api.register('average()', function () { + var data = this.flatten(); + var sum = data.reduce(function (a, b) { + return a * 1 + b * 1; // cast values in-case they are strings + }, 0); + + return sum / data.length; +}); diff --git a/api/src/column().title().ts b/api/src/column().title().ts new file mode 100644 index 0000000..cb0df39 --- /dev/null +++ b/api/src/column().title().ts @@ -0,0 +1,32 @@ +/*! © Alejandro Navarro - datatables.net/license */ + +/** + * This plug-in will read the text from the header cell of a column, returning + * that value. + * + * @name column().title() + * @summary Get the title of a column + * @author Alejandro Navarro + * @requires DataTables 1.10+ + * + * @returns {String} Column title + * + * @example + * // Read the title text of column index 3 + * var table = $('#example').DataTable(); + * table.column( 3 ).title(); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiColumnMethods { + /** Get the title of a column */ + title(): string; + } +} + +DataTable.Api.register('column().title()', function () { + var title = this.header(); + return $(title).text().trim(); +}); diff --git a/api/src/columns().order().ts b/api/src/columns().order().ts new file mode 100644 index 0000000..45282f0 --- /dev/null +++ b/api/src/columns().order().ts @@ -0,0 +1,57 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * The DataTables core library provides the ability to set the ordering via the + * `dt-api column().order()` method, but there is no plural equivalent. While + * multi-column ordering can be set using `dt-api order()` that method requires + * that column indexes be used. + * + * This plug-in provides the plural `columns().order()` method so you can set + * multi-column ordering, while retaining the benefits of the `dt-api columns()` + * selector options. + * + * @name columns().order() + * @summary Apply multi-column ordering through the columns() API method. + * @author [Allan Jardine](http://sprymedia.co.uk) + * @requires DataTables 1.10+ + * @param {string|array} dir The order to apply to the columns selected. This + * can be a string (`asc` or `desc`) which will be applied to all columns, + * or an array (again `asc` or `desc` as the elements in the array) which is + * the same length as the number of columns selected, and will be applied to + * the columns in sequence. + * + * @returns {DataTables.Api} DataTables API instance + * + * @example + * // Apply multi-column sorting with a common direction + * table.columns( [ 1, 2 ] ).order( 'desc' ).draw(); + * + * @example + * // Multi-column sorting with individual direction for the columns + * table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw(); + * + * @example + * // Multi-column sorting based on a name selector + * table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw(); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiColumnsMethods { + /** pply multi-column ordering through the columns() */ + order(dir: 'asc' | 'desc'): Api; + } +} + +DataTable.Api.register('columns().order()', function (dir) { + return this.iterator('columns', function (settings, columns) { + var a: [number, string][] = []; + + for (var i = 0, ien = columns.length; i < ien; i++) { + a.push([columns[i], Array.isArray(dir) ? dir[i] : dir]); + } + + new DataTable.Api(settings).order(a); + }); +}); diff --git a/api/src/order.neutral().ts b/api/src/order.neutral().ts new file mode 100644 index 0000000..89c1ffa --- /dev/null +++ b/api/src/order.neutral().ts @@ -0,0 +1,44 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * This function will restore the order in which data was read into a DataTable + * (for example from an HTML source). Although you can set `dt-api order()` to + * be an empty array (`[]`) in order to prevent sorting during initialisation, + * it can sometimes be useful to restore the original order after sorting has + * already occurred - which is exactly what this function does. + * + * Please note that this plug-in can only be used for client-side processing + * tables (i.e. without `serverSide: true`). + * + * @name order.neutral() + * @summary Change ordering of the table to its data load order + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {DataTables.Api} DataTables API instance + * + * @example + * // Return table to the loaded data order + * table.order.neutral().draw(); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiOrder { + /** Change ordering of the table to its data load order */ + neutral(): Api; + } +} + +DataTable.Api.register('order.neutral()', function () { + return this.iterator('table', function (s) { + s.aaSorting.length = 0; + s.aiDisplay.sort(function (a, b) { + return a - b; + }); + s.aiDisplayMaster.sort(function (a, b) { + return a - b; + }); + }); +}); diff --git a/api/src/page.jumpToData().ts b/api/src/page.jumpToData().ts new file mode 100644 index 0000000..957e950 --- /dev/null +++ b/api/src/page.jumpToData().ts @@ -0,0 +1,49 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * It can be quite useful to jump straight to a page which contains a certain + * piece of data (a user name for example). This plug-in provides exactly that + * ability, searching for a given data parameter from a given column and + * immediately shifting the paging of the table to jump to that point. + * + * If multiple data points match the requested data, the paging will be shifted + * to show the first instance. If there are no matches, the paging will not + * change. + * + * Note that unlike the core DataTables API methods, this plug-in will + * automatically call `dt-api draw()` to redraw the table with the current page + * shown. + * + * @name page.JumpToData() + * @summary Jump to a page by searching for data from a column + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @param {*} data Data to search for + * @param {integer} column Column index + * @returns {Api} DataTables API instance + * + * @example + * var table = $('#example').DataTable(); + * table.page.jumpToData( "Allan Jardine", 0 ); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiPage { + /** Change ordering of the table to its data load order */ + jumpToData(data: any): Api; + } +} + +DataTable.Api.register('page.jumpToData()', function (data, column) { + var pos = this.column(column, { order: 'current' }).data().indexOf(data); + + if (pos >= 0) { + var page = Math.floor(pos / this.page.info().length); + this.page(page).draw(false); + } + + return this; +}); diff --git a/api/src/processing().ts b/api/src/processing().ts new file mode 100644 index 0000000..8689283 --- /dev/null +++ b/api/src/processing().ts @@ -0,0 +1,44 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * Externally trigger the display of DataTables' "processing" indicator. + * + * Please note that of DataTables 2.0.0 this functionality is now built into + * DataTables core and this plug-in is no longer required. + * + * @name processing() + * @summary Show / hide the processing indicator via the API + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * @param {boolean} show `true` to show the processing indicator, `false` to + * hide it. + * + * @returns {DataTables.Api} Unmodified API instance + * + * @example + * // Show a processing indicator for two seconds on initialisation + * var table = $('#example').DataTable( { + * processing: true + * } ); + * + * table.processing( true ); + * + * setTimeout( function () { + * table.processing( false ); + * }, 2000 ); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface Api { + /** Show / hide the processing indicator. */ + processing(boolean): Api; + } +} + +DataTable.Api.register('processing()', function (show) { + return this.iterator('table', function (ctx) { + ctx.oApi._fnProcessingDisplay(ctx, show); + }); +}); diff --git a/api/src/row().show().ts b/api/src/row().show().ts new file mode 100644 index 0000000..cc35e18 --- /dev/null +++ b/api/src/row().show().ts @@ -0,0 +1,55 @@ +/*! © Edouard Labre - datatables.net/license */ + +/** + * This plugin jumps to the right page of the DataTable to show the required row + * + * @version 1.0 + * @name row().show() + * @summary See the row in datable by display the right pagination page + * @author [Edouard Labre](http://www.edouardlabre.com) + * + * @param {void} a row must be selected + * @returns {DataTables.Api.Rows} DataTables Rows API instance + * + * @example + * // Add an element to a huge table and go to the right pagination page + * var table = $('#example').DataTable(); + * var new_row = { + * DT_RowId: 'row_example', + * name: 'example', + * value: 'an example row' + * }; + * + * table.row.add( new_row ).draw().show().draw(false); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiRowMethods { + /** See the row in datable by display the right pagination page */ + show(): Api; + } +} + +DataTable.Api.register('row().show()', function () { + var page_info = this.table().page.info(); + // Get row index + var new_row_index = this.index(); + // Row position + var row_position = this.table().rows({ search: 'applied' })[0].indexOf(new_row_index); + + // Already on right page ? + if ((row_position >= page_info.start && row_position < page_info.end) || row_position < 0) { + // Return row object + return this; + } + + // Find page number + var page_to_display = Math.floor(row_position / this.table().page.len()); + // Go to that page + this.table().page(page_to_display); + + // Return row object + return this; +}); diff --git a/api/src/rows().generate().ts b/api/src/rows().generate().ts new file mode 100644 index 0000000..94c68d7 --- /dev/null +++ b/api/src/rows().generate().ts @@ -0,0 +1,33 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * When using `-init deferRender` you might find that under a specific set of circumstances you + * need the `-tag tr` element for a row which hasn't yet been drawn. This method can be used to + * create the nodes for the rows which haven't yet been drawn. + * + * @name rows().generate() + * @summary Create tr elements for rows which have not yet had their nodes created. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {DataTable.Api} DataTables API instance + * + * @example + * // Create nodes for all rows + * table.rows().generate(); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface ApiRowsMethods { + /** Create tr elements for rows which have not yet had their nodes created. */ + generate(): Api; + } +} + +DataTable.Api.register('rows().generate()', function () { + return this.iterator('row', function (context, index) { + context.oApi._fnCreateTr(context, index); + }); +}); diff --git a/api/src/sum().ts b/api/src/sum().ts new file mode 100644 index 0000000..c6e8642 --- /dev/null +++ b/api/src/sum().ts @@ -0,0 +1,61 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +/** + * Fairly simply, this plug-in will take the data from an API result set + * and sum it, returning the summed value. The data can come from any data + * source, including column data, cells or rows. + * + * Note that it will attempt to 'deformat' any string based data that is passed + * into it - i.e. it will strip any non-numeric characters in order to make a + * best effort attempt to sum all data types. This can be useful when working + * with formatting numbers such as currency. However the trade-off is that no + * error is thrown if non-numeric data is passed in. You should be aware of this + * in case unexpected values are returned - likely the input data is not what is + * expected. + * + * @name sum() + * @summary Sum the values in a data set. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {Number} Summed value + * + * @example + * // Simply get the sum of a column + * var table = $('#example').DataTable(); + * table.column( 3 ).data().sum(); + * + * @example + * // Insert the sum of a column into the columns footer, for the visible + * // data on each draw + * $('#example').DataTable( { + * drawCallback: function () { + * var api = this.api(); + * $( api.table().footer() ).html( + * api.column( 4, {page:'current'} ).data().sum() + * ); + * } + * } ); + */ + +import DataTable from 'datatables.net'; + +declare module 'datatables.net' { + interface Api { + /** Sum the values in a data set. */ + sum(): Number; + } +} + +DataTable.Api.register('sum()', function () { + return this.flatten().reduce(function (a, b) { + if (typeof a === 'string') { + a = a.replace(/[^\d.-]/g, '') as any * 1; + } + if (typeof b === 'string') { + b = b.replace(/[^\d.-]/g, '') as any * 1; + } + + return a + b; + }, 0); +}); diff --git a/api/sum().d.ts b/api/sum().d.ts new file mode 100644 index 0000000..517c098 --- /dev/null +++ b/api/sum().d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface Api { + /** Sum the values in a data set. */ + sum(): Number; + } +} +export {}; diff --git a/api/sum().js b/api/sum().js index 856ebd6..181fba9 100644 --- a/api/sum().js +++ b/api/sum().js @@ -1,3 +1,37 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * Fairly simply, this plug-in will take the data from an API result set * and sum it, returning the summed value. The data can come from any data @@ -11,19 +45,19 @@ * in case unexpected values are returned - likely the input data is not what is * expected. * - * @name sum() - * @summary Sum the values in a data set. - * @author [Allan Jardine](http://sprymedia.co.uk) - * @requires DataTables 1.10+ + * @name sum() + * @summary Sum the values in a data set. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ * - * @returns {Number} Summed value + * @returns {Number} Summed value * - * @example + * @example * // Simply get the sum of a column * var table = $('#example').DataTable(); * table.column( 3 ).data().sum(); * - * @example + * @example * // Insert the sum of a column into the columns footer, for the visible * // data on each draw * $('#example').DataTable( { @@ -35,17 +69,18 @@ * } * } ); */ +DataTable.Api.register('sum()', function () { + return this.flatten().reduce(function (a, b) { + if (typeof a === 'string') { + a = a.replace(/[^\d.-]/g, '') * 1; + } + if (typeof b === 'string') { + b = b.replace(/[^\d.-]/g, '') * 1; + } + return a + b; + }, 0); +}); -jQuery.fn.dataTable.Api.register( 'sum()', function ( ) { - return this.flatten().reduce( function ( a, b ) { - if ( typeof a === 'string' ) { - a = a.replace(/[^\d.-]/g, '') * 1; - } - if ( typeof b === 'string' ) { - b = b.replace(/[^\d.-]/g, '') * 1; - } - - return a + b; - }, 0 ); -} ); +return DataTable; +})); diff --git a/api/sum().min.js b/api/sum().min.js new file mode 100644 index 0000000..a542f9c --- /dev/null +++ b/api/sum().min.js @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +!function(n){"function"==typeof define&&define.amd?define(["datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t.fn.dataTable||require("datatables.net")(e,t),n(t,0,e.document)}:n(jQuery,window,document)}(function(e,t,n,r){"use strict";e=e.fn.dataTable;return e.Api.register("sum()",function(){return this.flatten().reduce(function(e,t){return(e="string"==typeof e?+e.replace(/[^\d.-]/g,""):e)+(t="string"==typeof t?+t.replace(/[^\d.-]/g,""):t)},0)}),e}); \ No newline at end of file diff --git a/api/sum().min.mjs b/api/sum().min.mjs new file mode 100644 index 0000000..b5e6511 --- /dev/null +++ b/api/sum().min.mjs @@ -0,0 +1,2 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +import DataTable from"datatables.net";DataTable.Api.register("sum()",function(){return this.flatten().reduce(function(t,e){return(t="string"==typeof t?+t.replace(/[^\d.-]/g,""):t)+(e="string"==typeof e?+e.replace(/[^\d.-]/g,""):e)},0)});export default DataTable; \ No newline at end of file diff --git a/api/sum().mjs b/api/sum().mjs new file mode 100644 index 0000000..2da620e --- /dev/null +++ b/api/sum().mjs @@ -0,0 +1,56 @@ +/*! © SpryMedia Ltd - datatables.net/license */ + +import DataTable from 'datatables.net'; + + +/** + * Fairly simply, this plug-in will take the data from an API result set + * and sum it, returning the summed value. The data can come from any data + * source, including column data, cells or rows. + * + * Note that it will attempt to 'deformat' any string based data that is passed + * into it - i.e. it will strip any non-numeric characters in order to make a + * best effort attempt to sum all data types. This can be useful when working + * with formatting numbers such as currency. However the trade-off is that no + * error is thrown if non-numeric data is passed in. You should be aware of this + * in case unexpected values are returned - likely the input data is not what is + * expected. + * + * @name sum() + * @summary Sum the values in a data set. + * @author [Allan Jardine](http://datatables.net) + * @requires DataTables 1.10+ + * + * @returns {Number} Summed value + * + * @example + * // Simply get the sum of a column + * var table = $('#example').DataTable(); + * table.column( 3 ).data().sum(); + * + * @example + * // Insert the sum of a column into the columns footer, for the visible + * // data on each draw + * $('#example').DataTable( { + * drawCallback: function () { + * var api = this.api(); + * $( api.table().footer() ).html( + * api.column( 4, {page:'current'} ).data().sum() + * ); + * } + * } ); + */ +DataTable.Api.register('sum()', function () { + return this.flatten().reduce(function (a, b) { + if (typeof a === 'string') { + a = a.replace(/[^\d.-]/g, '') * 1; + } + if (typeof b === 'string') { + b = b.replace(/[^\d.-]/g, '') * 1; + } + return a + b; + }, 0); +}); + + +export default DataTable; diff --git a/buttons/button.download.d.ts b/buttons/button.download.d.ts new file mode 100644 index 0000000..2e27712 --- /dev/null +++ b/buttons/button.download.d.ts @@ -0,0 +1,8 @@ +/*! © SpryMedia Ltd - datatables.net/license */ +declare module 'datatables.net' { + interface Api { + /** Average the values in a data set. */ + average(): Number; + } +} +export {}; diff --git a/buttons/button.download.js b/buttons/button.download.js index 3620965..9e891fc 100644 --- a/buttons/button.download.js +++ b/buttons/button.download.js @@ -1,6 +1,36 @@ -/*! Download button for Buttons - * 2018 SpryMedia Ltd - MIT licensed - datatables.net/license - */ +/*! © SpryMedia Ltd - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + + return factory( $, root, root.document ); + }; + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document, undefined ) { +'use strict'; +var DataTable = $.fn.dataTable; + /** * @summary Download Button @@ -14,7 +44,7 @@ * is enabled in the host table it will automatically add the last parameters * used for a table draw allowing the script to output a file with the same * order / search applied as the main table. - * + * * This is particularly useful when using server-side processing and wishing * to allow user export of the data in a table. The default Buttons package * will only export the data available on the client-side, while this button @@ -33,81 +63,74 @@ * ] * }); */ -(function(window, document, $, undefined) { - function flattenJson(data, name, flattened) { - if (!flattened) { - flattened = {}; - } - - if (!name) { - name = ''; - } - - if ($.isPlainObject(data) || Array.isArray(data)) { - $.each(data, function(idx, val) { - if (name === '') { - flattenJson(val, idx, flattened); - } else { - flattenJson(val, name + '[' + idx + ']', flattened); - } - }); - } else { - flattened[name] = data; - } - - return flattened; - } - - $.fn.dataTable.ext.buttons.download = { - text: 'Download', - action: function(e, dt, node, config) { - // Gather information to be submitted - var data = {}; - - if (dt.page.info().serverSide) { - $.extend(data, dt.ajax.params()); - } - - if (typeof config.data === 'function') { - config.data(data); - } else if (typeof config.data === 'object') { - $.extend(data, config.data); - } - - // Convert data to a flat structure for submission - var flattened = flattenJson(data); - - // Create an iframe - var iframe = $('