开发者

Plugin - comma decimals

开发者 https://www.devze.com 2023-01-09 23:15 出处:网络
I have a table with prices in this format: \"1.234,56\", (the thousands se开发者_如何学编程parator is a period, and the decimal separator is a comma). This format doesn\'t work because tablesorter plu

I have a table with prices in this format: "1.234,56", (the thousands se开发者_如何学编程parator 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?


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


'1.234,56'​.replace('.', '').replace(',', '.') // '1234.56'


$.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' 
                } 
            } 
        });


});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号