Search code examples
jqueryparsingnumberstablesorter

jQuery TableSorter Parser for Europeans


I have a table with numbers like that: 12000.34 which sort perfectly with default options.

I am italian, so I want that number to format like this: 12.000,34 (comma for decimals, dot for thousands).

I format like that but my tables stop sorting the right way.

So I build my own custom parser:

jQuery.tablesorter.addParser({
            id: "commaDigit",
            is: function(s) {
                return /^[0-9]?[0-9,\.]*$/.test(s);
            },
            format: function(s) {
                s = s.replace("%","")
                     .replace(/€/g, '')
                     .replace(/^\s+|\s+$/g,"")
                     .replace(/,/g, "")
                return jQuery.tablesorter.formatFloat(s);
            },
            type: "numeric"
        });     

It works with numbers like 12000,34 but not with 12.000,34 What do I have to do with that dot??

In addition, I'd like to sort also "integers" like "-1,23" "0,00" "+3,68"

How can I manipulate my "s" string for achieving that?


Solution

  • jQuery.tablesorter.addParser({
                id: "commaDigit",
                is: function(s) {
                    return /^[0-9]?[0-9,\.]*$/.test(s);
                },
                format: function(s) {
                    s = s.replace("%","")
                         .replace(/€/g, '')
                         .replace(/^\s+|\s+$/g,"")
                         .replace(",", "")
                         .replace(".", "")
                    return jQuery.tablesorter.formatFloat(s);
                },
                type: "numeric"
            });     
    

    This removes the . from the column value