views:

91

answers:

2

Hello,

I've been searching the web for an answer but there simply isn't a solution that works. I have a table with prices in this format: "1.234,56", so the decimals are after the comma and the dot is for separating thousands. This format doesn't work, tablesorter plugin sees it as strings rather than a number whenever there's a different character inside (only numbers, +/- and "." for decimals are allowed).

I've tried a few parsers that were supposed to fix this, tried writing my own but no luck.

Has anyone encountered and solved this before? I'd be very grateful for any response.

Thanks!

A: 
'1.234,56'​.replace('.', '').replace(',', '.') // '1234.56'
Dan Stocker
Thanks for your reply Dan. Yes, I've tried doing something like that with a custom parser but it simply doesn't work.
Stefan T
Don't put the parsing directly in the tablesorter, then. Pre-process the JSON and initialize the tablesorter with the already modified data.
Dan Stocker
A: 

Ok, I think I solved it. My table has currency so I edited the 'currency' parser but you could basically do it with any other. Currency parser looks like this in the end:

ts.addParser({
        id: "currency",
        is: function(s) {
            return /^[£$€?.]/.test(s);
        },
        format: function(s) {
            s=s.replace('.', '');
            return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9.]/g),""));
        },
        type: "numeric"
    });

(by the way, how do you turn on synthax highlight here on stackoverflow?)

Stefan T