服务器端排序的数据在数据表中未按相同顺序显示 [英] Server side sorted data not showing in same order in Datatables

查看:155
本文介绍了服务器端排序的数据在数据表中未按相同顺序显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据表来显示数据库中的数据.我正在使用CodeIgniter进行服务器端脚本编写.这是表格图片

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.

我已检查CodeIgniter选择查询以进行调试,即

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

$this->db->get_compiled_select()

它正在显示

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

这里 tmp 是我的数据库名称,并且运行良好.

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

这是我从数据库中获得的json响应-(由于文本太长,我无法向您显示整个JSON响应.这就是为什么我使用一些在线json解析站点来汇总我的数据的原因.)

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

我的json响应显示按查询顺序对数据库的数据,即按降序排列的国家/地区列.这就是第一个对象显示津巴布韦" 的原因.但是在数据表中,它显示的是其他数据,即"Andora" .

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

这是我的JavaScript-

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>

如果我在datatables参数中设置了"order":[[6,"desc"]] ,那么它将在得到json响应后进行排序.我不想添加客户端脚本来订购一列.我只想在数据表中使用排序的json响应.有人可以帮我吗?

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?

推荐答案

一个简单的解决方案是,只需为order选项指定空的[].

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

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

在初始化期间,DataTable不应用任何顺序.按DataTables读取行的顺序显示行(即,如果是DOM源,则是来自DOM的原始顺序;如果是Ajax/数据,则是数据的数组):

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

来源:> 数据表:订单

这篇关于服务器端排序的数据在数据表中未按相同顺序显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆