Date Sorting when not all rows have valid date

Date Sorting when not all rows have valid date

archiearchie Posts: 1Questions: 1Answers: 0

Hi, we are using the ultimate date/time sorting plugin and this works fine when we have dates in the column but sometimes we have 'TBC'. When this is the case the date sorting doesn't work. Can anyone provide help on how to resolve this?
Thanks

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓

    I guess you'd need to modify the code of the plugin a little.
    https://datatables.net/plug-ins/sorting/datetime-moment

    Looking at the code you might need to change this:

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

    to be like this:

    // Null , TBC and empty values are acceptable
            if ( d === '' || d === null || d === 'TBC') {
                return 'moment-'+format;
            }
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Orthogonal date is another option (set TBC to be 0 for the date integer). Or if you want to keep TBC always at the top or bottom of the table this blog post might be of interest.

    Allan

  • colincolin Posts: 15,151Questions: 1Answers: 2,587

    Yep, as @rf1234 says, you could do that. Another option, and perhaps more future proof if the plugin is updated for any reason, is to just columns.render your column so that the sort data returns something that moment already supports, for example:

      "columnDefs": [ {
        "targets": 4,
        "render": function ( data, type, row, meta ) {
          return type === 'sort' && data === 'TBC' ? '' : data;
        }
      } ]
    

    Cheers,

    Colin

This discussion has been closed.