Jquery: TableSorter- Date with specic format is not working

╄→尐↘猪︶ㄣ 提交于 2019-12-07 10:27:45

问题


I am using Tablesorter plugin to sort the table . fourth column is date fields having format :

-->30 Jan 2013

-->01 Feb 2013

when i try to sort format it gives wrong sorting.

My View page:(one of the date column )

<td onclick="viewTrainingeDetails(${privateTrainingInstance?.id})"><g:formatDate format="dd MMM yyyy" date="${privateTrainingInstance?.startDate}" /></td>

jquery

 $(function() {
         $("#myTable").tablesorter(); 
   });

回答1:


Try adding this custom parser (demo):

$.tablesorter.addParser({
    id: "date",
    is: function (s) {
        return false;
    },
    format: function (s, table) {
        return new Date(s).getTime() || '';
    },
    type: "numeric"
});

then initialize the plugin like this:

$('table').tablesorter({
    headers: {
            5: { sorter: 'date' }
        }
});

Update: for best results, make sure you are returning a valid date:

$.tablesorter.addParser({
    id: "date",
    is: function (s) {
        return false;
    },
    format: function (s, table) {
        var date = new Date(s);
        return date instanceof Date && isFinite(date) ? date.getTime() : '';
    },
    type: "numeric"
});


来源:https://stackoverflow.com/questions/14476899/jquery-tablesorter-date-with-specic-format-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!