jQuery Tablesorter不适用于日期

问题描述 投票:2回答:1

我的代码:

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

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

$.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"
});

有人可以帮我吗?

javascript jquery html tablesorter
1个回答
0
投票

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

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

$.tablesorter.regex.shortDateReplace = /-/g;

$(function() {
  $('table').tablesorter({
    theme: 'blue',
    dateFormat: 'yyyymmdd'
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.