Tablesorter 排序日期和空日期不正确 [英] Tablesorter ordering dates and empty dates incorrectly

查看:37
本文介绍了Tablesorter 排序日期和空日期不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tablesorter 表没有正确排序日期和空字段.

My tablesorter table is not sorting dates and empty fields correctly.

我希望空字段被视为零,并在顶部/底部分组.相反,我在它们下面找到了一些日期.

I would expect empty fields to be treated like zeros, and be grouped at the top/bottom. Instead I get some dates beneath them.

$('table').tablesorter({
    widthFixed: true,
    // sort on the last name in ascending order
    sortList: [
        [2, 0]
    ],
    dateFormat: "ddmmyyyy",
    emptyTo: "zero",
    sortInitialOrder: "desc",
    headers: {
        3: {
            sorter: "shortDate"
        },
        4: {
            sorter: "shortDate"
        },
        5: {
            sorter: "shortDate"
        },
        6: {
            sorter: "shortDate"
        }
    },
    textExtraction: {
        3: function (n, t, c) {
            return $(n).attr('data-date');
        },
        4: function (n, t, c) {
            return $(n).attr('data-date');
        },
        5: function (n, t, c) {
            return $(n).attr('data-date');
        }
    },
    widgets: ['zebra', 'columns']
});

此处查看小提琴.使用出生日期"列进行排序.

See the fiddle here. Sort using the 'Date of birth' column.

推荐答案

shortDate"解析器将所有日期转换为以毫秒为单位的时间,以进行日期比较(使用运算符或日期范围与过滤器小部件)和日期排序更轻松.为了做到这一点,它使用了一个 javascript 函数 getTime():

The "shortDate" parser converts all dates into a time in milliseconds to make date comparison (using a operator or date range with the filter widget) and sorting of dates easier. In order to do that, it uses a javascript function getTime():

getTime 方法返回的值是自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数.

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.

因此,您注意到的问题是因为非日期单元格被视为具有零值,而未正确排序的日期是日期1/1/1930".将此日期输入 javascript 控制台,您将看到以下结果:

So, the issue you are noticing is because non-date cells are treated as having a zero-value, and the date that is not sorting properly is the date "1/1/1930". Entering this date into the javascript console, you'll see this result:

new Date('1/1/1930').getTime()
// returns -1262282400000

请记住,这是自 1970 年 1 月 1 日以来的时间,因此时间返回负数,当然小于零.

Remember, it's the time since 1/1/1970, so the time returns a negative number, which of course is less than zero.

最简单的解决方案是将 emptyTo 选项设置为 "min",它将任何空单元格的值设置为最大负数 (演示):

The simplest solution would be to just set the emptyTo option to "min", which sets the value of any empty cells to be a maximum negative number (demo):

这篇关于Tablesorter 排序日期和空日期不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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