javascript - 如何将多列数据渲染到一列中?

标签 javascript jquery ajax datatables

我想将两个不同列的值渲染到我的 jquery 数据表中

https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js
https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css

$(document).ready(function(){

    var table = $('#table').DataTable({

         "ajax": {
                "url": "data.json",
                "dataSrc": "",
            },

         "columnDefs": [
            {
               "render": function (data, type, row) {
                   var result = 'The id is ' + data[0] + ' and the name is ' + data[1];
                        return result;
                    },
                    "targets": 0,
                },      

         ],

        "columns": [
                {
                    "data": "id"
                },
                {
                    "data": "name"
                }
            ]
    });

});

数据.json:

[{
    "id": "12",
    "name": "Laura"
}]

但我的结果是:

The id is 12 and the name is undefined

最佳答案

请更改以下内容:

"columnDefs": [
            {
               "render": function (data, type, row) {
                   var result = 'The id is ' + row["id"] + ' and the name is ' + row["name"];
                   console.log(result);
                        return result;
                    },
                    "targets": 0,
                },      

         ]

使用row["id"]代替data[0]

关于javascript - 如何将多列数据渲染到一列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768023/

相关文章:

javascript - 谷歌地图标记点击并拖动特定缩放级别

jquery - 附加的文本字段不会换行

jquery - 如何使用 jquery 在 ul 标签内删除和添加 li 元素

javascript - 两台服务器,同一个域名,取不到cookies

php - 跨域发布,无需访问服务器

jQuery 绑定(bind)和 getJSON 组合的行为不符合预期

javascript - 如何解决语法错误: Unexpected token = after importing a class?

javascript - ionic 与 jQuery

javascript - removeAttribute 删除与元素关联的所有其他类

javascript - 如何使用ajax隐藏弹出窗口