Ashis Biswas
Ashis Biswas

Reputation: 767

Server side sorted data not showing in same order in Datatables

I am using Datatables to show data from a database. I am using CodeIgniter for serverside scripting. Here is the table image

enter image description here

In my serverside coding, I query to my database order by Country and in descending order.

I have checked CodeIgniter select query for debugging purpose i.e

$this->db->get_compiled_select()

and it is showing

'SELECT *
FROM `tmp`
ORDER BY `country` DESC'

here tmp is my database name, and it's performing well.

here is my json response from the database- (I can't show you the whole JSON response because of long text. That's why I use some online json parsing site to summarize my data.)

enter image description here

My json response showing data as per query to database i.e country column in desc order. That's why the first object is showing "Zimbabwe". But in datatables it is showing other data i.e "Andora".

Here is my javascript -

    <script type="text/javascript">
        $(document).ready( function () {
            $('#myTable').DataTable( {
                "responsive": true,
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url":  "http://localhost/adminDemo/admin/test",
                    "type": "POST"
                },
                "columnDefs" : [
                    {"width" : "20%", "targets" : 7},
                    {"width" : "30%", "targets" : 5},
                    { "orderable": false, "targets": 7 } //Don't order the action column
                ]
            } );
        } );
    </script>

If i set "order": [[ 6, "desc" ]] in datatables parameter, then it will be sorted after getting the json response. I don't want to add client-side script to order a column. I just want to use the sorted json response in datatables. Can anyone help me with this?

Upvotes: 2

Views: 7019

Answers (1)

mmushtaq
mmushtaq

Reputation: 3520

A simple solution to that problem is, just specify empty [] for order option.

$('#example').DataTable( {
    "order": []
} );

No ordering applied by DataTables during initialization. The rows are shown in the order they are read by DataTables (i.e. the original order from the DOM if DOM sourced, or the array of data if Ajax / data sourced):

Source: DataTable:Order

Upvotes: 5

Related Questions