排序时间hh:mm:带有tablesorter插件的ss [英] Sorting time hh:mm:ss with tablesorter plugin

查看:61
本文介绍了排序时间hh:mm:带有tablesorter插件的ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tablesorter插件,我有一个列以hh:mm:ss格式排序。
分拣机:'时间'不起作用。有没有办法对此列进行排序?
谢谢

I'm using the tablesorter plugin and I have a column to sort in the format hh:mm:ss. sorter: 'time' does not work. Is there a way to sort this column? Thanks

我试过这样但是它没有对colum进行排序

I have tried it like this but it does not sort the colum

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"

});

推荐答案

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

There is already a built-in time parser for tablesorter, but it looks for an "AM" or "PM".

但它看起来像你需要使用计时器时间排序( hh:mm:ss ,或其他任何名称)。

But it looks like you need to sort using timer times (hh:mm:ss, or whatever it's called).

以下解析器代码看起来有点复杂,但它应该涵盖列只包含秒( ss )或分钟&秒( mm:ss )。

The following parser code looks a bit convoluted, but it should cover circumstances where a column only contains seconds (ss), or minutes & seconds (mm:ss).

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

The maxDigits setting is needed because the parser is basically adding leading zeros on to the value to keep the values consistent, so 10:30:40 becomes 010030040 (when maxDigits is set to 3).

无论如何,这是一个演示

$(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 ) - 参见此演示

I'm not sure if this matters to you, but there is also a "duration" parser available that allows adding labels after the times (e.g. 10y 30d 6h 10m) - see this demo

这篇关于排序时间hh:mm:带有tablesorter插件的ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆