Jquery Datatable插件未按预期工作
因此,我有一个datatables页面,需要按日期排序,格式如下:HH:mm:ss DD/mm/YYYY 我正在使用Datatables矩js插件。我试过这个: ,以及这个: 问题是,这个插件似乎只关注HH:mm:ss部分,而忽略了日期部分,甚至似乎在向后排序 我最后做的一个实验是运行这个稍微修改过的版本并做笔记:Jquery Datatable插件未按预期工作,jquery,datatables,momentjs,Jquery,Datatables,Momentjs,因此,我有一个datatables页面,需要按日期排序,格式如下:HH:mm:ss DD/mm/YYYY 我正在使用Datatables矩js插件。我试过这个: ,以及这个: 问题是,这个插件似乎只关注HH:mm:ss部分,而忽略了日期部分,甚至似乎在向后排序 我最后做的一个实验是运行这个稍微修改过的版本并做笔记: $.fn.dataTable.moment = function ( format, locale ) { console.log('moment function.');
$.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);
};
};
正如您所看到的,结果完全有效。不过,问题是,这些是在我重新排序之前才打印出来的。这意味着,本质上,当我看到这些,那些日期时间现在在我认为值得一提的最后一页,这对我来说似乎很奇怪
我也试过:
columnDefs: [{
targets: [8], //index of column
type: 'date'
}]
和“datetime时刻”一样,因为我在某个地方读过。没什么区别
除此排序插件外,还使用了以下软件:
拉威尔5.2
Yajra数据表插件v6.29.0
数据表1.10.16
力矩js 2.24.0
数据是通过ajax提供的。这可能是个问题吗
提前谢谢你的帮助。我已经知道发生了什么事 似乎是Yajra datatables插件实际在进行排序 问题是:在我的登录尝试模型中,我有以下服务器端日期格式化方法:
public function getAttemptedAtAttribute($value) {
return toDMY2($value);
}
toDMY2是一个使用Carbon库返回欧洲格式日期的函数
问题是,Yajra datatables插件似乎无法将其识别为日期
因此,我的解决方案如下:我完全删除了服务器端的格式设置,而是依赖MomentJS来设置列的格式,如下所示:
"columns": [
[...]
{ "data": "attempted_at", "render": function(data, type, row) {
return moment(data).format('HH:mm:ss DD/MM/YYYY');
}, "orderable": true},
],
你能不能把你的例子作为一个代码片段或一把小提琴来添加?您也可以使用或使用数据创建模拟json。有人会对你的问题给出更好的答案或回答。
"columns": [
[...]
{ "data": "attempted_at", "render": function(data, type, row) {
return moment(data).format('HH:mm:ss DD/MM/YYYY');
}, "orderable": true},
],