我有一个这样的嵌套 JSON:
[
{
"Run 1": {
"1.2.0": {
"Server": {
"TestSuite 1": [
{
"version": "1.2.0",
"type": "server",
"testdef": "TestSuite 1",
"testcaseid": "TestCase 1",
"status": "pass"
},
{
"version": "1.2.0",
"type": "server",
"testdef": "TestSuite 1",
"testcaseid": "TestCase 2",
"status": "fail"
}
],
"TestSuite 2": [
{
"version": "1.2.0",
"type": "server",
"testdef": "TestSuite 2",
"testcaseid": "TestCase 1",
"status": "pass"
}
]
}
}
}
}
]
我想将它作为无序列表显示在 html 页面上,如下所示:
但我看到最后一项重复了多次:
这是带有 knockout.js 绑定的 html:
<div class="row">
<div class="tree">
<ul data-bind="foreach: {data: testResults, as: 'item'}">
<li data-bind="foreach: {data: Object.keys(item), as: 'key'}"><span data-bind="text: 'Run' + key"></span>
<ul data-bind="foreach: {data: item[key], as: 'item2'}" class="child">
<li data-bind="foreach: {data: Object.keys(item2), as: 'key2'}"><span data-bind="text: key2"> </span>
<ul data-bind="foreach: {data: item2[key2], as: 'item3'}" class="child">
<li data-bind="foreach: {data: Object.keys(item3), as: 'key3'}"><span data-bind="text: key3"> </span>
<ul data-bind="foreach: {data: item3[key3], as: 'item4'}" class="child">
<li data-bind="foreach: {data: Object.keys(item4), as: 'key4'}"><span data-bind="text: key4"> </span>
<ul data-bind="foreach: {data: item4[key4], as: 'item5'}" class="child">
<li data-bind="foreach: {data: Object.keys(item5), as: 'key5'}"><span data-bind="text: item5.testcaseid, css : {'bg-success' : item5.status == 'pass', 'bg-danger' : item5.status == 'fail'}"> </span><br></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
testResults
是包含上述 JSON 的视图模型中的 ko.observableArray()。
什么是只显示一次叶子元素的正确方法