JQuery TableSorter Comma-Digit Parser无法正常工作

时间:2023-01-19 19:24:37

Here's my problem,

这是我的问题,

I am currently using the JQuery Table Sorter and I found a Comma-Digit parser on the web. The problem I am having is it doesn't seem to be working.

我目前正在使用JQuery Table Sorter,我在网上找到了一个Comma-Digit解析器。我遇到的问题是它似乎没有起作用。

So here is what the column is sorted as:

所以这是列的排序方式:

  1. 4,666
  2. 141,666
  3. 293
  4. 341,666
  5. 346
  6. 461,676

This should be sorted as

这应该按照排序

  1. 293
  2. 346
  3. 4,666
  4. 141,666
  5. 341,666
  6. 461,676

The parser I am using is this:

我使用的解析器是这样的:

$( function() { 

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

I just don't know I am doing wrong. Am I loading it wrong? Is the parser wrong? I need real help here and have been struggling with this problem for a while now.

我只是不知道我做错了。我加错了吗?解析器错了吗?我在这里需要真正的帮助,并且一直在努力解决这个问题。

Edit: Because of how I generate my columns and the columns allowed to be chosen by the user, I would never know which header is in and not. I have tried using the class="{sorter: 'fancyNumber'}" command as stated here: http://tablesorter.com/docs/example-meta-parsers.html

编辑:由于我如何生成我的列以及允许用户选择的列,我永远不会知道哪个标题是否存在。我已尝试使用class =“{sorter:'fancyNumber'}”命令,如下所述:http://tablesorter.com/docs/example-meta-parsers.html

**Edit:**It looks like one of the columns is working correctly, but this column is still having problems. maybe because it has digits and comma seperated digits?

**编辑:**看起来其中一列正常工作,但此列仍有问题。也许是因为它有数字和逗号分隔的数字?

10 个解决方案

#1


For anyone that comes across this question. I had to add the class to my header row. So for any header that I wanted to fancy sort, I added this class:

对于遇到这个问题的任何人。我不得不把这个类添加到我的标题行。所以对于我想要排序的任何标题,我添加了这个类:

<th class=\"{sorter: 'fancyNumber'}\">

This turned on the sorter by default which made it work nice.

这默认打开了分拣机,使其工作得很好。

What made me realize my error in my ways was turning on the debugger like so.

是什么让我意识到我的错误就是这样打开调试器。

$("#tblInfo").tablesorter({debug:true, widgets: ['zebra'], widgetZebra: { css: ['d0', 'd1']} });

#2


This can also happen if you forget to include the metadata plugin

如果您忘记包含元数据插件,也会发生这种情况

** Posted here since this was the first search result on Google.

**发布在此处,因为这是Google上的第一个搜索结果。

#3


here what I've done:

在这里我做了什么:

$(document).ready(function() {
      $.tablesorter.addParser({
        id: 'fancyNumber',
        is:function(s){return false;},
        format: function(s) {return s.replace(/[\,\.]/g,'');},
        type: 'numeric'
    });
    $("table").tablesorter({headers: {0: {sorter: 'fancyNumber'}}});
});

worked with comma and dot separator.

使用逗号和点分隔符。

test it http://jsbin.com/equci5

测试它http://jsbin.com/equci5

#4


I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

我找到了一个适合我的解决方案。在tablesorter.js中,修改formatFloat()函数,如下所示:

this.formatFloat = function (s) {
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

这将取代干扰排序的逗号。在这里找到答案。希望这可以帮助...!

#5


Try explicitly assigning the parser in the .tablesorter() declaration.

尝试在.tablesorter()声明中明确指定解析器。

.tablesorter( { headers: { 0: { sorter:'fancyNumber' } }); 

See the source

查看来源

#6


As Jared has mentioned, you need to specify which column uses which Parser, if you don't know the index of the column then you can find it our using this:

正如Jared所提到的,你需要指定哪个列使用哪个Parser,如果你不知道列的索引,那么你可以使用它来找到它:

var fancyIndex = $('th.fancyColumn').prevAll().length
var headers = {};
headers[fancyIndex] = {sorter:'fancyNumber'}

$("table").tablesorter({headers:headers})

#7


I, try this regular expression: /(\d{1,3})?(\,\d{3})*/

我试试这个正则表达式:/(\ d {1,3})?(\,\ d {3})* /

#8


The parsers only look at the first tbody row to detect which parser to use. I'm guessing your first row doesn't have any commas in it. I ran into the same problem, and finally just forced the parser I wanted, using class="{sorter: 'fancyNumber'}"

解析器只查看第一个tbody行以检测要使用的解析器。我猜你的第一行没有任何逗号。我遇到了同样的问题,最后只是强迫我想要的解析器,使用class =“{sorter:'fancyNumber'}”

#9


You are completely correct that the parser should be working, the reason it is not is due to a bug in the plugin. In short the plugin thinks it can sort numbers with commas in them correctly, and so uses its built in sorter, but then fails to sort them correctly.

解析器应该正常工作是完全正确的,原因不是插件中的错误。简而言之,插件认为它可以正确地用逗号分类数字,因此使用其内置的分类器,但是无法正确排序它们。

There are a few ways to fix it.

有几种方法可以解决它。

Firstly you can (as you suggested above) force the use of your sorting function (either in the or in the javascript on initialisation of the plugin).

首先,您可以(如上所述)强制使用您的排序功能(在插件初始化时在javascript中或在javascript中)。

Secondly you can edit the plugin to use your functions in preference to your own, this can be achieved by reversing the direction of the for loop on line 220 of the plugin.

其次,您可以编辑插件以优先使用您自己的功能,这可以通过在插件的第220行上反转for循环的方向来实现。

Thirdly you can fix the broken default sorting behaviour by modifying either the digit detection function to not accept commas (line 861 of the plugin) or by modifying the default number sorter to handle commas (line 852 of the plugin).

第三,您可以通过修改数字检测功能以不接受逗号(插件的第861行)或修改默认数字排序器来处理逗号(插件的第852行)来修复损坏的默认排序行为。

I have raised this issue on the tablesorter google code page: http://code.google.com/p/tablesorter/issues/detail?id=6

我在tablesorter谷歌代码页上提出了这个问题:http://code.google.com/p/tablesorter/issues/detail?id = 6

#10


I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

我找到了一个适合我的解决方案。在tablesorter.js中,修改formatFloat()函数,如下所示:

this.formatFloat = function (s) {
            s = s.toString();
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

这将取代干扰排序的逗号。在这里找到答案。希望这可以帮助...!

#1


For anyone that comes across this question. I had to add the class to my header row. So for any header that I wanted to fancy sort, I added this class:

对于遇到这个问题的任何人。我不得不把这个类添加到我的标题行。所以对于我想要排序的任何标题,我添加了这个类:

<th class=\"{sorter: 'fancyNumber'}\">

This turned on the sorter by default which made it work nice.

这默认打开了分拣机,使其工作得很好。

What made me realize my error in my ways was turning on the debugger like so.

是什么让我意识到我的错误就是这样打开调试器。

$("#tblInfo").tablesorter({debug:true, widgets: ['zebra'], widgetZebra: { css: ['d0', 'd1']} });

#2


This can also happen if you forget to include the metadata plugin

如果您忘记包含元数据插件,也会发生这种情况

** Posted here since this was the first search result on Google.

**发布在此处,因为这是Google上的第一个搜索结果。

#3


here what I've done:

在这里我做了什么:

$(document).ready(function() {
      $.tablesorter.addParser({
        id: 'fancyNumber',
        is:function(s){return false;},
        format: function(s) {return s.replace(/[\,\.]/g,'');},
        type: 'numeric'
    });
    $("table").tablesorter({headers: {0: {sorter: 'fancyNumber'}}});
});

worked with comma and dot separator.

使用逗号和点分隔符。

test it http://jsbin.com/equci5

测试它http://jsbin.com/equci5

#4


I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

我找到了一个适合我的解决方案。在tablesorter.js中,修改formatFloat()函数,如下所示:

this.formatFloat = function (s) {
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

这将取代干扰排序的逗号。在这里找到答案。希望这可以帮助...!

#5


Try explicitly assigning the parser in the .tablesorter() declaration.

尝试在.tablesorter()声明中明确指定解析器。

.tablesorter( { headers: { 0: { sorter:'fancyNumber' } }); 

See the source

查看来源

#6


As Jared has mentioned, you need to specify which column uses which Parser, if you don't know the index of the column then you can find it our using this:

正如Jared所提到的,你需要指定哪个列使用哪个Parser,如果你不知道列的索引,那么你可以使用它来找到它:

var fancyIndex = $('th.fancyColumn').prevAll().length
var headers = {};
headers[fancyIndex] = {sorter:'fancyNumber'}

$("table").tablesorter({headers:headers})

#7


I, try this regular expression: /(\d{1,3})?(\,\d{3})*/

我试试这个正则表达式:/(\ d {1,3})?(\,\ d {3})* /

#8


The parsers only look at the first tbody row to detect which parser to use. I'm guessing your first row doesn't have any commas in it. I ran into the same problem, and finally just forced the parser I wanted, using class="{sorter: 'fancyNumber'}"

解析器只查看第一个tbody行以检测要使用的解析器。我猜你的第一行没有任何逗号。我遇到了同样的问题,最后只是强迫我想要的解析器,使用class =“{sorter:'fancyNumber'}”

#9


You are completely correct that the parser should be working, the reason it is not is due to a bug in the plugin. In short the plugin thinks it can sort numbers with commas in them correctly, and so uses its built in sorter, but then fails to sort them correctly.

解析器应该正常工作是完全正确的,原因不是插件中的错误。简而言之,插件认为它可以正确地用逗号分类数字,因此使用其内置的分类器,但是无法正确排序它们。

There are a few ways to fix it.

有几种方法可以解决它。

Firstly you can (as you suggested above) force the use of your sorting function (either in the or in the javascript on initialisation of the plugin).

首先,您可以(如上所述)强制使用您的排序功能(在插件初始化时在javascript中或在javascript中)。

Secondly you can edit the plugin to use your functions in preference to your own, this can be achieved by reversing the direction of the for loop on line 220 of the plugin.

其次,您可以编辑插件以优先使用您自己的功能,这可以通过在插件的第220行上反转for循环的方向来实现。

Thirdly you can fix the broken default sorting behaviour by modifying either the digit detection function to not accept commas (line 861 of the plugin) or by modifying the default number sorter to handle commas (line 852 of the plugin).

第三,您可以通过修改数字检测功能以不接受逗号(插件的第861行)或修改默认数字排序器来处理逗号(插件的第852行)来修复损坏的默认排序行为。

I have raised this issue on the tablesorter google code page: http://code.google.com/p/tablesorter/issues/detail?id=6

我在tablesorter谷歌代码页上提出了这个问题:http://code.google.com/p/tablesorter/issues/detail?id = 6

#10


I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

我找到了一个适合我的解决方案。在tablesorter.js中,修改formatFloat()函数,如下所示:

this.formatFloat = function (s) {
            s = s.toString();
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

这将取代干扰排序的逗号。在这里找到答案。希望这可以帮助...!