problems with the 'ultimate date/time sorting plugin'

problems with the 'ultimate date/time sorting plugin'

scoonscoon Posts: 16Questions: 0Answers: 0
edited May 2019 in Free community support

I've been using dataTables along with the export buttons (thanks again for that), however we have a table column with dates formatted like the 'updated' column in the sample provided here:
https://datatables.net/blog/2014-12-18#Completed-plug-in
I attempted to implement the plugin according to the documentation, however the column sort goes by day, then date, versus date as in the example. Here is my code:

$.fn.dataTable.moment = function ( format, locale ) {
                   var types = $.fn.dataTable.ext.type;
 
                // Add type detection
                   types.detect.unshift( function ( d ) {
                      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 ) {
                      return moment( d, format, locale, true ).unix();
                   };
                };
                $(document).ready( function () {
                        $.fn.dataTable.moment( 'dddd, MMMM Do, YYYY' );
                        $('#inventoryByFirstReportTimeTable').dataTable({
                           dom: 'Blfrtip',
                           
                           buttons: [
                               'copy', 'csv', 'excel', 'print'
                           ]
                           
                        } );
                   } );

And my includes:

<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.19/sorting/datetime-moment.js"></script>

I'm not certain what's missing/incorrect here?
Thanks

Replies

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921

    If its not working then its usually due one or more rows of data don't match the format defined, ie 'dddd, MMMM Do, YYYY'. Without seeing the problem its hard to say. Please post a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • scoonscoon Posts: 16Questions: 0Answers: 0

    Found it- the sort format was indeed the problem. I had one of the data type formats incorrect, with commas in the wrong places.

  • scoonscoon Posts: 16Questions: 0Answers: 0

    Well, correction- it's close, but not quite there. Here are some examples of how the dates are sorting now:
    Thu, Sep 27, 2018
    Thu, Sep 27, 2018
    Fri, Sep 28, 2018
    Thu, Sep 27, 2018
    Thu, Sep 27, 2018
    Thu, Sep 27, 2018
    Thu, Sep 27, 2018
    Thu, Sep 27, 2018
    Sun, Sep 30, 2018
    Thu, Sep 27, 2018
    etc

    So there is an attempt by the system to sort by date, but there are still little anomalies like the ones above where it doesn't quite work. (it seems to sort by month only, where it should sort by month, THEN date)

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921

    Without seeing how you have the code setup with an example of your data its hard to say what the problem is. You changed the format but didn't show us what you changed to. Using the Moment format docs it looks like you need to use this format:

    $.fn.dataTable.moment( 'ddd, MMM DD, YYYY' );

    For example:
    http://live.datatables.net/kotemage/1/edit

    If this doesn't help then we will need to see an example of it not working . Please post a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • scoonscoon Posts: 16Questions: 0Answers: 0

    Lol, so your reply had what I was looking for. The format I was working with was 'ddd, MMM dd, YYYY'. Changing 'dd' to 'DD' was the key.

    Thanks Kevin

  • scoonscoon Posts: 16Questions: 0Answers: 0

    Similar situation on a different page. It seems moment.js doesn't work quite sorting dates when the month format is 'MMMM'.

    The format of the dates on the page in question are:
    'Fri, April 05, 2019 2:03:07 PM' so I used the format 'ddd, MMMM DD, YYYY h:mm:ss A' and 'ddd, MMMM DD, YYYY h:mm:ss a' neither of which worked. The sort results were:

    Wed, March 27, 2019
    2:00:14 PM

    Wed, March 20, 2019
    2:00:07 PM

    Wed, March 13, 2019
    2:00:11 PM

    Wed, March 06, 2019
    2:00:08 PM

    Wed, January 30, 2019
    2:00:11 PM

    Wed, January 23, 2019
    2:00:13 PM

    Wed, January 16, 2019
    2:00:10 PM

    Wed, January 09, 2019
    2:00:35 PM

    Wed, January 02, 2019
    12:01:28 PM

    Wed, February 27, 2019
    2:00:12 PM

    Where we can see the month parameter starts in March, jumps to January, then back to February. Note, I cannot link to a page as the data used in the table is generated by a datasource behind a firewall.

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921

    You can always build a simple test case with your data, for example:
    http://live.datatables.net/surulipa/1/edit

    What you have above does work. I suspect that you have some data in that column that doesn't match that format. I believe that Datatables will scan the columns and if all the data doesn't match a particular format, ie number, date, moment data, etc, then it will treat it as text data. Which is what looks like is happening with your sort.

    Kevin

This discussion has been closed.