gpt4 book ai didi

javascript - 排序时间hh :mm:ss with tablesorter plugin

转载 作者:行者123 更新时间:2023-11-28 00:48:55 26 4
gpt4 key购买 nike

我正在使用 tablesorter 插件,并且有一列以 hh:mm:ss 格式排序。排序器:“时间”不起作用。有没有办法对该列进行排序?谢谢

我已经尝试过这样的方法,但它没有对列进行排序

ts.addParser({
id: "customDate",
is: function(s) {
return false;
},
format: function(s) {
s = s.replace(/:/g," ");
s = s.split(" ");
return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2]).getTime()+parseInt(s[3]));
},
type: "numeric"

});

最佳答案

已经有一个用于表排序器的内置时间解析器,但它会查找“AM”或“PM”。

但看起来您需要使用计时器时间(hh:mm:ss,或任何名称)进行排序。

以下解析器代码看起来有点复杂,但它应该涵盖列仅包含秒 (ss) 或分钟和秒 (mm:ss) 的情况.

需要 maxDigits 设置,因为解析器基本上会在值上添加前导零以保持值一致,因此 10:30:40 变为 010030040(当 maxDigits 设置为 3 时)。

无论如何,here is a demo :

$(function () {

// change maxDigits to 4, if values go > 999
// or to 5 for values > 9999, etc.
var maxDigits = 3;

$.tablesorter.addParser({
id: "times",
is: function (s) {
return false;
},
format: function (s) {
// prefix contains leading zeros that are tacked
var prefix = new Array(maxDigits + 1).join('0'),
// split time into blocks
blocks = s.split(/\s*:\s*/),
len = blocks.length,
result = [];
// add values in reverse, so if there is only one block
// (e.g. "10"), then it would be the time in seconds
while (len) {
result.push((prefix + (blocks[--len] || 0)).slice(-maxDigits));
}
// reverse the results and join them
return result.length ? result.reverse().join('') : s;
},
type: "text"
});

$('table').tablesorter({
theme: 'blue',
headers: {
3: {
sorter: 'times'
}
}
});
});

我不确定这对您是否重要,但还有一个“持续时间”解析器可用,允许在时间后添加标签(例如 10y 30d 6h 10m) - 请参阅 this demo

关于javascript - 排序时间hh :mm:ss with tablesorter plugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27021789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com