使用jQuery tablesorter对mm/yy日期进行排序 [英] Using jQuery tablesorter to sort mm/yy dates

查看:85
本文介绍了使用jQuery tablesorter对mm/yy日期进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery tablesorter插件对表进行排序.我表格中的列中的以mm/yy格式显示日期.

I am using the jquery tablesorter plugin to sort a table. On of my the columns in my table shows the date in mm/yy format.

<tr>
    <td class="col-name">...</td>
    ...
    <td rel="2000" class="col-dob">10/00</td>
    ...
</tr>
<tr>
    <td class="col-name">...</td>
    ...
    <td rel="1986" class="col-dob">11/86</td>
    ...
</tr>

注意:

  • 每个单元格都有一个唯一的类
  • 日期以mm/yy格式显示
  • 带有日期的单元格也接收年份

我的jQuery代码如下:

My jQuery code is as below:

// add parser through the tablesorter addParser method
$.tablesorter.addParser({
        // set a unique id
        id: 'user-birthdate',
        is: function(s) {
                // return false so this parser is not auto detected
                return false;
        },
        format: function(s) {
                // format your data for normalization

                var dateSplit = s.split('/');

                if(2 !== dateSplit.length)
                        return 0;

                return new Date(dateSplit[1], dateSplit[0], 1);
        },
        // set type, either numeric or text
        type: 'numeric'
});

myClass.init = function() {
        $('.module .user table').tablesorter({
                sortList: [[0,0]],     
             widgets: ['zebra'],
                headers: {
                        5: {
                                sorter:'user-birthdate'
                        }
                }
        });
}

myClass.init();

我的问题是tableSorter将00解释为1900年而不是2000年,因此排序后的数据不正确.

My problem is that the tableSorter interprets 00 as year 1900 instead of 2000 and hence the sorted data is not correct.

任何提示我该如何解决?我正在使用jQuery 1.2.6和最新版本的tablesorter.

Any clue how can I resolve this? I am using jQuery 1.2.6 and the latest version of tablesorter.

推荐答案

tablesorter文档通常非常无用.它看起来很像,但缺少细节.

The tablesorter documentation is often rather unhelpful, I've found. It looks like it says a lot, but is lacking in the details.

在这种情况下,它不会告诉您解析器的功能签名.幸运的是,您可以阅读未缩小的代码来找到它.

In this case, it doesn't tell you the function signature for a parser. Fortunately, you can read the unminified code to find it.

我们发现metadata解析器可以做到这一点:

There we find that the metadata parser does this:

format: function(s,table,cell) {

这意味着您可以将格式方法调整为:

This means that you can adjust your format method to:

format: function(s, table, cell) {
    // format your data for normalization

    var dateSplit = s.split('/');
    var year = $(cell).attr('rel');

    if(2 !== dateSplit.length)
        return 0;

    return new Date(year, dateSplit[0], 1);
},

或至少与此类似.我还没有实际测试过.但这至少应该非常接近.

Or at least similar to that. I haven't actually tested this. But it should be at least very close.

这篇关于使用jQuery tablesorter对mm/yy日期进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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