I want to render the values from two different columns into my jquery datatable
.10.16/js/jquery.dataTables.min.js
.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
I want to render the values from two different columns into my jquery datatable
https://cdn.datatables/1.10.16/js/jquery.dataTables.min.js
https://cdn.datatables/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
Share
Improve this question
edited Apr 27, 2021 at 10:37
Zoe - Save the data dump
28.3k22 gold badges128 silver badges160 bronze badges
asked Oct 16, 2017 at 10:22
peace_lovepeace_love
6,47114 gold badges83 silver badges184 bronze badges
3
-
1
It should be
row[0]
notdata[0]
. – markpsmith Commented Oct 16, 2017 at 10:29 - @markpsmith But then my result is: The id is undefined and the name is undefined – peace_love Commented Oct 16, 2017 at 10:30
-
it should be
var result = 'The id is ' + row["id"] + ' and the name is ' + row["name"];
– juntapao Commented Oct 16, 2017 at 10:36
1 Answer
Reset to default 8Please 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]
;