Sorting Currency Issues
Sorting Currency Issues
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]
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]
This discussion has been closed.
Replies
Allan
Property 'currency-asc' of object # is not a function
Allan