jQuery Tablesorter - 自定义解析器不工作
我正在尝试为 jQuery 插件 Tablesorter 编写一个自定义解析器。这个想法是让它在第一次点击时按降序对数字进行排序。
但是,当我对表格进行排序时,顺序不会改变。有时,几行会移动,但大多数行保持不变。这是代码:
$.tablesorter.addParser({
id: 'desc',
is: function(s) { return false },
format: function(s) {
return 1.0 / parseFloat( s.replace(/,/g,'') );
},
type: 'numeric'
});
我编写的其他解析器工作正常。我尝试用 9999 减去数字,而不是 1.0 除以它,以防浮点数出现问题(不走运)。
I'm trying to write a custom parser for the jQuery plugin, Tablesorter. The idea is for it to sort the numbers in descending order on the first click.
However, when I sort the table, the order doesn't change. Sometimes a few rows move, but most of them stay the same. Here is the code:
$.tablesorter.addParser({
id: 'desc',
is: function(s) { return false },
format: function(s) {
return 1.0 / parseFloat( s.replace(/,/g,'') );
},
type: 'numeric'
});
The other parsers I've written are working fine. I tried 9999 minus the number instead of 1.0 divided by it, in case it was a problem with floats (no luck).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案。我在每列中有一些空单元格,它们被解析为“NaN”。我不知道为什么这会搞砸排序(空白单元格间歇性地与常规数字间隔开,任何东西都没有顺序)。
简而言之,这段代码适用于格式函数:
I found a solution. I had some empty cells in each column, which were being parsed as "NaN". Why this screwed up the ordering, I don't know (blank cells were intermittently spaced with regular numbers, there was no order to anything).
In short, this code works for the format function:
要以相反的数字顺序对某些内容进行排序,对我来说,自然的方法是将其乘以 -1,而不是您尝试过的方法。
至于解析器本身,我注意到的唯一区别是您返回的是实际数字,而 Tablesorter 站点上的解析器示例 返回一个字符串。也许在返回之前将值转换回字符串会起作用吗?
To sort something in reverse numeric order, to me the natural way to go is to multiply it by -1, rather than the methods you've tried.
As to the parser itself, the only difference I notice is that you are returning an actual number, whereas the parser example at the Tablesorter site returns a string. Perhaps converting the value back to string before returning it would work?