How to sort by Date with DataTables jquery plugin?

后端 未结 13 2661
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 11:02

I am using the datatables jquery plugin and want to sorty by dates.

I know they got a plugin but I can\'t find where to actually download it from

http://data

13条回答
  •  没有蜡笔的小新
    2020-12-02 11:33

    About update#1, there are 2 problems :

    • Number of days = 1 (d/MM/YYYY) instead of (dd/MM/YYYY)
    • Empty date

    here is the solution to avoid these problems :

    jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function (a, b) {
                var ukDatea = a.split('/');
                var ukDateb = b.split('/');
    
                //Date empty
                 if (ukDatea[0] == "" || ukDateb[0] == "") return 1;
    
                //need to change Date (d/MM/YYYY) into Date (dd/MM/YYYY) 
                if(ukDatea[0]<10) ukDatea[0] = "0" + ukDatea[0]; 
                if(ukDateb[0]<10) ukDateb[0] = "0" + ukDateb[0];
    
                var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
                var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
    
                return ((x < y) ? -1 : ((x > y) ? 1 : 0));
            };
    
            //Sorting by Date 
            jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function (a, b) {
                var ukDatea = a.split('/');
                var ukDateb = b.split('/');
    
                 //Date empty
                 if (ukDatea[0] == "" || ukDateb[0] == "") return 1;
    
                //MANDATORY to change Date (d/MM/YYYY) into Date (dd/MM/YYYY) 
                if(ukDatea[0]<10) ukDatea[0] = "0" + ukDatea[0]; 
                if(ukDateb[0]<10) ukDateb[0] = "0" + ukDateb[0];
    
                var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
                var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
    
                return ((x < y) ? 1 : ((x > y) ? -1 : 0));
            };
    

提交回复
热议问题