Problem with sorting dates with jquery tablesorter

后端 未结 8 727
终归单人心
终归单人心 2021-02-02 12:18

I am using tablesorter plugin to sort my tables in an MVC .NET App. Most of my columns are strings and I\'m having no problems with them. Neither with the numeric ones. The thin

8条回答
  •  梦谈多话
    2021-02-02 12:32

    I got the same problem, and I added a custom parser called datetime:

    $.tablesorter.addParser({
        id: "datetime",
        is: function(s) {
            return false; 
        },
        format: function(s,table) {
            s = s.replace(/\-/g,"/");
            s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
            return $.tablesorter.formatFloat(new Date(s).getTime());
        },
        type: "numeric"
    });
    

    Then you just need to apply that format to the columns you want, as Gabe G exposed (For example to assign this sorter to the first column you should do the following:

    $("#mytable").tablesorter( 
        {   dateFormat: 'dd/mm/yyyy', 
            headers: 
                {
                    0:{sorter:'datetime'}
                } 
        } ); 
    

提交回复
热议问题