2

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?

5
  • 1
    just set orderable to false otherwise data will be shown as per default ordering defined by datatable itself Commented Jun 28, 2018 at 6:11
  • If I set orderable to false then I can't order the column after get the json response. Commented Jun 28, 2018 at 6:12
  • so its better to datatable ordering instead of server side ordering Commented Jun 28, 2018 at 6:16
  • That's why I am asking is there any way to the json response as it is to datatables? If I set "ordering": false then it is showing as per json response but the problem is I can't sort manually. Hope you understand bro. Commented Jun 28, 2018 at 6:20
  • may be ordering to each individual columns helps you , see this : datatables.net/examples/basic_init/multi_col_sort.html Commented Jun 28, 2018 at 6:25

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes by default datatable takes the first column to sort whole data. By this way, initially, we can show the data based on json order. Thanks for your info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.