2017-06-29 96 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