jQuery tablesorter-用空格排序数字 [英] jquery tablesorter - sorting number with space

查看:85
本文介绍了jQuery tablesorter-用空格排序数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jQuery tablesorter和12 345 678,91之类的数字时遇到问题

i have problem with jQuery tablesorter and numbers like 12 345 678,91

$(document).ready(function() {
    $.tablesorter.addParser({ 
        id: 'thousands',
        is: function(s) { 
            return false; 
        }, 
        format: function(s) {
            return s.replace(' ','').replace(/,/g,'');
        }, 
        type: 'numeric' 
    }); 

    $("#tablesorter").tablesorter({
    headers: { 
                3: { sorter:'thousands' }, 
                4: { sorter:'thousands' }, 
                5: { sorter:'thousands' } 
            }
    });
});

输出过滤器:

-1 295,76
-331,2
-330,01
-290
0
3 986 495,06
1 942 503,09
0
0

当我替换它时:s.replace('','').replace(/,/g,''); 通过这样:s.replace(new RegExp(/[^ 0-9/A-Za-z.]/g),"); ...甚至比以前更糟. 有什么想法吗?

When i replace this: s.replace(' ','').replace(/,/g,''); by this : s.replace(new RegExp(/[^0-9/A-Za-z. ]/g),""); ...even worse than it was. any ideas?

推荐答案

解析器不会替换所有空格;使用replace(' ','')只会替换第一个空格.另外,逗号应替换为小数点,因为它表示分数.因此,请尝试以下操作(演示):

The parser isn't replacing all of the spaces; using replace(' ','') will only replace the first space. Also, the comma should be replaced with a decimal point since it indicates a fraction. So, try this (demo):

$.tablesorter.addParser({
    id: 'thousands',
    is: function (s) {
        return false;
    },
    format: function (s) {
        return s.replace(/\s+/g, '').replace(/,/g, '.');
    },
    type: 'numeric'
});

这篇关于jQuery tablesorter-用空格排序数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆