简体   繁体   English

数据表按日期正确排序

[英]Datatable sort by date correctly

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 我在我的网站上使用Django framework ,但为了显示信息,我使用了插件Datatable因此一栏显示了日期信息,用于显示日期的格式类似于May. 6, 2017 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? May. 6, 2017因此当我对日期列进行排序时,将订单作为字符串而不是日期,因此要显示的第一个日期类似于August 10, 2017 是否可以使用该格式按日期排序?

When defining the columns of your DataTable, you need to specify the render callback as in the example below: 在定义DataTable的列时,您需要指定render回调,如下例所示:

        columns: [
            {
                title: "Date",
                data: "yourDateRef",
                render: function(data, type, row) {
                    if (type == "display") {
                        return prettyFormat(data);
                    }
                    else {
                        return data;
                    }
                }
            },
            ...

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" ). 基本上,DataTables调用render回调来显示数据( type=="display" ),也可以在需要对数据进行排序( type=="sort" )或过滤( type=="filter" )时显示。

This allows you to control how a given field is displayed, but also sorted and filtered. 这使您可以控制给定字段的显示方式,还可以对其进行排序和过滤。

More information: https://datatables.net/reference/option/columns.render 更多信息: https : //datatables.net/reference/option/columns.render

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM