From 74d240ca8280d591f1669bb7ba1de6795cd09a69 Mon Sep 17 00:00:00 2001 From: thomasbuckle-uk Date: Mon, 1 Aug 2016 11:03:15 +0100 Subject: [PATCH] Added new plugin for automatic detection of negative currency Name: brackets-negative Summary: Detect data of currency type with a leading currency symbol as well at detect two types of negative number formatting --- sorting/brackets-negative.js | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sorting/brackets-negative.js diff --git a/sorting/brackets-negative.js b/sorting/brackets-negative.js new file mode 100644 index 0000000..f675237 --- /dev/null +++ b/sorting/brackets-negative.js @@ -0,0 +1,42 @@ +/** + * This plug-in will add automatic negative currency detection for currency columns to + * DataTables. Note that only $, c, £ and € symbols are detected with this code, + * This plugin has also been updated to automatically detect negative values either those + * using '-' as well as numbers using '()' to specify negatives. + * This plugin also includes automatic type detection + * + * @name brackets-negative + * @summary Detect data of currency type with a leading currency symbol as well at detect two types of negative number formatting + * @author [Tom Buckle](http://sprymedia.co.uk) + */ +(function(){ + // Change this list to the valid characters you want to be detected + var validChars = "$£€c" + "0123456789" + ".-,()'"; + // Init the regex just once for speed - it is "closure locked" + var + str = jQuery.fn.dataTableExt.oApi._fnEscapeRegex( validChars ),re = new RegExp('[^'+str+']'); + $.fn.dataTableExt.aTypes.unshift( + function ( data ) + { + if ( typeof data !== 'string' || re.test(data) ) { + return null; + } + return 'currency'; + } + ); + $.fn.dataTable.ext.type.order['currency-pre'] = function ( data ) { + //Check if its in the proper format + if(data.match(/[\()]/g)){ + if( data.match(/[\-]/g) !== true){ + //It matched - strip out parentheses & any characters we dont want and append - at front + data = '-' + data.replace(/[\$£€c\(\),]/g,''); + }else{ + //Already has a '-' so just strip out non-numeric charactors exluding '-' + data = data.replace(/[^\d\-\.]/g,''); + } + }else{ + data = data.replace(/[\$£€\,]/g,''); + } + return parseInt( data, 10 ); + }; +}());