tablesorter,不会正确地按日期排序 [英] tablesorter, wont sort by date correctly

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

问题描述

如果有空格的话,tableserver的行为是不一样的吗?

Does the tablesorter behave differently if there is whitespace about?

我对tablesorter很新,但我很快被要求添加到客户的网站允许他们按发布日期排序)。据我所知,日期(例如2012年5月14日)被读为文本,所以我需要添加一个解析器将其更改为数字,以便它正确排序。 (例如,14052012)

I'm pretty new to tablesorter but I was asked quickly to add it to a customers site (specifically to allow them to sort by date published). As far as i am aware, the date (eg. 14 May 2012) is read as text, so I need to add a parser to change this to numeric in order for it to sort properly. (eg. 14052012)

这我有(目前使用的代码是由其他开发人员编写的 - 驻留在tablesorter.js中)。

This I have (currently using code written by the other developer - resides in the tablesorter.js).

$.tablesorter.addParser({
id: 'dayMonthYear',
is: function(s) {
    return false;
},
format: function(s) {           

    var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
    var day = String(date[1]);
    if (day.length == 1) {
        day = "0" + day;
    }        

    var month = monthNames[date[2]];
    var year = date[3];
    var sortableDate = '' + year + month + day;
    return sortableDate;
},
type: 'numeric'  
});

var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

我也把它放在html页面

I also have this placed in the html page

$("table#searchresults").tablesorter ({
    headers: {
        0: {sorter:'dayMonthYear'}
    }
});

虽然我已经看到这个代码工作,它似乎并不适用于我。该月仍在阅读为文本。

Although I've seen this code working, it doesnt seem to work for me. The month is still being read as text.

任何想法?我只能看到不同的工作演示和我的网站,是表有负载的空白踢,这可能是一个问题?

Any ideas? The only thing I can see different between the working demo and my site, is that the table has loads of whitespace kicking about, could this be an issue?

编辑:刚才发现这个, http://beausmith.com/blog/custom-date-sorting-for-jquery-tablesorter-plugin/ ,看起来很熟悉上面的代码。希望它帮助...

Just found this, http://beausmith.com/blog/custom-date-sorting-for-jquery-tablesorter-plugin/, looks familiar to the above code. Hopefuly it helps...

推荐答案

有效... phew

Got it working.. phew

$.tablesorter.addParser({
id: 'dayMonthYear',
is: function(s) {
    return false;
},
format: function(s) {       

    s = $.trim(s.replace(/\s+/g, ' '));


    var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
    var day = String(date[1]);
    if (day.length == 1) { day = "0" + day;}        
    var month = monthNames[date[2]];
    var year = date[3];

    return sortableDate = '' + year + month + day;
},
type: 'numeric'  
});
var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

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

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