【发布时间】:2013-09-26 23:03:26
【问题描述】:
我正在寻找一种在 KnockoutJS 中实现简单嵌套/可嵌套树结构的方法;它应该只允许两个级别。
到目前为止,我发现的是这个(以及 SO 上的一系列非常相似的方法):Knockout.js nested sortable bindings
但是,在这个例子中 - 和其他例子中,“Containers”不能变成“Children”,反之亦然。
基本上,我正在寻找这样的结构:http://jsfiddle.net/uhVAW/2/
即它最终会呈现一个包含两个级别的列表:父类别及其子类别。
我在 Knockout ViewModel 中的树形结构采用这种形状(没有所有更新逻辑):
var VM = function(cats)
{
this.categories = ko.observableArray(cats); // bound to the list
}
var Category = function
{
this.name = ko.observable();
this.children = ko.observableArray([]); // if exists (i.e. if the cat is a parent), this is bound to a list within the <li>
}
所以,本质上:
- 排序父级
- 在父母中对孩子进行分类
- 孩子可以成为父母,反之亦然
- 只允许 n 层嵌套(在我的例子中是 2 层)
干杯!
【问题讨论】:
标签: javascript knockout.js tree nested knockout-2.0