我尝试对简单的数字进行排序,例如:
11 256 232
256 236
23056
11536
1 023 585
带有表格排序插件。
但是我的测试没有一个是好的。
我尝试:
$.tablesorter.addParser({
id: 'colpap',
is: function (s) {
return false;
},
format: function (s) {
return s.replace(/\s+/g, '');
},
type: 'numeric'
});
你有什么想法吗?
最佳答案
当您为解析器设置“数字”类型时,排序器将设置为评估数字值,而不是字符串。
所以你需要做的是解析数字并返回该值
$.tablesorter.addParser({
id: 'colpap',
is: function (s) {
return false;
},
format: function (s) {
var number = parseFloat(s.replace(/\s+/g, ''));
return isNaN(number) ? s : number;
},
type: 'numeric'
});
* 注意:如果您的数字采用欧洲格式(其中使用逗号代替小数),则上述解析器将不起作用,例如1 234 545,34
。
我不知道您使用的是哪个版本的 tablesorter,所以我假设它是原始版本 - here is a demo .
关于javascript - Tablesorter,对简单数字进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38331019/