json - 服务器端排序的数据在数据表中未按相同顺序显示

标签 json codeigniter datatables

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

enter image description here

在我的服务器端编码中,我按国家/地区并按降序查询数据库顺序。

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

$this->db->get_compiled_select()

它正在显示

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

这里tmp是我的数据库名称,它运行良好。

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

enter image description here

我的 json 响应显示按照数据库查询的数据,即按降序排列的国家/地区列。这就是为什么第一个对象显示“津巴布韦”。但在数据表中,它显示其他数据,即“Andora”

这是我的 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 响应。谁能帮我解决这个问题吗?

最佳答案

该问题的一个简单解决方案是,只需为 order 选项指定空的 [] 即可。

$('#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):

来源:DataTable:Order

关于json - 服务器端排序的数据在数据表中未按相同顺序显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075675/

相关文章:

json - couchdb - 导入 json 文件

search - Powerapps:将搜索文本拆分为单个字符串,现在我只需要在图库中显示包含其中一个字符串的项目

php - Codeigniter 中的分页 ajax,从数据库加载数据 otomatis

javascript 仅带回文本的特定部分(codeigniter 和 JQuery)

javascript - JavaScript 的 Fetch 可以用来更新 Rails Controller 中的参数值吗?

jquery - 如何在现有数据表中添加新行作为 html

javascript - 有什么方法可以评论或更好地组织 bower.json 文件吗?

javascript - 点击后如何在 AngularJS 中一一循环使用 JSON

c# - 序列化为 JSON 时出现 OutOfMemoryException?

php - 在 View 内的函数中访问 Codeigniter 数据变量