数据表按日期正确排序 [英] Datatable sort by date correctly
问题描述
我在网站上使用Django framework
,但是为了显示信息,我使用了插件Datatable
,所以一栏显示日期信息,用于显示日期的格式类似于May. 6, 2017
,所以当我进行排序时日期列将订单作为字符串而不是日期,因此要显示的第一个日期类似于August 10, 2017
是否可以使用该格式按日期排序?
I'm using Django framework
for my web site but to show info I use the plugin Datatable
so one column show date info, the format that is used to show the date is like May. 6, 2017
so when I sort the date column take the order as string and not date so the first date to show is like August 10, 2017
Is there a way to sort by date using that format?
推荐答案
在定义DataTable的列时,您需要指定render
回调,如下例所示:
When defining the columns of your DataTable, you need to specify the render
callback as in the example below:
columns: [
{
title: "Date",
data: "yourDateRef",
render: function(data, type, row) {
if (type == "display") {
return prettyFormat(data);
}
else {
return data;
}
}
},
...
基本上,DataTables会调用render回调来显示数据(type=="display"
),也可以在需要对数据进行排序(type=="sort"
)或过滤(type=="filter"
)时显示.
Basically, DataTables calls the render callback to display the data (type=="display"
), but also when the data needs to be sorted (type=="sort"
), or filtered (type=="filter"
).
这使您可以控制给定字段的显示方式,还可以对其进行排序和过滤.
This allows you to control how a given field is displayed, but also sorted and filtered.
更多信息: https://datatables.net/reference/option/columns.render
希望这会有所帮助!
这篇关于数据表按日期正确排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!