Updated to the numeric comma type detection - period allowed for decimal place and negative checking simplified.

Note added that the formatted numbers plug-in is preferable now.
pull/1/head
Allan Jardine 12 years ago
parent 8eb593c99f
commit 3af3ee2c1d

@ -3,6 +3,10 @@
* decimal place. This sorting plug-in shows how that can be taken account of in * decimal place. This sorting plug-in shows how that can be taken account of in
* sorting by adding the type 'numeric-comma' to DataTables. A type detection * sorting by adding the type 'numeric-comma' to DataTables. A type detection
* plug-in for this sorting method is provided below. * plug-in for this sorting method is provided below.
*
* Please note that the 'Formatted numbers' type detection and sorting plug-ins
* offer greater flexibility that this plug-in and should be used in preference
* to this method.
* @name Commas for decimal place * @name Commas for decimal place
* @anchor numeric_comma * @anchor numeric_comma
* @author <a href="http://sprymedia.co.uk">Allan Jardine</a> * @author <a href="http://sprymedia.co.uk">Allan Jardine</a>

@ -1,6 +1,10 @@
/** /**
* Automatically detect numbers which use a comma in the place of a decimal * Automatically detect numbers which use a comma in the place of a decimal
* point to allow them to be sorted numerically. * point to allow them to be sorted numerically.
*
* Please note that the 'Formatted numbers' type detection and sorting plug-ins
* offer greater flexibility that this plug-in and should be used in preference
* to this method.
* @name Commas for decimal place * @name Commas for decimal place
* @anchor numeric_comma * @anchor numeric_comma
* @author <a href="http://sprymedia.co.uk">Allan Jardine</a> * @author <a href="http://sprymedia.co.uk">Allan Jardine</a>
@ -9,28 +13,24 @@
jQuery.fn.dataTableExt.aTypes.unshift( jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData ) function ( sData )
{ {
var sValidChars = "0123456789-,"; var sValidChars = "0123456789,.";
var Char; var Char;
var bDecimal = false; var bDecimal = false;
var iStart=0;
/* Negative sign is valid - shift the number check start point */
if ( sData.charAt(0) === '-' ) {
iStart = 1;
}
/* Check the numeric part */ /* Check the numeric part */
for ( i=0 ; i<sData.length ; i++ ) for ( i=iStart ; i<sData.length ; i++ )
{ {
Char = sData.charAt(i); Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1) if (sValidChars.indexOf(Char) == -1)
{ {
return null; return null;
} }
/* Only allowed one decimal place... */
if ( Char == "," )
{
if ( bDecimal )
{
return null;
}
bDecimal = true;
}
} }
return 'numeric-comma'; return 'numeric-comma';

Loading…
Cancel
Save