diff --git a/api/average().js b/api/average().js new file mode 100644 index 0000000..b655486 --- /dev/null +++ b/api/average().js @@ -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; +} ); + diff --git a/api/sum().js b/api/sum().js new file mode 100644 index 0000000..74c90f1 --- /dev/null +++ b/api/sum().js @@ -0,0 +1,17 @@ +/** + * Sum the values in a data set. + * @name sum() + * @anchor sum() + * @author Allan Jardine + * @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 + } ); +} ); +