gpt4 book ai didi

javascript - 显示 DataTables 中嵌套对象的集合

转载 作者:行者123 更新时间:2023-11-30 12:24:48 25 4
gpt4 key购买 nike

[
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "/Date(1429653550233)/",
"IssueList": null
},
{
"name": "Tiger Nixon 1",
"position": "System Architect 1",
"salary": "$420,800",
"start_date": "2011/04/25",
"IssueList": [
{
"Number": 1,
"IssueDate": "/Date(1429653550233)/",
"Issue": "Lots of Problems"
},
{
"Number": 2,
"IssueDate": "/Date(1429185060000)/",
"Issue": "Lots of Problems here too"
}
]
},
{
"name": "Tiger Nixon 2",
"position": "System Architect 2",
"salary": "$520,800",
"start_date": "2011/04/25",
"IssueList": [
{
"Number": 3,
"IssueDate": "/Date(1429653550233)/",
"Issue": "Lots of Problems"
},
{
"Number": 4,
"IssueDate": "/Date(1429185060000)/",
"Issue": "Lots of Problems here too"
}
]
},
{
"name": "Tiger Nixon 3",
"position": "System Architect 3",
"salary": "$620,800",
"start_date": "2011/04/25",
"IssueList": null
}

]

我想在“DataTables”中显示上面的 JSON。当用户点击一行时,IssueList 中的嵌套对象应显示为主表内的子表。

如何在“DataTables”中做到这一点?我对“数据表”和 JavaScript 还很陌生,非常感谢您的帮助。

最佳答案

我不是 100% 确定我会以这种方式使用数据表。我做了类似的事情,但我没有在单元格中添加表格,而是添加了定义列表......我认为否则你会冒着让表格变得很忙的风险。

但如果您确信这是您想要的方式,我会查看列上的渲染调用并给出一个创建表的函数...或 dl...

$("#myTable").DataTable({
"data": data,
"columns": [
{
"title": "Name",
"data": "name"
}, {
"title": "Position",
"data": "position"
}, {
"title": "Salary",
"data": "salary"
}, {
"title": "Start Date",
"data": "start_date"
}, {
"title": "Issue List",
"data": "IssueList",
"render": function(d){
if(d !== null){
var table = "<table>";
$.each(d, function(k, v){
table += "<tr><td>" + v.Issue + "</td><td>" + v.IssueDate + "</td><td>" + v.Number + "</td></tr>";
});
return table + "</table>";
}else{
return "";
}
}
}
]
});

Working Example

关于javascript - 显示 DataTables 中嵌套对象的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784959/

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