Datatable: date / time sorting plug-in not ordering

跟風遠走 提交于 2021-02-08 13:11:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!