Using jQuery tablesorter to sort mm/yy dates

前端 未结 2 1369
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 07:57

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.


    

        
相关标签:
2条回答
  • 2020-12-19 08:21

    I think you will find that your problem is the Date constructor and the 2-digit year string you are passing without disambiguation: new Date(dateSplit[1], dateSplit[0], 1);

    I don't think you can (easily) get access to rel based on s in the parser. Does s contain the entire contents of the cell? Can you do something in the data in the cell like: <span style="display : none">CC</span>MM/YY, strip out the tags and then combine CC with YY in your parse?

    0 讨论(0)
  • 2020-12-19 08:23

    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.

    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.

    0 讨论(0)
提交回复
热议问题