JQuery的表分拣机不能与日期范围字符串工作

问题描述:

我有我的表一栏,显示下面的文本,其中的日期改变JQuery的表分拣机不能与日期范围字符串工作

Requested Statement 7/1/2014 - 9/16/2014 

的tablesorter有麻烦整理此正确,你可以变化在这个小提琴看到。点击时第一列将排序,但第二列不会。我还包括一些字符串比较的表格表明,JavaScript的正确识别它们应该在的顺序。

http://jsfiddle.net/kfu4ragh/1/

我尝试添加自定义textExtraction功能,但我仍然得到同样的结果。

似乎tablesorter正在做一些不同于简单的><来确定字符串值的顺序。有没有一种方法可以改变tablesorter来正确地对这个列进行排序?

问题是第二列(“Requested Statement ...”)被检测为日期列,解析器试图将整个字符串转换为日期;这是无效的。

这里是a demo with the relevant extracted out functions from tablesorter。其结果是:

// start with "Requested Statement 7/1/2014 - 9/16/2014" 
"Requested Statement 2014/7/1/9/16/2014" => 0 

所以你需要使用textExtraction功能为目标的日期(demo):

$('table').tablesorter({ 
    textExtraction : function(node){ 
     var txt = $(node).text(); 
     if (/request/i.test(txt)) { 
      // return the 3rd block (first date in range) 
      txt = txt.split(' ')[2]; 
     } 
     return txt; 
    } 
}); 

注意字符串中的第二次约会时完全忽略。如果您想提出第二次约会事宜,请尝试以下代码(demo):

$('table').tablesorter({ 
    textExtraction : function(node){ 
     var d1, d2, 
      txt = $(node).text(); 
     if (/request/i.test(txt)) { 
      // return the 3rd block (first date in range) 
      txt = txt.split(' '); 
      d1 = $.tablesorter.formatFloat(new Date(txt[2]).getTime()); 
      d2 = $.tablesorter.formatFloat(new Date(txt[4]).getTime()); 
      // add the times together - there is likely a better 
      // method but this works in this situation 
      txt = d1 + d2; 
     } 
     return txt; 
    } 
});