开发者

Tablesorter custom date with possible other values

I am using latest version of the tablesorter plugin and I need to sort a column in a special way.

Values can be like this:

Tablesorter custom date with possible other values

I need开发者_StackOverflow社区 to order by the date, so values like Q4 (31-12-2010) should show up as the first

The date format is like: dd-mm-yyyy

Is this even possible?


I redid this using their custom parsers

http://jsfiddle.net/rTrqz/2/

Seems work work for your purposes.

The important parts are here:

$.tablesorter.addParser({
    id: 'quarters',
    is: function (s) {
        return false;
    },
    format: function (s) {
        s = s.toLowerCase().replace(/q[0-9]/,'').replace(/\(/,'').replace(/\)/,'');            
        match = s.match(/(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20\d\d)/);
        return match[3] + match[2] + match[1]; 
    },
    type: 'text'
});


Here is a sample of how I would do this:

     $.tablesorter.addParser({
        id: 'quarters',
        is: function (s) {
            return false;
        },
        format: function (s) {
            var match = s.match(/(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20\d\d)/);
            if (match)
            {
               return match[3] * 1000 - match[2] * 100 - match[1] * 10;                
            }
            else return 0;
        },
        type: 'text'
    });

    $(document).ready(function () {
        $("table").tablesorter({
            headers: {
                0: {
                    sorter: 'quarters'
                }
            }
        });
    });                  

You can adjust the return for the Not Valid Date part to whatever integer you want (0 makes it come before the earliest date.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜