2017-06-29 95 views
1

我是新來的jQuery等,但建立一個網站與HTML表,並設法讓jQuery tablesorter工作。然而,在一列我早就日期就像6月5日,2017年使用jQuery tablesorter排序長日期

如果按日期列排序,它的排序是這樣的行:

2017年7月6日

2017年6月29日

2018年5月21日

的代碼我使用的頭部分是:

<!-- load jQuery and tablesorter scripts --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="js/jquery.tablesorter.js"></script> 
<script> 
$(document).ready(function() { 
    $("#myTable").tablesorter({sortList: [[0,1]]}); 
}); 
</script> 

我的表看起來像這樣:

<table id="myTable" class="table table-bordered table-striped tablesorter"> 
        <colgroup> 
         <col class="col-lg-2"> 
         <col class="col-lg-2"> 
         <col class="col-lg-5"> 
         <col class="col-lg-3"> 
        </colgroup> 
        <thead> 
         <tr> 
          <th>Date</th> 
          <th>Location</th> 
          <th>Event</th> 
          <th>Organiser</th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr> 
          <td>29 Jun, 2017</td> 
          <td>New York</td> 
          <td>Book Club</td> 
          <td>John Smith</td> 
         </tr> 
         <tr> 
          <td>6 Jul, 2017</td> 
          <td>New York</td> 
          <td>Book Club</td> 
          <td>John Smith</td> 
         </tr> 
         <tr> 
          <td>21 May, 2018</td> 
          <td>Chicago</td> 
          <td>Book Expo</td> 
          <td>Bob Smith</td> 
         </tr> 
        </tbody> 
       </table> 

任何幫助將不勝感激!

回答

1

您可以在日期列的th標記內使用shortDate分析器和data-date-format選項。這裏是你的代碼jsfiddle https://jsfiddle.net/savicmi/htLdapj8/<th data-sorter="shortDate" data-date-format="d MMM yyyy">Date</th>

更多關於https://mottie.github.io/tablesorter/docs/example-option-date-format.html的tablesorter日期格式選項。

+0

非常感謝!直到我將>更改爲這就是你似乎正在使用的。日期現在排序正確,但CSS不再有效,特別是gif箭頭不再出現在列標題旁邊。有任何想法嗎? – user3200365

+0

該CSS如下所示: table.tablesorter thead tr .header { background-image:url(/img/bg.gif); background-repeat:no-repeat; background-position:center right;光標:指針; } table.tablesorter thead tr .headerSortUp { background-image:url(/img/asc.gif); } table.tablesorter thead tr .headerSortDown { background-image:url(/img/desc.gif); – user3200365

+0

在您的html文件中包含css文件,例如https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.14/css/theme.default.min.css。 – savicmi