问题
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.
- 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