Anyway to render the currency in the format I need with correct ordering?
Anyway to render the currency in the format I need with correct ordering?
I'm using the column render option to render money in this style (this is a business requirement which I have no control over):
123456.78 = $123,457
-123456.78 = ($123,457)
This is for both the table on the form, as well as the exporting and printing buttons.
When I do the render, the column acts like it has string values when ordering. So, I tried specifying the "num-fmt" column type, but the parentheses are ignored - meaning a $0 and ($32) are ordered as though they were the same (so not in the correct order).
I also tried turning off the render and redrawing the columns on the fly in a "drawCallback" function, which fixes the problem on the table, but, of course, the export still appears as the original data.
What I need is some way for "num-fmt" to recognize that currency enclosed by parentheses is negative. Is this possible? Perhaps someone could indicate where I might start looking in the code?
Otherwise, how about some way to render/redraw the columns on the fly that works with export and orders correctly?
Answers
Okay - this worked for me. I searched through and found this:
"num-fmt": function ( d ) {
return __numericReplace( d, decimalPlace, _re_formatted_numeric );
},
I replaced it with this:
"num-fmt": function ( d ) {
if ((d.indexOf("(") === 0) && (d.indexOf(")") === (d.length - 1))) {
d = d.replace("(","-").replace(")","");
}
return __numericReplace( d, decimalPlace, _re_formatted_numeric );
},
I realize this is NOT optimal. But until num-fmt can officially recognize ($32) as a negative number, I am forced to use this workaround.
Hi Jeffrey,
Thanks for your post. I've wondered about having
()
enclosed numbers automatically detected and treated as negatives before, and it is something I might yet include in DataTables core.Until then, a sorting plug-in is the best way to do this. I had thought that there was one already available for such numbers, but it doesn't look like there is.
Allan
You're very welcome. Not the greatest solution, but works for now...
Data Tables is a fantastic library - so glad I found it! Just the ability to get into the code and create a workaround like that puts it way above many paid solutions (IMHO).
Thanks to you and anyone else involved!!