gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:40:21 24 4
gpt4 key购买 nike

我想将两个不同列的值渲染到我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com