1 回答
TA贡献1906条经验 获得超10个赞
您只能使用一个变量,即:project并将您的变量附加到每个循环中html使用的变量,最后您可以将其附加到您的 div 中。工作代码:+=project
var response = {
"status": "Success",
"data": [{
"project_id": "1",
"project_name": " Project Name ",
"l1_task": [{
"l1_id": "1",
"l1_name": " MVP 1 ",
"l2_task": [{
"l2_id": "1",
"l2_name": " Sprint 1 ",
"l3_task": [{
"l3_id": "1",
"l3_name": " Sprint 1 Task 1"
},
{
"l3_id": "1",
"l3_name": " Sprint 1 Task 2 "
}
]
}]
},
{
"l1_id": "1",
"l1_name": " MVP 2 ",
"l2_task": [{
"l2_id": "1",
"l2_name": " Sprint 1 ",
"l3_task": [{
"l3_id": "1",
"l3_name": " Sprint 1 Task 1"
},
{
"l3_id": "1",
"l3_name": " Sprint 1 Task 2 "
}
]
}]
}
]
}
]
};
if (response.status == "Success") {
// Layer Project Name
$.each(response.data, function(key, value) {
project = "<div class='text-danger card container'><span>" + value.project_name + " - </span>";
// Layer MVP
$.each(value.l1_task, function(key, value) {
project += "<div class='card bg-info pd-10 container'><span>" + value.l1_name + " Layer MVP - </span>";
// Layer Sprint
$.each(value.l2_task, function(key, value) {
project += "<div class='card bg-warning pd-20 container'><span>" + value.l2_name + " Layer Sprint - </span>";
// Layer Task
$.each(value.l3_task, function(key, value) {
project += "<div class='card bg-dark container'><span>" + value.l3_name + " Layer Task - </span>" +
"</div>"; // for Layer Task
});
project += "</div>";
});
project += "</div>";
});
project += "</div>" // for Layer Sprint
$("#projectDetail .view_result").append(project);
});
} else {
// else
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="container-fluid" id="projectDetail">
<div class="row mt-3 view_result">
</div>
</div>
- 1 回答
- 0 关注
- 90 浏览
添加回答
举报