Bob Mwenda
Bob Mwenda

Reputation: 89

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>

Upvotes: 0

Views: 621

Answers (3)

EnthuOns
EnthuOns

Reputation: 34

I think you should try this :-

$(document).ready(function(){
    $('#employee-grid').DataTable({
        "order": [[ 1, "desc" ]]
    });
});     

For more info please go to this link :- https://datatables.net/examples/basic_init/table_sorting.html

Upvotes: 0

Asif Rahaman
Asif Rahaman

Reputation: 775

<script type="text/javascript" language="javascript" >
        $(document).ready(function() {
            var dataTable = $('#employee-grid').DataTable( {
                "processing": true,
                "serverSide": true,
                "order": [[ 0, "desc" ]],    //add this line
                "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>

N.B: Assuming your first column of table is some kind of auto incremented id

Upvotes: 1

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

Upvotes: 0

Related Questions