1
Hello,
I need to create a treeview that accepts an observable as input so that as I remove/add a node in the tree the same update. I have been researching and many people use the biding template to do this however, I’m still a little confused about its use.
Below follows my attempt at code. In it I can only print the first element of the tree and not its children.
var ViewModel = function(){
self = this;
self.data = ko.observable(data);
debugger;
}
var data = {
items: [{
"name": "MORPHED",
"items": [{
"name": "5 Day",
"items": [{
"CategoryId": 20,
"name": "30 day countdown"
}, {
"CategoryId": 19,
"name": "Staffing your program"
}, {
"CategoryId": 22,
"name": "Emergency/Medical Information"
}, {
"CategoryId": 18,
"name": "Promoting your program"
}, {
"CategoryId": 21,
"name": "Week of camp"
}]
}, {
"name": "4 Day",
"items": []
}, {
"name": "1/2 Day",
"items": []
}, {
"name": "Age Targeted",
"items": []
}]
}, {
"name": "CREATE",
"items": [{
"name": "5 Day",
"items": []
}, {
"name": "4 Day",
"items": []
}, {
"name": "1/2 Day",
"items": []
}]
}, {
"name": "INNOVATE",
"items": [{
"name": "5 Day",
"items": []
}, {
"name": "4 Day",
"items": []
}, {
"name": "1/2 Day",
"items": []
}]
}, {
"name": "ENVISION",
"items": [{
"name": "5 Day",
"items": []
}, {
"name": "4 Day",
"items": []
}, {
"name": "1/2 Day",
"items": []
}]
}]
};
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<ul data-bind="template: { name: 'itemTmpl', foreach: $root.data().items }"></ul>
<script id="itemTmpl" type="text/html">
<li>
<span data-bind="text: name"></span>
<ul data-bind="template: { name: 'itemTmpl', foreach: $root.data().items }">
</ul>
</li>
</script>
Here follows the example I’m basing.
Thank you so much, that’s what I was looking to do.
– Guto