我有一個JSON對象,它也包含一個嵌套的項目--TagListList和TaskRecordList。在HTML表格中顯示嵌套的JSON數據對象
我想顯示EntityCode和EntityName作爲'父'行,然後顯示TagList,然後顯示最後嵌套層 - TaskRecordList數據在此之下。
如何使用Knockout來做到這一點?
var viewModel = function(data) {
var self = this;
// variables
self.taskRecords = ko.observableArray([
\t \t {
"EntityCode": "SP00",
"EntityName": "Sample",
"TagList": [
{
"TagID": 3,
"TagName": "Level 3",
"TaskRecordList": [
{
"TaskRecordID": 33,
"TaskName": "Cash",
"Period": "2017-05-16T00:00:00"
},
{
"TaskRecordID": 34,
"TaskName": "CashTest",
"Period": "2017-05-16T00:00:00"
}
]
},
{
"TagID": 2,
"TagName": "Level 5",
"TaskRecordList": [
{
"TaskRecordID": 27,
"TaskName": "FileIN",
"Period": "2017-05-16T00:00:00"
},
{
"TaskRecordID": 21,
"TaskName": "FileTest",
"Period": "2017-05-16T00:00:00"
},
{
"TaskRecordID": 35,
"TaskName": "OTE Rec",
"Period": "2017-05-16T00:00:00"
}
]
}
]
},
{
"EntityCode": "DEMO",
"EntityName": "Demo Fund",
"TagList": []
}
]);
};
ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class="table table-hover">
<thead>
<tr>
<th>Entity Code</th>
<th>Entity Name</th>
<th>TagID</th>
<th>TagName</th>
<th>TaskRecordID</th>
<th>TaskName</th>
</tr>
</thead>
<tbody id="sortable" data-bind="foreach: taskRecords">
<tr class="ui-state-default ui-state-disabled" >
<td class="ui-state-default" data-bind="text: EntityCode"></td>
<td class="ui-state-default" data-bind="text: EntityName"></td>
<td></td>
<td></td>
</tr>
<tr data-bind="foreach: TagList">
<td></td>
<td></td>
<td class="ui-state-default" data-bind="text: TagID"></td>
<td class="ui-state-default" data-bind="text: TagName"></td>
</tr>
<tr data-bind="foreach: TaskRecordList">
<td></td>
<td></td>
<td></td>
<td></td>
<td class="ui-state-default" data-bind="text: TaskRecordID"></td>
<td class="ui-state-default" data-bind="text: TaskName"></td>
</tr>
</tbody>
</table>
您好,我現在有一個3層的對象。我如何綁定這個? http://jsfiddle.net/v2L1ny8r/6/ – Kevin
@Kevin你想要第三層在同一行或第二層下面的新行? –
Jason,作爲第二層以下的新行 – Kevin