jQuery Tablesorter 的自定义解析器
我正在使用 jQuery Tablesorter,并且对表列应用解析器的顺序有问题。 我添加了一个自定义解析器来处理 $-3.33 形式的货币。
$.tablesorter.addParser({
id: "fancyCurrency",
is: function(s) {
return /^\$[\-]?[0-9,\.]*$/.test(s);
},
format: function(s) {
s = s.replace(/[$,]/g,'');
return $.tablesorter.formatFloat( s );
},
type: "numeric"
});
问题似乎是内置货币解析器优先于我的自定义解析器。 我可以将解析器放在表排序器代码本身中(在货币解析器之前)并且它可以正常工作,但这不是很容易维护。 我无法使用类似以下内容手动指定排序器:
headers: {
3: { sorter: "fancyNumber" },
11: { sorter: "fancyCurrency" }
}
因为表列是根据用户输入动态生成的。 我想一种选择是指定排序器用作 css 类,并使用一些 JQuery 显式指定排序器,例如 这个问题建议,但如果可能的话,我更愿意坚持使用动态检测。
I'm using the jQuery Tablesorter and have an issue with the order in which parsers are applied against table columns. I'm adding a custom parser to handle currency of the form $-3.33.
$.tablesorter.addParser({
id: "fancyCurrency",
is: function(s) {
return /^\$[\-]?[0-9,\.]*$/.test(s);
},
format: function(s) {
s = s.replace(/[$,]/g,'');
return $.tablesorter.formatFloat( s );
},
type: "numeric"
});
The problem seems to be that the built-in currency parser takes precedence over my custom parser. I could put the parser in the tablesorter code itself (before the currency parser) and it works properly, but this isn't very maintainable. I can't specify the sorter manually using something like:
headers: {
3: { sorter: "fancyNumber" },
11: { sorter: "fancyCurrency" }
}
since the table columns are generated dynamically from user inputs. I guess one option would be to specify the sorter to use as a css class and use some JQuery to explicitly specify a sorter like this question suggests, but I'd prefer to stick with dynamic detection if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个选项是为具有货币值的列提供附加类“{'sorter':'currency'}”。 还要确保包含 tablesorter 支持的插件元数据。 这将引入每个元素选项并明确告诉 tablesorter 如何排序。
此外,格式化货币的函数中存在一个错误:它不处理负数。
我已经提交了一个 bug,并且正在努力发布补丁。
第二个选项是将此修复应用于您的表排序器副本。 一旦应用了修复程序,您就不需要在 th 或 td 中指定货币分类器(无论如何在 td 中指定都太过分了)。
The first option is to give the columns that have currency values the additional class "{'sorter':'currency'}". Also make sure you're including the plugin metadata, which tablesorter supports. This will pull in the per element options and explicitly tell tablesorter how to sort.
Also, there's a bug in the function to format currency: it doesn't handle negative numbers.
I've filed a bug, and am working on landing a patch.
The second option is to apply this fix to your copy of tablesorter. Once you've applied the fix, you won't need to specify the currency sorter in the th or td's(specifying in the td's is over-kill anyway).
我遇到了类似的问题,并发现了
textExtraction
当您的单元格包含标记时推荐使用的选项。 然而,它作为预格式化程序运行得非常好!I had a similar issue and discovered the
textExtraction
option which is recommended when your cells contain markup. However, it works perfectly well as a pre-format formatter!我用过类似的东西,它对我有用。
在 header < 中使用此类 th class="{'sorter':'digit'}"> 并在列 < td class="{'sorter':'digit'}">。
完成后,更改 javascript 代码以获取数字形式的所有货币。
完成,享受排序!
这是代码:
标题:
列(td):
JAVASCRIPT:
I used something like this and it worked for me.
Use this class in header < th class="{'sorter':'digit'}"> and in column < td class="{'sorter':'digit'}">.
Once its done , make changes in javascript code for getting all the currency in the form of digits.
Its done, enjoy sorting !!!
Here is the code:
HEADER :
COLUMN (td):
JAVASCRIPT :