jQuery tablesorter - 连字符以及带小数(浮点数)的数字导致排序问题

时间:2013-09-27 12:45:50

标签: jquery tablesorter

要求
我的表可以包含这些值 -

1.00
0.00
23.10
-
35.00
-
22.00
0.00
0.00

我希望这样排序(所有连字符都应该在最后) -

1.00
22.00
23.10
35.00
0.00
0.00
0.00
-
-

或者即使这样也没问题(开头的所有连字符,至少它们与其他连字符是分开的) -

-
-
0.00
0.00
0.00
1.00
22.00
23.10
35.00

我做了什么
我正在使用这样的自定义排序器(这是我们在项目中使用的那个,它对于整数和十进制数,即浮点类型非常好) -

$.tablesorter.addParser({
        // set a unique id
        id: 'formattedNumbers',
        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, ''); //removes comma separator from formatted numbers

        },
        // set type, either numeric or text
        type: 'numeric'
    });

并且这样称呼它 -

$("#"+report_type).tablesorter({
                    indexing: true,
                    textExtraction: 'complex',
                    headers : 1: {
                                        { sorter: 'formattedNumbers' },
                                    },
                    widgets :['zebra'],
                    sortList:[[0,0]]
                });

结果我
但我得到了不可预知的结果。除了0.00和连字符( - )之外的数字在它们之间正确排序,但是0.00和连字符中的一些仍然随机分散。像这样 -

0.00
1.00
22.00
0.00
-
23.10
-
35.00
0.00
-

我的想法

我们需要更改自定义排序器中的逻辑,以便处理连字符。我尝试按jQuery tablesorter - sorting a column with mixed text and numbers中的建议寻找自然排序,但没有找到像我们的“formattedNumbers”那样有效的自定义排序器。

4 个答案:

答案 0 :(得分:1)

tablesorter的默认排序器方法将为您提供

0.00
0.00
0.00
1.00
22.00
23.10
35.00
-
-

-
-
35.00
23.10
22.00
1.00
0.00
0.00
0.00

看看这里:http://jsfiddle.net/Y6eMv/2/

我不明白有这个结果的目标:

1.00
22.00
23.10
35.00
0.00
0.00
0.00
-
-

可以提供您想要实现的更多细节吗?

答案 1 :(得分:1)

朋友,请在添加解析器代码中进行更改。

旧代码

format: function(s){
            // format your data for normalization
                return s.replace(/,/g, '').replace(/%/g, ''); //removes comma separator from formatted numbers

            }

根据莫蒂提出的建议。

更新了代码

format: function(s){
            // format your data for normalization
                s = s.replace(/,/g, '').replace(/%/g, ''); //removes comma, percentage 
                if(s && s.trim() && s.trim().length==1){
                       s = s.replace(/-/g,'');                  
                   }
                return s;
    }

希望,这将解决您的问题。 在你给出的场景中(值包含更多的零并且没有出现负数),为了使它看起来更漂亮你可以用-1替换' - '以将它与零分开。

更新版本(如果您确定没有数据< 0):

format: function(s){
            // format your data for normalization
                s = s.replace(/,/g, '').replace(/%/g, ''); //removes comma, percentage 
                if(s && s.trim() && s.trim().length==1){
                       s = s.replace(/-/g,'-1');                  
                   }
                return s;
    }

干杯。

答案 2 :(得分:1)

由于没有共享HTML,我们只能假设标记中的超标不包含额外的空格,制表符或其他不可见的字符。 tablesorter解析器设置为“numeric”但返回一个字符串,因此比较可能不正确。

正如@Xiaodoudou在jsFiddle示例中所示,我的fork of tablesorter将对正确的事情进行排序。

此外,您可以在列的底部对非数字数据(字符串)进行排序 - 请参阅this demo。不需要额外的解析器,您需要做的只是将sort-bottom类添加到列的<th> - 尝试this demo

<th class="string-bottom">Numeric</th>

答案 3 :(得分:0)

你可以做数组并使用排序方法:

tab.sort(function(a,b){ 
    if(b==0){
      return -1
    }
    else if(a==0){
      return 1
    }
    else{
      return (a-b)
    }
})
相关问题