Bob Mwenda Bob Mwenda - 5 months ago 8
jQuery Question

JQuery datatables ordering by Desc oder

I'm using JQuery datatable for my table however I want the last entry to appear first as in the table to appear in a descending order. Tried changing the PHP MYSQL select statement but not working. I thought, could this be the code to arrange as to my needs? I f so, any one lead me how to oder the same? Thank you...



<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get

error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");

}
}
} );
} );

</script>

Answer

You need to specify the default order column, like this:

var dataTable = $('#employee-grid').DataTable( {
        "order": [[ 1, "desc" ]],
 "processing": true,
                "serverSide": true,
        "ajax":{
            url :"employee-grid-data.php", // json datasource
            type: "post",  // method  , by default get

            error: function(){  // error handling
                $(".employee-grid-error").html("");
                $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                $("#employee-grid_processing").css("display","none");

            }
        }
    } );

Check the Datatables docs for more information: https://datatables.net/examples/basic_init/table_sorting.html

Comments