Datatable Moment插件无法按预期工作

时间:2019-07-19 11:09:49

标签: jquery datatables momentjs

因此,我有一个数据表页面,需要按以下格式对日期进行排序:HH:mm:ss DD / MM / YYYY。

我正在使用Datatables moment js插件。我已经尝试过这个:https://datatables.net/blog/2014-12-18#Completed-plug-in ,以及这个:https://datatables.net/plug-ins/sorting/datetime-moment

问题是,该插件似乎只关注HH:mm:ss部分,而忽略日期部分,甚至似乎倒退了。

我做的最后一个实验是运行这个经过稍微修改的版本并做笔记:

$.fn.dataTable.moment = function ( format, locale ) {
    console.log('moment function.');

    var types = $.fn.dataTable.ext.type;

    // Add type detection
    types.detect.unshift(function (d) {
        if (d) {
            // Strip HTML tags and newline characters if possible
            if ( d.replace ) {
                d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
            }

            // Strip out surrounding white space
            d = $.trim( d );
        }

        // Null and empty values are acceptable
        if (d === '' || d === null) {
            return 'moment-' + format;
        }

        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) {
        if (d) {
            // Strip HTML tags and newline characters if possible
            if ( d.replace ) {
                d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
            }

            // Strip out surrounding white space
            d = $.trim( d );
        }

        console.log('unformatted: ' + d);
        console.log('moment:');
        console.log(moment(d, format, locale, true));
        console.log('format x: ' + parseInt(moment(d, format, locale, true).format('x'), 10));

        return !moment(d, format, locale, true).isValid() ?
            Infinity :
            parseInt(moment(d, format, locale, true).format('x'), 10);
    };
};

结果如下:

unformatted: 19:13:28 16/07/2019 login_attempts:63:21
moment: login_attempts:64:21
Object { _isAMomentObject: true, _i: "19:13:28 16/07/2019", _f: "HH:mm:ss DD/MM/YYYY", _strict: true, _isUTC: false, _pf: {…}, _locale: {…}, _d: Date Tue Jul 16 2019 19:13:28 GMT+0200 (Central European Summer Time), _isValid: true }
login_attempts:65:21
format x: 1563297208000 login_attempts:66:21
unformatted: 19:13:27 16/07/2019 login_attempts:63:21
moment: login_attempts:64:21
Object { _isAMomentObject: true, _i: "19:13:27 16/07/2019", _f: "HH:mm:ss DD/MM/YYYY", _strict: true, _isUTC: false, _pf: {…}, _locale: {…}, _d: Date Tue Jul 16 2019 19:13:27 GMT+0200 (Central European Summer Time), _isValid: true }
login_attempts:65:21
format x: 1563297207000 login_attempts:66:21
unformatted: 19:13:26 16/07/2019 login_attempts:63:21
moment: login_attempts:64:21
Object { _isAMomentObject: true, _i: "19:13:26 16/07/2019", _f: "HH:mm:ss DD/MM/YYYY", _strict: true, _isUTC: false, _pf: {…}, _locale: {…}, _d: Date Tue Jul 16 2019 19:13:26 GMT+0200 (Central European Summer Time), _isValid: true }
login_attempts:65:21
format x: 1563297206000

如您所见,完全有效的结果。不过,只有在我重新排序之前才打印这些陷阱。从本质上讲,这意味着当我看到这些日期时,这些日期时间现在对我来说是最后一页(这是值得一提的,对我来说似乎很奇怪。)

我也尝试过这个:

 columnDefs: [{
      targets: [8], //index of column
      type: 'date'
    }]

相同,但带有'datetime-moment',因为我在某处阅读过。没什么。

除此排序插件外使用的软件是:

  • laravel 5.2
  • Yajra Datatables插件v6.29.0
  • 数据表1.10.16

  • 瞬间js 2.24.0

数据通过ajax提供。这可能是个问题吗?

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我已经知道发生了什么事。

似乎是Yajra datatables插件实际上在进行排序。

问题是:在我的登录尝试模型中,我具有以下服务器端日期格式设置方法:

public function getAttemptedAtAttribute($value) {
    return toDMY2($value);
}

toDMY2是使用Carbon库返回欧洲格式日期的函数。

问题是,Yajra数据表插件似乎无法将其识别为日期。

因此,我的解决方案如下:我完全删除了服务器端的格式化,而是依靠moment js格式化列,如下所示:

"columns": [

      [...]

      { "data": "attempted_at", "render": function(data, type, row) {
        return moment(data).format('HH:mm:ss DD/MM/YYYY');
      }, "orderable": true},
],
相关问题