From e45f19eb607d55a6a58cd6322cb6fd7a0b7dca66 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 7 Mar 2014 11:45:53 +0000 Subject: [PATCH] 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. --- .datatables-commit-sync | 2 +- media/js/jquery.dataTables.js | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/.datatables-commit-sync b/.datatables-commit-sync index 2ddff374..c88a4cf1 100644 --- a/.datatables-commit-sync +++ b/.datatables-commit-sync @@ -1 +1 @@ -a0fac0689db72b158ac0a905a32bf9724f630c5e +dfdf6371182e92cc53cdac96a01ae9316c30702b diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index cc6c9afd..47ae52f6 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -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