Datetime-moment sorting not working

Datetime-moment sorting not working

jwajwa Posts: 4Questions: 2Answers: 0

Hi,

I'm trying to sort some columns with the datetime-sorting plugin.

This is the relevant code:

            $.fn.dataTable.moment('DD.MM.YYYY');

            dataTable = $('#table-inventar').DataTable({
                ajax: {
                    url: '/' + table + '/json',
                    dataSrc: ''
                },
                columns: columns,
                language: {
                    url: '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/German.json'
                },
                responsive: true
            });

It does not sort the relevant columns correctly (no change to before the integration of the plugin).

I tried debugging the plugin.The types.detect.unshift part seems to work correctly. moment( d, format, locale, true ).isValid() becomes true where expected. However, when sorting the relevant column, the types.order[ 'moment-'+format+'-pre' ] part is never called, which i suppose it should? What am I doing wrong?

Im using jQuery 3.1.1, dataTables 1.10.16, moment.js 2.22.1 and the current version of the datetime-moment plugin.

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    Answer ✓

    That looks fine. Do you have rows in the table that do not contain a valid date? If it is likely not to work. This post has a couple of valuable hints:
    https://datatables.net/forums/discussion/49621

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Answer ✓

    We'd really need a link to a page showing the issue. The error suggests that one or more data points in the column don't match the date format given.

    Allan

  • jwajwa Posts: 4Questions: 2Answers: 0

    There was indeed an invalid date in the data. The item didn't show up in the table at all, so I didn't notice before. Thank you for your help!

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    Maybe relevant for you @jwa or somebody else. I just had to modify the date time plug in to also handle non date values. This is what I did to include a row with totals into one of my tables. By returning a high date in case the totals row is hit I make sure the totals stay at the bottom when ordering ascending and vice versa.

    (function (factory) {
        if (typeof define === "function" && define.amd) {
            define(["jquery", "moment", "datatables.net"], factory);
        } else {
            factory(jQuery, moment);
        }
    }(function ($, moment) {
     
    $.fn.dataTable.moment = function ( format, locale ) {
        var types = $.fn.dataTable.ext.type;
     
        // Add type detection
        types.detect.unshift( function ( d ) {
            if ( d ) {
                // Strip HTML tags and newline characters if possible
                if ( d.replace ) {
                    d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
                }
     
                // Strip out surrounding white space
                d = $.trim( d );
            }
     
            // Null and empty values are acceptable
            if ( d === '' || d === null || d === "Summen per Laufzeitende" ||
                                           d === "Totals"                      ) {
                return 'moment-'+format;
            }
     
            return moment( d, format, locale, true ).isValid() ?
                'moment-'+format :
                null;
        } );
     
        // Add sorting method - use an integer for the sorting
        types.order[ 'moment-'+format+'-pre' ] = function ( d ) {
            if ( d ) {
                
                if ( d === "Summen per Laufzeitende" ) {
                    d = "31.12.2999";
                }
                if ( d === "Totals" ) {
                    d = "31/12/2999";
                }
                // Strip HTML tags and newline characters if possible
                if ( d.replace ) {
                    d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
                }
     
                // Strip out surrounding white space
                d = $.trim( d );
            }
             
            return !moment(d, format, locale, true).isValid() ?
                Infinity :
                parseInt( moment( d, format, locale, true ).format( 'x' ), 10 );
        };
    };
     
    }));
    
  • nikhil_gyannikhil_gyan Posts: 1Questions: 0Answers: 0

    Try this,

    // "sType": "date" TO SPECIFY SORTING IS APPLICABLE ON DATE
    "aoColumns": [
    null,
    null,
    null,
    null,
    {"sType": "date"},
    null
    ]

This discussion has been closed.