Javascript 在div jquery中动态创建多个表
我得到以下JSON结果,需要将其绑定到UI中:Javascript 在div jquery中动态创建多个表,javascript,jquery,Javascript,Jquery,我得到以下JSON结果,需要将其绑定到UI中: [ { "name":"abc", "class" : "first" }, { "name":"def", "class" : "first" } ],[ { "name":"ijk", "class" : "second" }, { "name":"lmn", "
[
{
"name":"abc",
"class" : "first"
},
{
"name":"def",
"class" : "first"
}
],[
{
"name":"ijk",
"class" : "second"
},
{
"name":"lmn",
"class" : "second"
}
]
HTML:
Javascript:
for(j=0;J<somelength;j++) {
//create table header here
var html = `"<table class='table' id='tab' width='100%'
style='margin-bottom: 0px;'><thead><th>NAME</th><th>CLASS</th></thead></table>";`
$('#itemListTable').html(html);
for(i=0;i<somelength;j++){
invcdTlbTr += '<tr>'
+ '<td width="20%">' + std.name + '</td>'
+ '<td width="35%">' + std.class + '</td>'
+ '</tr>';
}
invcdTlbBody.innerHTML = invcdTlbBody.innerHTML
}
for(j=0;j您可以使用Array.map
迭代数组,并使用模板字符串
(`````)为每个外部数组创建一个表,其中标题表示名称和类
var json=[[
{
“名称”:“abc”,
“类”:“第一”
},
{
“名称”:“定义”,
“类”:“第一”
}
],[
{
“名称”:“ijk”,
“类”:“第二”
},
{
“名称”:“lmn”,
“类”:“第二”
}
]];
var table=json.map(items=>{
var tbody=items.map(item=>`
${item.name}
${item.class}
`).加入(“”);
返回`
名称
等级
${tbody}
`;
}).加入(“”);
var itemListTable=document.getElementById('itemListTable');
itemListTable.innerHTML=table;
您的json是错误的,好像它是一个对象({}),它将是一个键/值pair@anura这是一个json数组我更新了我的问题你能看看这里吗?
for(j=0;J<somelength;j++) {
//create table header here
var html = `"<table class='table' id='tab' width='100%'
style='margin-bottom: 0px;'><thead><th>NAME</th><th>CLASS</th></thead></table>";`
$('#itemListTable').html(html);
for(i=0;i<somelength;j++){
invcdTlbTr += '<tr>'
+ '<td width="20%">' + std.name + '</td>'
+ '<td width="35%">' + std.class + '</td>'
+ '</tr>';
}
invcdTlbBody.innerHTML = invcdTlbBody.innerHTML
}