【发布时间】:2017-10-28 19:26:18
【问题描述】:
我有一个基本的 SpringBoot 应用,嵌入式 Tomcat,Thymeleaf 模板引擎 我想订购数据表的 1 个日期列。
在我的 POJO 中:
public String getTimeFormatted() {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEEE, MMMM d,yyyy h:mm,a", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.UTC);
return dateTime.format(formatter);
}
在模板中:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.15/sorting/datetime-moment.js"></script>
<script th:inline="javascript">
$(document).ready(function() {
$.fn.dataTable.moment( 'EEEE, MMMM d,yyyy h:mm,a' );
$('#table').dataTable( {
"bLengthChange": false,
"pageLength": 25,
});
} );
</script>
【问题讨论】:
-
我做的技巧实际上是添加一个带有
hidden的跨度和unix数据时间,所以DataTable可以正确排序...... -
如果没有指定排序函数,数据表排序是按字母顺序排列的。 @balexandre 使用的解决方法是可以的,但“正确”的方法是根据您使用的日期格式定义排序函数。 datatables.net/plug-ins/sorting/#Custom-data-source-sorting
标签: jquery spring-boot datatable datatables thymeleaf