Sorting date colum with existing null values

Sorting date colum with existing null values

LapointeLapointe Posts: 430Questions: 81Answers: 4

Hello all
I'm back....
I can't find solution for this case: I have a table with up to four data fields and want to sort data for some of them;
In fact I use in datatable (thanks for previous help about this subject) these settings:

....
    {data: 'col0' },
    {data: 'col1' },
    {data: 'col2' },
    {data: 'col3' },
    {data: 'col4Date'},
    {data: 'col5Date'},
    {data: 'col6Date'},
    {data: 'col7Date'}
.......

columnDefs: [{target: [4,5,6,7]}]
.......
$(document).ready(function() {
.....
        $.fn.dataTable.moment = function ( format, locale ) {
            var types = $.fn.dataTable.ext.type;
            types.detect.unshift( function ( d ) {      // Add type detection
                return moment( d, format, locale, true ).isValid() ?
                    'moment-'+format :
                    null;
            } );
            types.order[ 'moment-'+format+'-pre' ] = function ( d ) {   // Add sorting method - use an integer for the sorting
                return moment( d, format, locale, true ).unix();
            };
        };      
            
        $.fn.dataTable.moment( 'ddd DD MMM YYYY HH:mm', 'fr' );
....

So when I try to sort a column where date is set for all records, the sorting is ok, else not (sorted as a string field)

How can I set explicitly (force) or bypass type detection to set a column order correctly ?

Like ? : (or other possibility ?)

        $.fn.dataTable.moment = function ( format, locale ) {
            var types = $.fn.dataTable.ext.type;
            types.detect.unshift( function ( d ) {      // Add type detection
                return (moment( d, format, locale, true ).isValid() || (d===null)) ?
                    'moment-'+format :
                    null;
            } );
            types.order[ 'moment-'+format+'-pre' ] = function ( d ) {   // Add sorting method - use an integer for the sorting
                return (d===null) ? null : moment( d, format, locale, true ).unix();
            };
        };      
            
        $.fn.dataTable.moment( 'ddd DD MMM YYYY HH:mm', 'fr' );

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    Funny there is another thread today with almost the opposite problem. Looks like if you get the CDN version of the plugin from the blog it will also allow for "" or null.

    Kevin

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi Kevin
    In fact I have a look to your link (including null and empty detection) and find it as in js include in my website, but in code I did add the short method (ultimate date / time sorting) that does not allow empty and null values.
    So I just comment these line and everythink goes OK
    Thanks

This discussion has been closed.