Sort date on grid chronologically?

Asked

Viewed 79 times

0

 $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var min  = $('#min-date').val() ? $('#min-date').val().split('/') : null;
        var max  = $('#max-date').val() ? $('#max-date').val().split('/') : null;

        if(max && min){
          max = moment(max[2] + '-' + max[1] + '-' + max[0])

          min = moment(min[2] + '-' + min[1] + '-' + min[0])
        }

        var createdAt = data[4].substring(0,10); // Our date column in the table
        createdAt = createdAt.split('/')
        dataCriacao = createdAt[2] + '-' + createdAt[1] + '-' + createdAt[0] 

        if( (!max || !min) || moment(dataCriacao).isSameOrBefore(max) && moment(dataCriacao).isSameOrAfter(min)) {
          return true
        }

      return false

    }
);

1 answer

0


I made an example of how your rule should be with another lib. [dayjs] - Follow the link if you want to check it.

const dayjs = require('dayjs');

let x = [
    { data: dayjs(new Date('01-01-2018')).format('DD-MM-YYYY') } , 
    { data: dayjs(new Date('07-06-1996')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-03-1981')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-02-2005')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-05-2003')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-02-2001')).format('DD-MM-YYYY') }
];

var global;

function sortData(){
    global = x.sort( (a,b) => {
        if (dayjs(a.data).isBefore(dayjs(b.data)))
            return -1 
        else 
            return 1
    }) 


    console.log(global);
}

sortData();

However what matters to you is the "Sort" that will sort for you according to the callback function passed.

  • 1

    Thanks a lot bro worked out !!!

  • For nothing! Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.