对DataTable

时间:2018-05-15 11:25:25

标签: php datetime datatable

我在排序DateTime的{​​{1}}字段时遇到问题,我通过服务器端从DataTable表返回记录。问题是,当需要单击类型为customers的列的标签时,数据的排序方式就像它是字符串而不是DateTime一样。我需要巴西格式:DD / MM / YYYY

enter image description here

的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>

1 个答案:

答案 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;
}