API: average() and sum() methods for DT1.10

pull/40/head
Allan Jardine 11 years ago
parent 1f9595d4a9
commit 02d801764c

@ -0,0 +1,20 @@
/**
* Average the values in a data set.
* @name average()
* @author [Allan Jardine](http://sprymedia.co.uk)
* @requires DataTables 1.10+
*
* @example
* table.column( 3 ).data().average();
*/
jQuery.fn.dataTable.Api.register( 'average()', function () {
var i=0;
var sum = this.flatten().reduce( function ( a, b ) {
i++;
return (a*1) + (b*1); // cast values in-case they are strings
} );
return sum / i;
} );

@ -0,0 +1,17 @@
/**
* Sum the values in a data set.
* @name sum()
* @anchor sum()
* @author <a href="http://sprymedia.co.uk">Allan Jardine</a>
* @requires DataTables 1.10+
*
* @example
* table.column( 3 ).data().sum();
*/
jQuery.fn.dataTable.Api.register( 'sum()', function () {
return this.flatten().reduce( function ( a, b ) {
return (a*1) + (b*1); // cast values in-case they are strings
} );
} );
Loading…
Cancel
Save