Knockout 和 JSTree 无法正确渲染
我在页面上使用 KnockoutJS 并尝试填充 JsTree 的 DOM,但 JsTree 无法正确识别叶子,也无法对 KnockoutJs 填充的叶子(或文件夹)的代码应用任何更改。有没有办法告诉 JsTree 有新数据并且应该重新评估所有内容? 页面开头的脚本
$(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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在填充列表后调用
.jstree
,因此请将调用移至$.getJSON
回调的末尾。此外,
navigation
应该是一个包含元素的容器,而不是列表本身。
将
partsViewModel
函数更改为:将 HTML 更改为:
此处< /strong> 是一个简化的示例。
更新:
如果您想在每次
更改时更新树,您可以使用 Knockout 的
.afterRender
回调。这将在 DOM 更新后调用。您不能使用.subscribe()
因为它是在 DOM 之前调用的变化。将其插入到
partsViewModel
中,并从.getJSON
回调中删除.jstree
部分:并将 HTML 更改为:
You need to call
.jstree
after the list is populated, so move the call to the end of the$.getJSON
callback.Also the
navigation
should be a container containing the<ul>
element and not the list itself.Change
partsViewModel
function 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.afterRender
callback. This will be called after the DOM is updated. You cannot use.subscribe()
because that is called before the DOM changes.Insert this into
partsViewModel
and remove the.jstree
part from.getJSON
callback:And change HTML to: