2014-09-23 69 views
0

我使用JQuery Tablesorter版本2.17.8對錶格進行排序
我創建了一個自定義分析器來排序日期,然後是時間,由一個可選的字符串。JQuery Tablesorter:emptyTo:'bottom'對於自定義分析器不起作用

解析器看起來像這樣:

$.tablesorter.addParser({ 
id: "datetime", 
is: function (s) { 
    return /\d{2}\-\d{2}\-\d{2}\s\d{2}:\d{2}(door \w+)?/.test(s); 
}, format: function (s) { 
    arr = s.split(/[\s\-:]/); 
    if(arr[0] == "") // table column is empty 
     timestamp = null; 
    else { 
     // Month is zero-indexed so subtract one from the month inside the constructor 
     date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M 
     timestamp = date.getTime(); 
    } 
    console.log(timestamp); 
    return timestamp; 
}, type: "numeric" 
}); 

表列的實施例的數據進行排序:

  • 30-04-14 9點55
  • 30-04-14 14 :11門I1
  • 空細胞

解析器瓦特按預期的方式,但我希望空單元格被排序到底部,似乎沒有工作。

我把這個在我的PHP文件:

<script type="text/javascript"> 
{literal} 
$(document).ready(function() 
{ 
    $("#betalingen").tablesorter({ 
     headers: { 
      5 : { sorter: false } },  // disable sorting for column #5 
     widgets: ["saveSort", "zebra"],  // apply alternating row coloring 
     sortList: [[2,0]],     // initial sorting order for Date column = ascending (0) 
     emptyTo: 'bottom'     // empty cells are always at the bottom 
     } 
    ); 
}); 
{/literal} 
</script> 

請注意,我在這裏已經想盡選項:http://mottie.github.io/tablesorter/docs/example-option-sort-empty.html
當我刪除我的自定義分析器(讓的tablesorter找出一個解析器),它按照預期將空單元排序到底部,但顯然該列沒有正確排序。

任何人都知道這裏發生了什麼?

+1

數據真的是空的嗎?或者它是空的? – karlingen 2014-09-23 15:01:30

回答

1

你需要稍微調整一下你的解析器。

  • 因爲您可以將解析器設置爲列而不需要自動檢測它,所以您並不需要is函數。
  • @karlingen指出,當單元格爲空時,您仍然返回已被設置爲空的timestamp;它會被保存爲空而不是空字符串。

    $.tablesorter.addParser({ 
        id: "datetime", 
        is: function (s) { 
         // no need to auto-detect 
         return false; 
        }, 
        format: function (s) { 
         var date, timestamp, 
          arr = s.split(/[\s\-:]/); 
         if (arr[0] !== "") { 
          // Month is zero-indexed so subtract one from the month inside the constructor 
          date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M 
          timestamp = date.getTime(); 
         } 
         // is date really a date? 
         return date instanceof Date && isFinite(date) ? timestamp : s; 
        }, 
        type: "numeric" 
    }); 
    
+0

這個作品,謝謝! 我做了不正確的假設,空單元格和null是相同的。我還是相當新的JavaScript/JQuery – Liiva 2014-09-24 08:26:59