问题
I have a basic SpringBoot app., embedded Tomcat, Thymeleaf template engine I want to order 1 date column of a datatable.
in my 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);
}
in the template:
<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>
but it does not order properly
回答1:
This is quite easy to debug.
I even made a simple example.
You are using a format as EEEE, MMMM d,yyyy h:mm,a
in your code (I assume in spring) but you forgot to translate that into moment
format... and from the docs, that should be: dddd, MMMM D,YYYY h:mm,a
so the code should actually be:
$.fn.dataTable.moment("dddd, MMMM D,YYYY h:mm,a");
回答2:
Personally, I prefer to use a data-order attribute. Then I just pass it the timestamp in either epoch
or YYYYmmddHHiiss
.
You can see an example here: https://datatables.net/examples/advanced_init/html5-data-attributes.html
来源:https://stackoverflow.com/questions/44226347/datatable-date-time-sorting-plug-in-not-ordering