jquery - 使用 jQuery tablesorter 对长日期进行排序

标签 jquery tablesorter

我是 jQuery 等新手,但正在构建一个带有 HTML 表格的网站,并设法使 jQuery 表排序器正常工作。然而,在其中一列中,我有很长的日期,例如 2017 年 6 月 5 日。

当按日期列排序时,它对行进行排序,如下所示:

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 看起来像这样:

<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>

任何帮助将不胜感激!

最佳答案

您可以在日期列的 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 日期格式选项的更多信息.

关于jquery - 使用 jQuery tablesorter 对长日期进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816637/

相关文章:

javascript - 如何使用javascript获取值(value)包?

javascript - 使用 < 和 > 替换 < 和 >

jquery - 如何使用 Jquery 按表格子标题排序

javascript - 触发更新后,Tablesorter 滚动条的高度保持不变

tablesorter如何设置动态滚动条高度

javascript - jQuery,如何通过属性 NAME 查找元素?

javascript - 使用 jquery/javascript 将带有下拉列表的 html 表导出到 CSV 文件

javascript - 重置后退按钮上的表单(Safari)

javascript - Modernizr 减慢了表排序器的速度

jquery - 使用jquery tablesorter时如何显示表中的行数?