日期时间插件不会在数据表中排序 [英] Datetime plugin doesn't sort in Datatable

查看:25
本文介绍了日期时间插件不会在数据表中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得终极插件(.基本上,此参数允许您为不同类型的操作(排序、显示等)格式化列数据.在您的情况下,您需要执行以下操作:

$(function(){$.fn.dataTable.moment('dd/MM/yyyy HH:mm:ss');var table = $('#example').DataTable({ajax: {url: "query.php", dataType: "json", dataSrc: ''},列": [{"data": "eisodos",渲染":功能(数据,类型,完整){如果(类型=='显示')return moment(data).format('dd/MM/yyyy HH:mm:ss');别的return moment(data).format('yyyy/MM/dd HH:mm:ss');}}],语言":{网址":Greek.json"}});});

在前面的代码中,当操作用于显示时,日期将被格式化为西班牙语,而对于其他操作(如排序),将使用英语格式.您也可以参考我之前针对类似问题所做的回答:

对数字数据进行排序.格式化的表格值

I can't get the ultimate plugin (https://datatables.net/blog/2014-12-18) work for my site. I have a datetime column. The input for the column (called eisodos) from JSON is dd/MM/yyyy HH:mm:ss (e.g. 31/10/2018 10:03:00)

The field in SQL Server is Datetime. In query.php, I FORMAT it to dd/MM/yyyy HH:mm:ss and encode it to JSON.

If I FORMAT it to yyyy/MM/dd HH:mm:ss it sorts fine in the Datatable plugin, but I want it to be displayed like dd/MM/yyyy HH:mm:ss.

I have include both scripts (latest versions) :

<script src="bower_components/moment/min/moment.min.js"></script>

and

<script src="bower_components/moment/min/datetime-moment.js"></script>

I have checked everything.

The result I get when I sort is like this:

I have been searching two days and I can't find a solution.

Here is my code:

$(function ()
{
    $.fn.dataTable.moment('dd/MM/yyyy HH:mm:ss');

    var table = $('#example').DataTable(
    {
        ajax: {url: "query.php", dataType: "json", dataSrc: ''},
        "columns": [
            {"data": "eisodos"}
            // I have also tried the following (column render) but nothing changed.
            // "render": function(data, type, full) {return moment(data).format('dd/MM/yyyy HH:mm:ss');}   
        ],
        "language": {"url": "Greek.json"}
    });
});

解决方案

In your case, you will have to use the argument type of the render method for the column. For read further about this, refer to Columns Render. Basically, this arguments lets you format your column data for differents types of actions (sort, diplay, etc...). In your case, you will need to do something like this:

$(function ()
{
    $.fn.dataTable.moment('dd/MM/yyyy HH:mm:ss');

    var table = $('#example').DataTable(
    {    
        ajax: {url: "query.php", dataType: "json", dataSrc: ''},
        "columns": [
        {
            "data": "eisodos",
            "render": function(data, type, full)
            {
                if (type == 'display')
                    return moment(data).format('dd/MM/yyyy HH:mm:ss');
                else
                    return moment(data).format('yyyy/MM/dd HH:mm:ss');
            }
        }],
        "language": {"url": "Greek.json"}    
    });
});

On the previous code, date will be formated to the spanish way when the action is for display, and for others actions (like sorting) the english format will be used. You can also refer to this answer I have made before for a similar issue:

Sort numeric data.table values that are formatted

这篇关于日期时间插件不会在数据表中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆