| 4 |
lars |
1 |
/**
|
|
|
2 |
* Fairly simply, this plug-in will take the data from an API result set
|
|
|
3 |
* and sum it, returning the summed value. The data can come from any data
|
|
|
4 |
* source, including column data, cells or rows.
|
|
|
5 |
*
|
|
|
6 |
* @name sum()
|
|
|
7 |
* @summary Sum the values in a data set.
|
|
|
8 |
* @author [Allan Jardine](http://sprymedia.co.uk)
|
|
|
9 |
* @requires DataTables 1.10+
|
|
|
10 |
*
|
|
|
11 |
* @returns {Number} Summed value
|
|
|
12 |
*
|
|
|
13 |
* @example
|
|
|
14 |
* // Simply get the sum of a column
|
|
|
15 |
* var table = $('#example').DataTable();
|
|
|
16 |
* table.column( 3 ).data().sum();
|
|
|
17 |
*
|
|
|
18 |
* @example
|
|
|
19 |
* // Insert the sum of a column into the columns footer, for the visible
|
|
|
20 |
* // data on each draw
|
|
|
21 |
* $('#example').DataTable( {
|
|
|
22 |
* drawCallback: function () {
|
|
|
23 |
* var api = this.api();
|
|
|
24 |
* api.table().footer().to$().html(
|
|
|
25 |
* api.column( 4, {page:'current'} ).data().sum()
|
|
|
26 |
* );
|
|
|
27 |
* }
|
|
|
28 |
* } );
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
jQuery.fn.dataTable.Api.register( 'sum()', function () {
|
|
|
32 |
return this.flatten().reduce( function ( a, b ) {
|
|
|
33 |
return (a*1) + (b*1); // cast values in-case they are strings
|
|
|
34 |
}, 0 );
|
|
|
35 |
} );
|
|
|
36 |
|