Plugin - comma decimals
Asked Answered
L

3

2

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?

Libby answered 4/8, 2010 at 8:15 Comment(4)
What exactly is the problem, do you want to remvoe the comma or what?Quantify
No, I want to make the commma a decimal point, and remove the dot before sorting so it sees my "12.400,50" number as: "12400,50" and then the sorting would (theoretically, at least) work. I have a database with price numbers in this format so I can't edit that out.Libby
Does the tables-sorter allow you to define a custom sorting function?Catenoid
Yes, this way - tablesorter.com/docs/example-parsers.htmlLibby
L
3

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

Libby answered 4/8, 2010 at 9:9 Comment(1)
Syntax highlighting happens automatically if you indent your code by four spaces (though if you mix html with JS it might get confused unless you explicitly include <script> tags around the JS). Don't use <pre> tags like you originally did. You can also enter your code without indenting while you are typing it and then select it and click the {} button to format it as code.Paralyze
S
0
'1.234,56'​.replace('.', '').replace(',', '.') // '1234.56'
Superego answered 4/8, 2010 at 8:36 Comment(2)
Thanks for your reply Dan. Yes, I've tried doing something like that with a custom parser but it simply doesn't work.Libby
Don't put the parsing directly in the tablesorter, then. Pre-process the JSON and initialize the tablesorter with the already modified data.Superego
N
0
$.tablesorter.addParser({ 
        // set a unique id 
        id: 'pesos', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization
            return s.replace(/' '/g,'').replace(/\./g, ''); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
    $("#menuh").sticky({topSpacing:0});
    $("#myTable").tablesorter();
    $("#myTableBienes").tablesorter({ 
            headers: { 
                5: { 
                    sorter:'pesos' 
                } 
            } 
        });


});
Neuman answered 24/7, 2015 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.