Server side sorted data not showing in same order in Datatables

那年仲夏 提交于 2020-01-02 14:03:41

问题


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

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.)

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?


回答1:


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



来源:https://stackoverflow.com/questions/51075675/server-side-sorted-data-not-showing-in-same-order-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!