Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sum().js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * Note that it will attempt to 'deformat' any string based data that is passed
  7. * into it - i.e. it will strip any non-numeric characters in order to make a
  8. * best effort attempt to sum all data types. This can be useful when working
  9. * with formatting numbers such as currency. However the trade-off is that no
  10. * error is thrown if non-numeric data is passed in. You should be aware of this
  11. * in case unexpected values are returned - likely the input data is not what is
  12. * expected.
  13. *
  14. * @name sum()
  15. * @summary Sum the values in a data set.
  16. * @author [Allan Jardine](http://sprymedia.co.uk)
  17. * @requires DataTables 1.10+
  18. *
  19. * @returns {Number} Summed value
  20. *
  21. * @example
  22. * // Simply get the sum of a column
  23. * var table = $('#example').DataTable();
  24. * table.column( 3 ).data().sum();
  25. *
  26. * @example
  27. * // Insert the sum of a column into the columns footer, for the visible
  28. * // data on each draw
  29. * $('#example').DataTable( {
  30. * drawCallback: function () {
  31. * var api = this.api();
  32. * api.table().footer().to$().html(
  33. * api.column( 4, {page:'current'} ).data().sum()
  34. * );
  35. * }
  36. * } );
  37. */
  38. jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
  39. return this.flatten().reduce( function ( a, b ) {
  40. if ( typeof a === 'string' ) {
  41. a = a.replace(/[^\d.-]/g, '') * 1;
  42. }
  43. if ( typeof b === 'string' ) {
  44. b = b.replace(/[^\d.-]/g, '') * 1;
  45. }
  46. return a + b;
  47. }, 0 );
  48. } );