peace_love
peace_love

Reputation: 6461

How can I render data from multiple columns into one column?

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

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

But my result is:

The id is 12 and the name is undefined

Upvotes: 2

Views: 7364

Answers (1)

juntapao
juntapao

Reputation: 427

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

Upvotes: 8

Related Questions