2014-01-21 107 views
2

我目前使用Tablesorter(一個jQuery插件)對錶進行排序。我試圖以yyyy MMM dd格式排序日期,但它似乎不能做到這一點。我需要說我的日期輸入法語如此:jQuery tablesorter自定義日期格式

  • 2014 janv。 05
  • 2013févr。 03
  • 2011火星02

我嘗試了很多東西,但它只是不排序正確的方式。我不知道是因爲我的日期輸入還是法語或其他什麼,但我即將放棄這一點。

下面是我用

$.tablesorter.addParser({ 
      id: "date", 
      is: function (s) { 
       return false; 
      }, 
      format: function (s, table) { 
       var date = s.split(' '); 
       var month = translateMonth(date[1]); 

       var d = new Date(date[0], month, date[2]); 
       console.log(d.toString()); 

       return d.getTime(); 
      }, 
      type: "numeric" 
     }); 

而且

$("#table").tablesorter({ 
      headers: { 
       2: { 
        sorter: 'date' 
       } 
      } 
     }); 

而且

function translateMonth(month) { 
      switch (month) { 
       case "janv.": return 0; 
       case "févr.": return 1; 
       case "mars": return 2; 
       case "avril": return 3; 
       case "mai": return 4; 
       case "juin": return 5; 
       case "juil": return 6; 
       case "août.": return 7; 
       case "sept.": return 8; 
       case "oct.": return 9; 
       case "nov.": return 10; 
       case "déc.": return 11; 
       default: return -1; 
      } 
     } 

天代碼正確排序但這裏的問題是我幾個月

我希望任何形式的幫助

感謝 - S

+0

我發現,數據表是相當驚人的,可擴展的。 http://datatables.net/ – cgatian

回答

2

你說得對,它不知道這些月份縮寫。有日期庫可以支持這種事情,如moment.js這是一個很棒的工具,如果你能夠使用jQuery。如果沒有,那麼只需將縮寫轉換爲適當的月份數字即可。

function translateMonth(month) 
{ 
switch (month) 
{ 
    case 'janv.': return 0; 
    case 'févr.': return 1; 
    case 'févr.': return 2; 
    case 'mars': return 3; 
    case 'avril': return 4; 
    case 'mai': return 5; 
    case 'juin': return 6; 
    case 'juil.': return 7; 
    case 'août': return 8; 
    case 'oct.': return 9; 
    case 'nov.': return 10; 
    case 'déc.': return 11; 
    default: return -1; 
} 
} 

$.tablesorter.addParser({ 
    id: "customParser", 
    is: function (s) { 
     return false; 
    }, 
    format: function (s) { 
     var date = s.split(' '), 
      month = translateMonth(date[1]); 
     if(month >= 0) 
      return new Date(date[0], month ,date[2]).getTime(); 
     else 
      return new Date().getTime(); 
    }, 
    type: 'numeric' 
}); 
+0

我想回答你一個積極的迴應,但不幸的是日期仍然排序不好。我更新了我的問題中的代碼。謝謝您的幫助。 – Sebastien

+0

@Sebastien更改解析器的名稱,因爲已經有一個「日期」解析器,它可能不會被您的自定義解析器替換。 – Mottie

0

好吧我找到了。我的JavaScript文件沒有更新。其次,我用我的細胞屬性來獲得一個傳統的日期。是的,這裏真正的問題是,我不得不處理可摺疊的行。

我的代碼:

$(function() { 

      $.tablesorter.addParser({ 
       id: "date", 
       is: function (s) { 
        return false; 
       }, 

       type: "text", 
       format: function (s, table, cell, cellIndex) { 
        var tr = $(cell).parent("tr"); 
        var date = tr.data("date"); 
        var id = tr.data("id"); 
        var ind = tr.data("ind"); 

        return date + " " + id + " " + ind; 
       } 
      }); 

      $("table").tablesorter({ 
       headers: { 2: { sorter: "date" } 
       }, 
       sortList: [[1, 0]], 
       widgets: ['group', 'filter'], 
       widgetOptions: { 
        group_collapsible: true, 
        group_collapsed: false, 
        group_count: false, 
        filter_childRows: false, 
       } 
      }); 
     }); 

     $(".open-supp").click(function (e) { 
      var detail = $(this).closest("tr").next("tr.supp"); 
      $("tr.supp").not(supp).hide(); 
      detail.toggle("fast"); 

      e.preventDefault(); 
     }); 

感謝

+0

解析器不會正確排序日期名稱,除非「data-id」屬性具有月份的索引。此解析器將要求您爲每個表格行輸入3個自定義數據屬性,而@凱文的答案不需要任何數據屬性。 – Mottie