I am looking for a way to implement a simple nested/nestable tree structure in KnockoutJS; and it should only allow two levels.
What I found so far is this (and a range of very similar approaches here on SO): Knockout.js nested sortable bindings
However, in this example - and the others, "Containers" cannot become "Children" and vice versa.
Basically, I am looking for a structure like this: http://jsfiddle.net/uhVAW/2/
I.e. it will ultimately render a list with two levels: parent categories and their children.
My tree-structure in the Knockout ViewModel takes this shape (without all the updating logic):
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>
}
So, in essence:
- Sorting parent level
- Sorting children within parents
- Children can become parents & vice versa
- Only allowing n-levels of nesting (2 in my case)
Cheers!