如何将来自多个列的数据呈现到一个列中?

时间:2020-12-20 20:12:45

I want to render the values from two different columns into my jquery datatable

我想将来自两个不同列的值呈现到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:

data.json:

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

But my result is:

但是我的结果是:

The id is 12 and the name is undefined

1 个解决方案

#1


3  

Please change the following:

请更改以下:

"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,
                },      

         ]

Use row["id"] instead of data[0];

使用row["id"]而不是数据[0];

#1


3  

Please change the following:

请更改以下:

"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,
                },      

         ]

Use row["id"] instead of data[0];

使用row["id"]而不是数据[0];