I am using KnockoutJS on my page and trying to populate the DOM for a JsTree, but the JsTree is not identifying the leaves correctly and applying any changes to the code of the leaves(or folders) that KnockoutJs populates. Is there a way to tell JsTree that there is new data and should re-evaluate everything?
Script at beginning of page
$(document).ready(function() {
function treeNode(data) {
var self = this;
self.id = ko.observable(data.ID);
self.name = ko.observable(data.Name);
}
function partsViewModel() {
var self = this;
self.partTypes = ko.observableArray([]);
self.selectedPartType = ko.observable();
self.selectedPartType = ko.observable();
$.getJSON("/PartKO/PartTypes", function(allData) {
var mappedPartTypes = $.map(allData, function(item) { return new treeNode(item); });
self.partTypes(mappedPartTypes);
});
$("#navigation").jstree({
"themes": { "theme": "classic" },
"plugins": ["themes", "html_data"]
});
}
ko.applyBindings(new partsViewModel());
});
HTML
ul id="navigation" class="treeview" data-bind="foreach: partTypes">
<li>
<a href="#" data-bind="text:name"></a>
</li>
</ul>
You need to call
.jstreeafter the list is populated, so move the call to the end of the$.getJSONcallback.Also the
navigationshould be a container containing the<ul>element and not the list itself.Change
partsViewModelfunction to:And your HTML to:
HERE is a simplified example.
Update:
If you want to update the tree everytime the
<ul>is changed you can use Knockout’s.afterRendercallback. This will be called after the DOM is updated. You cannot use.subscribe()because that is called before the DOM changes.Insert this into
partsViewModeland remove the.jstreepart from.getJSONcallback:And change HTML to: