Skip to content

Instantly share code, notes, and snippets.

@gvinaccia
Created August 5, 2013 10:09
Show Gist options
  • Save gvinaccia/6154845 to your computer and use it in GitHub Desktop.
Save gvinaccia/6154845 to your computer and use it in GitHub Desktop.
Javascript: datatable currency sort
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
var sValidChars = "0123456789.-,";
var Char;
/* Check the numeric part */
for ( i=1 ; i<sData.length ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
}
/* Check prefixed by currency */
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
{
return 'currency';
}
return null;
}
);
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
/* Remove the currency sign */
x = x.substring( 1 );
y = y.substring( 1 );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y;
};
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
/* Remove the currency sign */
x = x.substring( 1 );
y = y.substring( 1 );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return y - x;
};
$(document).ready(function() {
$(".dataTable").dataTable({
"sPaginationType": "full_numbers",
"aaSorting": [[1,'asc'], [0,'asc']]
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment