【问题标题】:How to fix sorting issue date in datatables column?如何修复数据表列中的排序问题日期?
【发布时间】:2020-04-09 14:34:37
【问题描述】:

我有包含多列日期的数据表。但是在按 asc 和 desc 排序时,所有列的日期都不能正常工作。只是为了通知我所有的日期都是 dd-mm-yyyy 格式。我正在使用一个函数将其设为 dd-mm-yyyy。

我试图申请this 参考但没有帮助。不知道自己申请的方式对不对,请指正。

HTML

<table id="projectListTable">
    <thead>
        <tr>
            <th>Project Name</th>
            <th>Plan Start</th>
            <th>Plan Finish</th>
        </tr>
    </thead>
</table>

JS

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "extract-date-pre": function(data) {
        if (data == null){
            return data;
        }
        else {
            new_data = data.split("T");
            new_data[0] = displayDate(new_data[0]);
            return new_data[0];
        }
    },
    "extract-date-asc": function(a, b) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "extract-date-desc": function(a, b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});    

$('#projectListTable').DataTable({
    columns: [           
        { data : "project_name" },
        { data : "project_planned_start",        
            render: function(data){
                if (data == null){
                    return data;
                }
                else {
                    new_data = data.split("T");
                    new_data[0] = displayDate(new_data[0]);
                    return new_data[0];
                }
            }
        },
        { data : "project_planned_end",
            render: function(data){
                if (data == null){
                    return data;
                }
                else {
                    new_data = data.split("T");
                    new_data[0] = displayDate(new_data[0]);
                    return new_data[0];
                }
            }
        }     
    ],
    columnDefs: [
        {
            type: 'extract-date',
            targets: [1]
        },
        {
            type: 'extract-date',
            targets: [2]
        }
    ]
});

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    如果您的实际日期数据采用 ISO 8601 格式。

    示例:2019-03-12T04:08:08.069Z

    您可以在每一列的render函数中使用。

    {
        data: "project_planned_start",
        render: function(data, type) {
            if (type === 'sort') { // ADD this for sorting
                return data;
            }
            if (data == null) {
                return data;
            } else {
                new_data = data.split("T");
                new_data[0] = displayDate(new_data[0]);
                return new_data[0];
            }
        }
    },
    

    【讨论】:

    • 这个jQuery.extend... 是否仍然需要?
    • columnDefs 是否还需要?
    • 如果您的数据采用 ISO 8601 格式,则不需要它们。也适用于列定义。您可以像这样指定{ type: 'extract-date', targets: [1,2] },
    • 是的,我使用的是 ISO 8601 格式。你的答案对我有用。谢谢。会接受你的回答。
    • 例如:== "1" == 1 -> true,字符串和数字。 === 是严格的。
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 2016-06-14
    • 2012-08-13
    • 2013-05-16
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多