所以在我的数据库中,我有一个与自身有一对多关系的模型。一个很好的例子是像reddit这样的评论系统。
我目前正在做这样的事情:
<div class="body" data-bind="foreach: { data: Comments}">
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- ko foreach: { data: Children } -->
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- /ko -->
</div>
显然只支持一个级别的孩子。 当一个Child(Children [i])可能有也可能没有需要循环的Children数组时,是否有一种干净的方式来构建它。在我的例子中,技术上可能是这样的无限级别(它不会)。
我很确定我能想出一些hacky但我认为可能有更好的方法。 感谢。
编辑:
我想要映射的数据:
{
"@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
"SectionID":4,
"Text":"Text",
"Html":null,
"OrderQualifier":"1",
"IsUserCreated":false,
"Children":[
{
"@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
"SectionID":4,
"Text":"Text",
"Html":null,
"OrderQualifier":"1",
"IsUserCreated":false,
"Children":[
{
"@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
"SectionID":4,
"Text":"Text",
"Html":null,
"OrderQualifier":"1",
"IsUserCreated":false,
"Children":[
{
"@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
"SectionID":4,
"Text":"Text",
"Html":null,
"OrderQualifier":"1",
"IsUserCreated":false,
"Children":[
]
}
]
}
]
}
]
}
正如您所看到的,这包含3个级别的儿童评论,但我需要能够处理未知数量的儿童评论。
答案 0 :(得分:2)
knockout模板支持递归。 http://jsfiddle.net/m812qjeq/2/
<div class="body" data-bind="foreach: Comments">
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
</div>
<script type="text/html" id="childTemplate">
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- ko if: $data.Children -->
<!-- ko foreach: Children -->
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
<!-- /ko -->
<!-- /ko -->
</script>
答案 1 :(得分:1)
您可以使用模板
<div class="body" data-bind="foreach: comments">
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
</div>
<script type="text/html" id="childTemplate">
<span data-bind="text:comment"></span>
<!-- ko if: $data.childrenLength > 0 -->
<!-- ko foreach: children -->
<div data-bind="template: { name: 'childTemplate', data: $data }" style="padding-left:35px;"></div>
<!-- /ko -->
<!-- /ko -->
</script>
示例Viewmodel
var comments = [{
id: 1,
comment: 'How can i use knockoutjs?',
childrenLength: 3,
children: [{
id: 2,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 3,
comment: 'Please read the documentation',
childrenLength: 0,
children: []
}, {
id: 4,
comment: 'You can see the blog posts on this',
childrenLength: 2,
children: [{
id: 5,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 6,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}]
}]
}, {
id: 7,
comment: 'You question is not sufficient to be asked here?',
childrenLength: 3,
children: [{
id: 8,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 9,
comment: 'Please read the documentation',
childrenLength: 0,
children: []
}, {
id: 10,
comment: 'You can see the blog posts on this',
childrenLength: 0,
children: []
}]
}]
var vm = function(){
var self = this
self.comments = ko.observableArray(comments)
}
$('document').ready(function () {
ko.applyBindings(new vm())
})