Replacing empty date with string

Replacing empty date with string

bczm8703bczm8703 Posts: 16Questions: 5Answers: 0

would like to check if it is possible to replace the cell with an empty date with a string, eg: Never, and this string should not affect the sorting of the date. this string is to be sorted at last.

Answers

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

    It depends on what you are using.

    If you are using columns.data
    https://datatables.net/reference/option/columns.data
    it is easy. Just use a renderer.

    columns: [
        {   data: "yourDateField",
            render: function ( data, type, row ) {
                if ( data <= "" ) {
                    return "Never";
                }
                return data;
            }    
        },
    

    If you are not using columns data you need to work with cell().data().
    https://datatables.net/reference/api/cell().data()

    Sorting: Same thing, it depends on what you are using.

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

    Sorting using a modified version of this plugin: https://datatables.net/plug-ins/sorting/datetime-moment

    Here is a modified version that sorts "Never" to the end (if DD.MM.YYYY is your date format; you would need to adjust this):

    (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 === "Never" ) {
                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 === "Never" ) {
                    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 );
        };
    };
     
    }));
    
Sign In or Register to comment.