Why date is not sorting perfectly in Datatables?

六月ゝ 毕业季﹏ 提交于 2021-02-08 11:15:50

问题


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. 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){}
    });
}

回答1:


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

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

thank you! Happy coding!



来源:https://stackoverflow.com/questions/61242819/why-date-is-not-sorting-perfectly-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!