我使用tablesorter对一组值进行排序,这些值的格式从710,231到6.39 million、37.3 million、5.3 million等等都有所不同。目前,插件排序只是简单地忽略million,否则排序很好,但结果是你得到了类似于`530万,639万,3730万,710,231,
发布于 2014-10-03 11:28:47
您需要添加一个解析器来将名称替换为它们的值(demo):
// see big list here: http://www.unc.edu/~rowlett/units/large.html
var numbers = {
'zero' : 0,
'hundred' : 100,
'thousand' : 1e3,
'million' : 1e6,
'billion' : 1e9,
'trillion' : 1e12,
'quadrillion' : 1e15,
'quintillion' : 1e18,
'sextillion' : 1e21,
'septillion' : 1e24,
'octillion' : 1e27,
'nonillion' : 1e30,
'decillion' : 1e33
};
$.tablesorter.addParser({
id: "namedNumbers",
is: function () {
return false;
},
format: function (s, table) {
var v,
result = 1,
arry = (s || '').split(/[\-\s]+/),
len = arry.length;
while (len) {
v = $.tablesorter.formatFloat( (arry[--len] || '').toLowerCase(), table );
if ( numbers.hasOwnProperty(v) ) {
result *= numbers[v];
} else {
result *= parseFloat(v);
}
}
return result !== 1 ? result : s;
},
type: "numeric"
});
$(function () {
$('table').tablesorter({
theme: 'blue',
headers : {
1 : { sorter : 'namedNumbers' }
}
});
});https://stackoverflow.com/questions/26172283
复制相似问题