Stefan T Stefan T - 7 months ago 25
Javascript Question

Plugin - comma decimals

I have a table with prices in this format: "1.234,56", (the thousands separator is a period, and the decimal separator is a comma). This format doesn't work because tablesorter plugin sees it as strings rather than as a number whenever there's a different character inside (only numbers, +/- and "." for decimals are allowed).

How can I remove the periods and replace the commas with periods before sorting?

Answer

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?)