我在排序DateTime
的{{1}}字段时遇到问题,我通过服务器端从DataTable
表返回记录。问题是,当需要单击类型为customers
的列的标签时,数据的排序方式就像它是字符串而不是DateTime
一样。我需要巴西格式:DD / MM / YYYY
的index.php
DateTime
服务器side.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Customers</title>
</head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<body>
<h2>Customers</h2>
<table id="server-side" class="table table-striped table-bordered table-hover" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>REGISTER</th>
<th>NAME</th>
<th>LEVEL</th>
<th>OPTIONS</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function(e){
$('#server-side').dataTable({
"bProcessing": true,
"serverSide": true,
"aoColumnDefs": [
{
"bSearchable": false,
"bVisible": false,
"aTargets": [0]
},
{
"aTargets": [4],
"mRender": function ( data, type, full ) {
return '<a href="view.php?id=' + full[0] + '" class="btn btn-sm btn-success"><i class="fa fa-eye"></i> VIEW</a> '+
'<a href="edit.php?id=' + full[0] + '" class="btn btn-sm btn-success"><i class="fa fa-pencil"></i> EDIT</a> '+
'<a href="#" class="btn btn-sm btn-success" data-toggle="modal" data-target="#delete-modal" data-customer="' + full[0] + '"><i class="fa fa-trash"></i> DELETE</a>';
}
}
],
language: {
processing: "processing...",
},
"ajax":{
url :"server-side.php",
type: "POST",
error: function(){
$("#post_list_processing").css("display","none");
}
},
"order": [ 1, "desc"],
});
});
</script>
</body>
</html>
答案 0 :(得分:0)
您可以使用PHP格式化日期,并使用初始日期格式与MySQL进行排序。
// Remove format here,
$sql_query = "SELECT id,register,name,level FROM customers";
然后,在while
循环中格式化您的日期:
while( $row = mysqli_fetch_row($queryRecords) ) {
$row['register'] = date('d/m/Y', strtotime($row['register']));
$data[] = $row;
}