Tablesorter,对简单数字进行排序
Tablesorter, sort simple number
我尝试对简单的数字进行排序,例如:
11 256 232
256 236
23 056
11 536
1 023 585
使用 tablesorter 插件。
但是我的测试没有一个是可以的
我试试:
$.tablesorter.addParser({
id: 'colpap',
is: function (s) {
return false;
},
format: function (s) {
return s.replace(/\s+/g, '');
},
type: 'numeric'
});
你有想法吗?
当您为解析器设置 "numeric" 类型时,排序器将设置为评估数字值,而不是字符串。
所以你需要做的是解析数字和return那个值
$.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。
我尝试对简单的数字进行排序,例如:
11 256 232
256 236
23 056
11 536
1 023 585
使用 tablesorter 插件。
但是我的测试没有一个是可以的
我试试:
$.tablesorter.addParser({
id: 'colpap',
is: function (s) {
return false;
},
format: function (s) {
return s.replace(/\s+/g, '');
},
type: 'numeric'
});
你有想法吗?
当您为解析器设置 "numeric" 类型时,排序器将设置为评估数字值,而不是字符串。
所以你需要做的是解析数字和return那个值
$.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。