【问题标题】:Jquery datatable sorting not working with Date columnJquery数据表排序不适用于日期列
【发布时间】:2015-02-06 01:33:28
【问题描述】:

我正在使用 Datatable 插件。我想按日期列降序排序,但它不适用于 merender。我也想使用mRender 来格式化日期。对于其他列排序工作正常

查看演示:http://jsfiddle.net/wwhy5d2e/

这里是js代码:

$table = $('#KiList').dataTable({
    bAutoWidth: false,
    aoColumns: [{
        'sWidth': '35%'
    }, {
        'sWidth': '20%',
        "sType": "datetime-us"
    }, {
        'sWidth': '25%',
        "sType": 'string'
    }, {
        'sWidth': '10%',
        "sType": 'string'
    }, {
        'sWidth': '10%',
        "sType": 'string'
    }],
    "aoColumnDefs": [{
        "aTargets": [1],
        "mRender": function(date, type, full) {
            return moment(date).format('MM-DD-YYYY hh:mm a');
        }
    }],
    aaSorting: [[1, 'desc']]
});

【问题讨论】:

    标签: jquery datatable


    【解决方案1】:

    我得到了这个工作:Working Demo

    参考:https://gist.github.com/mark47/10427687

    $table = $('#KiList').dataTable({
        bAutoWidth: false,
        aoColumns: [{
            'sWidth': '35%'
        }, {
            'sWidth': '20%',
            "type": "datetime-us"//changed
        }, {
            'sWidth': '25%',
            "sType": 'string'
        }, {
            'sWidth': '10%',
            "sType": 'string'
        }, {
            'sWidth': '10%',
            "sType": 'string'
        }],
        "aoColumnDefs": [{
            "aTargets": [1],
            "mRender": function(date, type, full) {
                return moment(date).format('MM-DD-YYYY hh:mm a');
            }
        }],
        aaSorting: [[1, 'desc']]
    });
    

    添加:

    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
            "datetime-us": function ( a ) {
            // If there's no slash, then it's not an actual date, so return zero for sorting
            if(a.indexOf('/') === -1) {
                return '0';
            } else {
                    // Set optional items to zero
                    var hour = 0,
                        min = 0,
                        ap = 0;
                        // Execute match. Requires month, day, year. Can be mm/dd or m/d. Can be yy or yyyy
                        // Time is optional. am/pm is optional
                        // TODO - remove extra time column from array
                    var b = a.match(/(\d{1,2})\/(\d{1,2})\/(\d{2,4})( (\d{1,2}):(\d{1,2}))? ?(am|pm|AM|PM|Am|Pm)?/),
                        month = b[1],
                        day = b[2],
                        year = b[3];
                    // If time exists then output hours and minutes
                    if (b[4] != undefined) {
                        hour = b[5];
                        min = b[6];
                    }
                    // if using am/pm then change the hour to 24 hour format for sorting
                    if (b[7] != undefined) {
                        ap = b[7];
                        if(hour == '12') hour = '0';
                        if(ap == 'pm') hour = parseInt(hour, 10)+12;
                    }
    
                    // for 2 digit years, changes to 20__ if less than 70
                    if(year.length == 2){
                        if(parseInt(year, 10) < 70) year = '20'+year;
                        else year = '19'+year;
                    }
                    // Converts single digits
                    if(month.length == 1) month = '0'+month;
                    if(day.length == 1) day = '0'+day;
                    if(hour.length == 1) hour = '0'+hour;
                    if(min.length == 1) min = '0'+min;
                    var tt = year+month+day+hour+min;
    
                    return tt;
                }
            },
            "datetime-us-asc": function ( a, b ) {
            return a - b;
            },
            "datetime-us-desc": function ( a, b ) {
            return b - a;
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 1970-01-01
      • 2016-12-11
      • 2015-03-03
      • 2013-10-13
      • 2019-11-12
      • 2014-11-17
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多