Datatable – Currency / Amount Sorting With Extend Plugin

Datatable plugin natively doesn’t sort the data like
1. Currency with commas e.g. 5,565.00
2. Currency symbols and names etc. $65,2265.00 USD

the below extension of the plugin will solve the issue. 🙂


	jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "numeric-comma-pre": function ( a ) {
      var x = (a == "-") ? 0 : a.replace(/[^0-9]/g, ""); //remove everything other than numbers
        return parseFloat( x );
    },
 
    "numeric-comma-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
 
    "numeric-comma-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );


And usage is

 columnDefs: [
       { type: 'numeric-comma', targets: 0 }
     ]

 

Download Complete Example

Leave a Reply

Your email address will not be published. Required fields are marked *