javascript - 将多级 json 数组追加到 html 表 javascipt
问题描述
我有 url json 数据多级数组,将其附加到 html 表中,我如何将数组的第一级附加到 html 表中?
我的代码仅适用于一级和二级数据
id:457 - 第一级,
ids: 460, 458 - 第二级,
id: 743 - 第三级
如何将 id 为 743 的数组附加到我的 html 表中?
我只是尝试使用但它不起作用
data = [
{
"id": 457,
"name": "name1",
"code": "url1",
"show_code": "url1",
"icon": null,
"image": null,
"productsCount": 1,
"items": [
{
"id": 460,
"name": "name2",
"code": "url2",
"show_code": "url2",
"icon": null,
"image": null,
"productsCount": 1
},
{
"id": 458,
"name": "name3",
"code": "url3",
"show_code": "url3",
"icon": null,
"image": null,
"productsCount": 1,
"items": [
{
"id": 743,
"name": "name4",
"code": "url4",
"show_code": "url4",
"icon": null,
"image": null,
"productsCount": 1
}
]
}
]
}
]
$(document).ready(function(){
var product_data = '';
$.each(data, function (i, data) {
$.each(data.items, function (i, items) {
$.each(items.items, function (i, item) {
product_data += '<tr>';
product_data += '<td class="nav-submenu css-n9ebcy-Item"> <a class="code-parent" target="_blank" href="test' + data.code + '">' + data.name + '(' + data.id + ')' + '</a></td>';
product_data += '<td class="child-item"><a class="code-child" target="_blank" href="test' +
items.code + '/' + items.show_code + '">' + items.name + ' ' + '(' + items
.productsCount +
')' + '</a></td>';
for(var i=0; i<items.items.length;i++){
product_data += '<td>' + items.items[i].name + '</td>';
}
// product_data += '<td class="child-item"><a class="code-child" target="_blank" href="https://test' +
// items.items.code + '</a></td>';
product_data += '</tr>';
});
});
});
$('#showData').append(product_data);
});
th, td, p, input {
font:14px Verdana;
}
table, th, td
{
border: solid 1px #DDD;
border-collapse: collapse;
padding: 2px 3px;
text-align: center;
}
th {
font-weight:bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="showData" class="tavle">
<tr>
<th>level 1</th>
<th>level 2</th>
<th>level 3</th>
</tr>
解决方案
推荐阅读
- python - 如何阻止从列表中输入数字?
- java - 通过映射表使用@ManyToMany 映射实体设置投影
- python - 检查字符串是否具有相同频率的字符,是否删除 1 个字符
- python - 使用 cx_Freeze 将 python 文件转换为可执行文件然后得到错误 AttributeError: 'list' object has no attribute 'main_script'
- mysql - 错误:ER_NOT_SUPPORTED_AUTH_MODE 节点docker镜像和本地mysql连接错误
- angular - Angular SSR 如何使用 canActive Auth 防护
- matlab - 是否可以在 MATLAB 中使用 sendmail 或类似工具以编程方式创建电子邮件草稿
- user-interface - 在 SwiftUI 中创建类似 MacOS 的首选项列表样式视图
- azure - AKS 无法从 GitHub 私有注册表中提取图像,但 Minikube 可以提取相同的图像
- cluster-computing - 京都内阁集群和复制?