【问题标题】:How can I render data from multiple columns into one column?如何将多列中的数据呈现为一列?
【发布时间】:2017-10-16 10:22:40
【问题描述】:

我想将两个不同列的值呈现到我的 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"
                }
            ]
    });

});

data.json:

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

但我的结果是:

The id is 12 and the name is undefined

【问题讨论】:

  • 应该是row[0] 而不是data[0]
  • @markpsmith 但是我的结果是:id 未定义,名称未定义
  • 应该是var result = 'The id is ' + row["id"] + ' and the name is ' + row["name"];

标签: javascript jquery ajax datatables


【解决方案1】:

请更改以下内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2015-06-24
    • 2021-03-27
    • 2021-12-27
    相关资源
    最近更新 更多