Date sorting problem with jQuery Tablesorter

前端 未结 7 1405
孤街浪徒
孤街浪徒 2020-11-29 03:21

I am trying to sort a table which has column like 2009-12-17 23:59:59.0. I am using below to apply sort

$(document).ready(function() { 
    $(\         


        
7条回答
  •  伪装坚强ぢ
    2020-11-29 03:30

    Just add another choice works perfectly for me to sort the date format (dd/MM/yyyy hh:mm:ss). By using the js dataTables plugin.

    Add the code below to your is code:

    $(document).ready(function () {
    oTable = $('#AjaxGrid').dataTable({
    "aLengthMenu": [[5, 10, 25, 50, 100, 500,1000,-1], [5, 10, 25, 50, 100,500,1000,"All"]],
    iDisplayLength: 1000,
    aaSorting: [[2, 'asc']],
    bSortable: true,
    aoColumnDefs: [
    { "aTargets": [ 1 ], "bSortable": true },
    { "aTargets": [ 2 ], "bSortable": true },
    { "aTargets": [ 3 ], "bSortable": true },
    { "aTargets": [ 4 ], "bSortable": true },
    {"aTargets": [ 5 ], "bSortable": true, "sType": "date-euro"},
    {"aTargets": [ 6 ], "bSortable": true, "sType": "date-euro"},
    { "aTargets": [ 7 ], "bSortable": false }
    ],
    "sDom": '<"H"Cfr>t<"F"ip>',
    "oLanguage": {
    "sZeroRecords": "- No Articles To Display -",
    "sLengthMenu": "Display _MENU_ records per page",
    "sInfo": " ", //"Displaying _START_ to _END_ of _TOTAL_ records",
    "sInfoEmpty": " ", //"Showing 0 to 0 of 0 records",
    "sInfoFiltered": "(filtered from _MAX_ total records)"
    },
    "bJQueryUI": true
    });
    });
    
    
    //New code
    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "date-euro-pre": function ( a ) {
    if ($.trim(a) != '') {
    var frDatea = $.trim(a).split(' ');
    var frTimea = frDatea[1].split(':');
    var frDatea2 = frDatea[0].split('/');
    var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
    } else {
    var x = 10000000000000; // = l'an 1000 ...
    }
    
    return x;
    },
    
    "date-euro-asc": function ( a, b ) {
    return a - b;
    },
    
    "date-euro-desc": function ( a, b ) {
    return b - a;
    }
    } );
    

提交回复
热议问题