Date sorting with moment.js. Empty cells to bottom, please.

Date sorting with moment.js. Empty cells to bottom, please.

effkayeffkay Posts: 12Questions: 3Answers: 0
edited December 2016 in Free community support

I'm using the moment.js-plugin for sorting localized dateformats and it works great. My only gripe is that empty cells are listed before cells with a value when the relevant column is sorted in descending order. Implementing a custom sorting-function seems to be the only way to achieve this. Unless there's some config-option I've missed, this seems to be an overly difficult and complicated way just to achieve that particular behaviour. Compare this to jQuery tablesorter 2.0 which has the very convenient option {emptyCellsTo: 'bottom'}.

I haven't considered writing a custom sorting function, but how would it combine with the moment.js sorting-plugin if I decided to write one? Does the community have any other suggestions? Isn't it about time that something similar to tablesorter 2.0's solution be added to DataTables?

Thanks in advance!

This question has an accepted answers - jump to answer

Answers

  • effkayeffkay Posts: 12Questions: 3Answers: 0

    No one has anything on this?

  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin

    Isn't it about time that something similar to tablesorter 2.0's solution be added to DataTables?

    Sounds like a good idea. I've never seen that option before - but I do like it and I've add it to my list.

    In the mean time, you could modify this file to allo the auto detect to accept empty strings as a valid value for that type. Oddly the sorting function itself does already handle an empty string as -Infinity (i.e. bottom), but the auto detect isn't allowing it to get that far.

    Allan

  • effkayeffkay Posts: 12Questions: 3Answers: 0

    I'm delighted to see that you still inhabit your own forum, Allan, active as ever :smile:

    So that's why modifying the moment-sorter didn't work. I'll have a looksie later and get back to you.

    I think it'd be a useful feature to have, and I'm glad you agree.

    Thanks!

  • effkayeffkay Posts: 12Questions: 3Answers: 0
    edited December 2016

    I think that you did your job well when you wrote the moment-plugin back in 2014. The type detection code includes these lines:

    // Null and empty values are acceptable
    if ( d === '' || d === null ) {
        return 'moment-'+format;
    }
    

    ... which would seem to indicate that empty - or null values are indeed treated as valid for that type....... wouldn't it? I'm not sure modifying the type detection is the solution here. If I'm reading the code correctly, the sort function converts my dates to a unix timestamp and any given unix timestamp will always be more than -Infinity. So, if I sort my dates in ascending order, wouldn't it make sense that the empty rows (which are treated as -Infinity) come before those with a value? That is precisely the behaviour I'm seeing now.

    Edit:
    In case it wasn't clear in my OP, I want empty cells to be at the bottom no matter which way the date-column is sorted, i.e. they're ignored. I don't know how to accomplish this in the moment sorting-function. I mean, which value could it return so that the relevant row is sent to bottom no matter what?

    Thanks

  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin
    Answer ✓

    ... which would seem to indicate that empty - or null values are indeed treated as valid for that type....... wouldn't it?

    That is in the sorting part of that plug-in.

    A few lines above is where it adds its type detection. There it accepts only a valid Moment date. That should be modified to check for empty string or null as well and allow that to be valid.

    In case it wasn't clear in my OP, I want empty cells to be at the bottom no matter which way the date-column is sorted

    Ah - no I didn't get that. That would require a different sorting method in that case. Rather than having a single -pre sorting function, you'd need to provide both a -asc and -desc sorting function that would each sort the empty cells to the bottom.

    There documentation here will explain that terminology.

    So yes, not quite as simple as I had thought, but still quite do-able.

    Allan

  • effkayeffkay Posts: 12Questions: 3Answers: 0

    That is in the sorting part of that plug-in.

    No, it is not. Here is the same code + the final return statement from the type detection part.

    // Null and empty values are acceptable
    if ( d === '' || d === null ) {
        return 'moment-'+format;
    }
    
    return moment( d, format, locale, true ).isValid() ?
        'moment-'+format :
        null;
    

    Yeah, it's as my research indicated initially then; a custom sorting function is needed. That's why I mentioned jQuery Tablesorter 2's emptyCellsTo-option. It works the way I want and from the dev's point of view, it's simple and easy to use. Hopefully it will be part of DataTables' repertoire in the future as well :smile:

    Thanks for your help, Allan.

  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin

    Yup - I think that would be a useful addition. Thanks!

  • effkayeffkay Posts: 12Questions: 3Answers: 0
    edited December 2016

    For future reference and/or anyone wondering, this is how I ended up doing it in my case:

    function dateFormatter(arg, ascending)
    {
        date = moment( arg, 'YYYY-MM-DD', 'en', true );
    
        if ( ! date.isValid() )
        {
            return ascending ? Infinity : -Infinity;
        }
    
        return parseInt(date.format( 'x' ), 10);
    }
    
    $.extend( $.fn.dataTable.ext.type.order, {
        'moment-asc': function ( a, b ) {
            a = dateFormatter(a, true);
            b = dateFormatter(b, true);
    
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        },
      
        'moment-desc': function ( a, b ) {
            a = dateFormatter(a);
            b = dateFormatter(b);
    
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    });
    
  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin

    Nice one! Thanks for sharing that with us!

    Allan

This discussion has been closed.