【问题标题】:Jquery: TableSorter- Date with specic format is not workingJquery:TableSorter-具有特定格式的日期不起作用
【发布时间】:2013-01-06 18:23:13
【问题描述】:

我正在使用Tablesorter 插件对表格进行排序。 第四列是具有格式的日期字段:

-->2013 年 1 月 30 日

-->2013 年 2 月 1 日

当我尝试对格式进行排序时,它给出了错误的排序。

我的查看页面:(日期栏之一)

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

jquery

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

【问题讨论】:

  • 使用 beforesort & aftersort 事件在 unix 模式下为 ex 转换日期
  • 我认为您应该为回答者投票! :)

标签: javascript jquery jquery-plugins tablesorter


【解决方案1】:

尝试添加此自定义解析器 (demo):

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

然后像这样初始化插件:

$('table').tablesorter({
    headers: {
            5: { sorter: '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"
});

【讨论】:

  • 我想对答案投两次赞成票 :( 这个解析器比 SO 中与此问题相关的所有其他答案更简单,也更有意义
猜你喜欢
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多