本文介绍了为什么日期不能在Datatables中完美排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多方法来对dataTables中的日期进行排序,但是工作不完美(asc或desc).我提到了找到解决方案但没有解决的地方.

I have try many ways to sort date in dataTables but not working perfectly (asc or desc).I referred to any place to find the solution but not solve.

下面是我尝试但无法使用的方法.

Below are the method that I have tried but not working.

  1. 使用 Date Eu 插件.此处1 ,,此处3
  1. Use Date Eu plugin. Here 1 , Here 2 , Here 3

JS

function loadtable(){

    var project = '';

    $.ajax({
        url : url,
        crossDomain: true,
        type : 'POST',
        dataType : 'json',
        data: JSON.stringify({project_id: project_id}),
        success: function(response){
            if (response.status == "Success"){
                $.each(response.data, function(key, value){
                    "<div class='table-responsive'>"+
                        "<table id='' class='table Layer1Table' style='width:100%'>"+
                            "<thead>"+
                                "<tr>"+
                                    "<th>Activity Name</th>"+
                                    "<th>Plan Start Date</th>"+
                                    "<th>Plan Finish Date</th>"+
                                    "<th>Actual Start Date</th>"+
                                    "<th>Actual Finish Date</th>"+
                                "</tr>"+
                            "</thead>";
                            $.each(value.l3_task, function(key, value1){
                                project += 
                                "<tr>"+
                                    "<td>"+value1.task_name+"</td>"+
                                    "<td>"+value1.task_planned_start_date+"</td>"+
                                    "<td>"+value1.task_planned_end_date+"</td>"+
                                    "<td>"+value1.task_start_date+"</td>"+
                                    "<td>"+value1.task_end_date+"</td>"+
                                "</tr>";
                            }); //end of each
                            project +=
                        "</table>"+
                    "</div>";
                }); //end of each
                $("#projectDetail").append(project);
                $('table.Layer1Table').DataTable({
                    processing: true,
                    dom: 'Bfltip',
                    buttons: [
                        { extend: 'excelHtml5' }
                    ],
                    destroy: true,
                    ordering: true,
                    columnDefs: [
                        { type: 'date-eu', targets: 1 }  //example target for planned start date
                    ],
                });
            }
            else {}
        },
        error: function(e){}
    });
}

推荐答案

请注意,您使用日期作为 string ,因此它将把它作为普通字符串进行排序.

notice that you are using date as string so it will sort this as normal string.

对于您的问题,您可以参考以下 https://datatables.net/plug-ins/sorting/date-uk

for your problem you can refer this https://datatables.net/plug-ins/sorting/date-uk

谢谢!编码愉快!

这篇关于为什么日期不能在Datatables中完美排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:26