1
0
mirror of https://github.com/DataTables/DataTables.git synced 2024-12-01 13:24:10 +01:00

New: Number formating helper

- I've come accross a lot of cases recently where numbers need to be
  formatted for display, so I wanted to introduce rendering helpers for
  DataTables to make this easier. Only one helper introduced here -
  `$.fn.dataTable.render.number()` which will return an object that can
  be used by `columns.render` to format "raw" numbers.
This commit is contained in:
Allan Jardine 2014-03-07 11:45:53 +00:00
parent 92e380c778
commit e45f19eb60
2 changed files with 52 additions and 1 deletions

View File

@ -1 +1 @@
a0fac0689db72b158ac0a905a32bf9724f630c5e
dfdf6371182e92cc53cdac96a01ae9316c30702b

View File

@ -13924,6 +13924,57 @@
} );
/*
* Public helper functions. These aren't used internally by DataTables, or
* called by any of the options passed into DataTables, but they can be used
* externally by developers working with DataTables. They are helper functions
* to make working with DataTables a little bit easier.
*/
/**
* Helpers for `columns.render`.
*
* The options defined here can be used with the `columns.render` initialisation
* option to provide a display renderer. The following functions are defined:
*
* * `number` - Will format numeric data (defined by `columns.data`) for
* display, retaining the original unformatted data for sorting and filtering.
* It takes 4 parameters:
* * `string` - Thousands grouping separator
* * `string` - Decimal point indicator
* * `integer` - Number of decimal points to show
* * `string` (optional) - Prefix.
*
* @example
* // Column definition using the number renderer
* {
* data: "salary",
* render: $.fn.dataTable.render.number( '\'', '.', 0, '$' )
* }
*
* @namespace
*/
DataTable.render = {
number: function ( thousands, decimal, precision, prefix ) {
return {
display: function ( d ) {
d = parseFloat( d );
var intPart = parseInt( d, 10 );
var floatPart = precision ?
(decimal+(d - intPart).toFixed( precision )).substring( 2 ):
'';
return (prefix||'') +
intPart.toString().replace(
/\B(?=(\d{3})+(?!\d))/g, thousands
) +
floatPart;
}
};
}
};
/*
* This is really a good bit rubbish this method of exposing the internal methods
* publicly... - To be fixed in 2.0 using methods on the prototype