JQuery TableSorter 逗号数字解析器不工作
这是我的问题,
我目前正在使用 JQuery Table Sorter,并且我在网络上找到了一个逗号数字解析器。 我遇到的问题是它似乎不起作用。
因此,该列的排序如下:
- 4,666
- 141,666
- 293
- 341,666
- 346
- 461,676
这应该排序为
- 293
- 346
- 4,666
- 141,666
- 341,666
- 461,676
我正在使用的解析器是这样的:
$( 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"
});
});
我只是不知道我做错了。 难道是我加载错了? 解析器有错吗? 我在这里需要真正的帮助,并且已经为这个问题苦苦挣扎了一段时间。
编辑:由于我生成列的方式以及允许用户选择的列,我永远不知道哪个标题在其中。 我尝试使用 class="{sorter: 'fancyNumber'}" 命令,如下所述: http://tablesorter.com/docs/example-meta-parsers.html
**编辑:**看起来其中一列工作正常,但该列仍然有问题。 也许是因为它有数字和逗号分隔的数字?
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.
So here is what the column is sorted as:
- 4,666
- 141,666
- 293
- 341,666
- 346
- 461,676
This should be sorted as
- 293
- 346
- 4,666
- 141,666
- 341,666
- 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
**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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于任何遇到这个问题的人。 我必须将类添加到我的标题行。 因此,对于我想要进行排序的任何标头,我添加了此类:
默认情况下,这会打开排序器,这使其工作得很好。
让我意识到我的错误的是像这样打开调试器。
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:
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.
如果您忘记包含元数据插件,也可能会发生这种情况
** 在此发布,因为这是 Google 上的第一个搜索结果。
This can also happen if you forget to include the metadata plugin
** Posted here since this was the first search result on Google.
这是我所做的:
使用逗号和点分隔符。
测试一下http://jsbin.com/equci5
here what I've done:
worked with comma and dot separator.
test it http://jsbin.com/equci5
我找到了一个适合我的解决方案。
在tablesorter.js中,修改formatFloat()函数如下:
这将替换干扰排序的逗号。
在此处找到了答案。
希望这可以帮助...!
I found a solution that worked for me.
In the tablesorter.js, modify the formatFloat() function as follows:
This will replace the commas which were interfering with the sorting.
Found the answer here.
Hope this helps...!
尝试在 .tablesorter() 声明中显式分配解析器。
查看源代码
Try explicitly assigning the parser in the .tablesorter() declaration.
See the source
正如 Jared 所提到的,您需要指定哪个列使用哪个解析器,如果您不知道该列的索引,那么您可以使用以下命令找到它:
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:
我,尝试这个正则表达式:/(\d{1,3})?(\,\d{3})*/
I, try this regular expression: /(\d{1,3})?(\,\d{3})*/
解析器仅查看第一个 tbody 行来检测要使用哪个解析器。 我猜你的第一行没有任何逗号。 我遇到了同样的问题,最后只是强制使用我想要的解析器,使用 class="{sorter: 'fancyNumber'}"
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'}"
您认为解析器应该可以工作是完全正确的,它不工作的原因是插件中的错误。 简而言之,该插件认为它可以正确对包含逗号的数字进行排序,因此使用其内置排序器,但随后无法正确对它们进行排序。
有几种方法可以修复它。
首先,您可以(如上面所建议的)强制使用排序功能(在插件初始化时或在 javascript 中)。
其次,您可以编辑插件以优先使用您自己的函数,这可以通过反转插件第 220 行 for 循环的方向来实现。
第三,您可以通过修改数字检测函数以不接受逗号(插件的第 861 行)或修改默认的数字排序器以处理逗号(插件的第 852 行)来修复损坏的默认排序行为。
我在tablesorter google代码页上提出了这个问题:http://code .google.com/p/tablesorter/issues/detail?id=6
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).
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.
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).
I have raised this issue on the tablesorter google code page: http://code.google.com/p/tablesorter/issues/detail?id=6
我找到了一个适合我的解决方案。 在tablesorter.js中,修改formatFloat()函数如下:
这将替换干扰排序的逗号。 在这里找到了答案。 希望这可以帮助...!
I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:
This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!