2014-10-31 37 views
1

我有我的表一欄,顯示下面的文本,其中的日期改變JQuery的表分揀機不能與日期範圍字符串工作

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

的tablesorter有麻煩整理此正確,你可以變化在這個小提琴看到。點擊時第一列將排序,但第二列不會。我還包括一些字符串比較的表格表明,JavaScript的正確識別它們應該在的順序。

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

我嘗試添加自定義textExtraction功能,但我仍然得到同樣的結果。

似乎tablesorter正在做一些不同於簡單的><來確定字符串值的順序。有沒有一種方法可以改變tablesorter來正確地對這個列進行排序?

回答

1

問題是第二列(「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; 
    } 
});