Sorting Currency Issues

Sorting Currency Issues

ezos86ezos86 Posts: 22Questions: 0Answers: 0
edited January 2014 in General
http://datatables.net/forums/discussion/11168/sorting-currency/p1

I looked at this forum post and several others, but still seem to be having issues.
I first got the _fn---Regex error, then I added the function from that forum and it worked, but now I get a no replace method error on the values.

Still trying to work through this.
[code]
(function(){
function _fnEscapeRegex ( sVal )
{
var acEscape = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\', '$', '^', '-' ];
var reReplace = new RegExp( '(\\' + acEscape.join('|\\') + ')', 'g' );
return sVal.replace(reReplace, '\\$1');
}

// Change this list to the valid characters you want
var validChars = "$£€c" + "0123456789" + ".-,'";
// Init the regex just once for speed - it is "closure locked"
var str = _fnEscapeRegex( validChars ),
re = new RegExp('[^'+str+']');

jQuery.fn.dataTableExt.aTypes.unshift(
function ( data )
{
if ( typeof data !== 'string' || re.test(data) ) {
return null;
}

return 'currency';
}
);
}());

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"currency-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},

"currency-asc": function ( a, b ) {
return a - b;
},

"currency-desc": function ( a, b ) {
return b - a;
}
} );
[/code]

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Try using 1.10 from the 'nightly' on the downloads page ( http://datatables.net/download ). It has a currency type built in.

    Allan
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    No luck there buddy, I got currency-asc error.

    Property 'currency-asc' of object # is not a function
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Can you link to the page so I can fix that please?

    Allan
This discussion has been closed.