首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery表排序程序不使用日期

问jQuery表排序程序不使用日期
EN

Stack Overflow用户
提问于 2020-03-05 23:04:44
回答 1查看 50关注 0票数 2

我的代码:

<th class="sorter-shortDate">Date</th> $('table').tablesorter({dateFormat: "yyyymmdd"});

排序适用于数字,但不适用于日期,我的日期格式是y-m-d H:i,我还尝试添加自定义解析器:

代码语言:javascript
复制
$.tablesorter.addParser({
    id: "customDate",
    is: function(s) {
          return false;
          //use the above line if you don't want table sorter to auto detected this parser
          //else use the below line.
          //attention: doesn't check for invalid stuff
          //2009-77-77 77:77:77.0 would also be matched
          //if that doesn't suit you alter the regex to be more restrictive
          //return /\d{1,4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}/.test(s);
    },
    format: function(s) {
          s = s.replace(/\-/g," ");
          s = s.replace(/:/g," ");
          s = s.replace(/\./g," ");
          s = s.split(" ");
          return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3]).getTime()+parseInt(s[6]));
    },
    type: "numeric"
});

有谁能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-06 11:45:33

shortDate解析器中有一个小错误;它将所有.替换为/,同时将日期转换为内置JS日期解析器可以理解的内容(例如,2009-12-31 08:09:10.1变为12/31/2009 08:09:10/1 -将最后一个小数位改为斜杠将破坏所有内容。

您可以通过修改短日期解析器的正则表达式demo来解决此问题

代码语言:javascript
复制
$.tablesorter.regex.shortDateReplace = /-/g;

$(function() {
  $('table').tablesorter({
    theme: 'blue',
    dateFormat: 'yyyymmdd'
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60548333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档