Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable: date / time sorting plug-in not ordering

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 enter image description here

like image 240
Nunyet de Can Calçada Avatar asked May 28 '17 10:05

Nunyet de Can Calçada


1 Answers

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");
like image 151
balexandre Avatar answered Nov 05 '22 08:11

balexandre