Sort the Date Column in MM/DD/YYYY format

Sort the Date Column in MM/DD/YYYY format

beginner_2018beginner_2018 Posts: 46Questions: 19Answers: 0
edited March 2018 in Free community support

Dear All,

I had written the below code to sort the Data Column in the format of MM/DD/YYYY . But I find no luck with it.,
Can any one please help me how can I make it

  function sortDatePlugin(){
            $.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();

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

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Can you link to a test case showing the issue please?

    Allan

  • beginner_2018beginner_2018 Posts: 46Questions: 19Answers: 0

    Allan,

    Thanks for your kind hope . Please find the fiddle.

    https://jsfiddle.net/spspecalist87/heoo51pu/6/

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    A few issues there:

    1. Moment wasn't being loaded
    2. You weren't calling your sortDatePlugin() function so it was never loading
    3. The date format was wrong - it should be D/M/YYYY rather than DD/MM/YYYY since you don't include leading zeros for the day and month parts.
    4. I'd suggest that you call the $.fn.dataTable.moment() function on its own

    https://jsfiddle.net/heoo51pu/9/

    Allan

  • beginner_2018beginner_2018 Posts: 46Questions: 19Answers: 0
    edited April 2018

    Sir,

    It's working fine when I set the row limit to 10 or 50 rows

    But when I bind the entire data-set or on large amount of data-set to data-table , It is loosing the control to sort the order

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Take a look at the browser's console to see if you are getting any errors.

    Kevin

This discussion has been closed.