我正在使用RP Niemeyer的kendo-knockout库。我在knockout observable中使用dataSource运行kendoTreeView。
HTML:
<div id="main">
<div id="reportGrid" data-bind="kendoTreeView: { dataSource: treeViewDataSource }"> </div>
<button data-bind="click: addItem">Add</button>
</div>>
的javascript:
var billingReportViewModel = ko.observable({
treeViewDataSource: ko.observableArray([{text: "Tea" },{ text: "Coffee" }]),
addItem : function () {
this.treeViewDataSource.push({text: "Water"});
alert(this.treeViewDataSource().length);
}
});
ko.applyBindings(billingReportViewModel);
当我单击添加按钮时,我将向可观察数组添加一个新元素。该项目已添加,但视图未相应更新。
我正在按照这里的步骤进行操作:
Kendo-Knockout: use knockout view model with kendo datasource
我做错了吗?或者目前不支持此功能?如果是这样,我的选择是什么?每次添加/删除/更新节点时,我都可以从DOM中删除树视图,然后使用新的数据源重新创建它。但我希望绑定工作或一些更灵活的解决方案。谢谢!
的jsfiddle:
更新
我还能够使用对小部件的引用来加载treeView,如下所示:
HTML:
<div data-bind="kendoTreeView: { widget: myWidget}">
</div>
<button data-bind="click: initialize">Initialize</button>
的javascript:
var ViewModel = function () {
this.initialize = function () {
var inline = new kendo.data.DataSource({
data: [
{ id: 1, text: "Tea", sprite: "icon-tea" },
{ id: 2, text: "Coffee", sprite: "icon-coffee" }
]
});
var widget = this.myWidget();
widget.setDataSource(inline);
};
//hold the widget
this.myWidget = ko.observable();
};
ko.applyBindings(new ViewModel());
使用这种方法我想在树视图中添加,删除和更新项目。我正在考虑操纵inline
数据源,我希望视图会相应地更新(类似于我上面原始帖子中可观察数据源的想法)。我怎样才能做到这一点 ?任何有效的例子都会很棒!
答案 0 :(得分:1)
根据documentation,应该将knockout-kendo TreeView添加到最外层的ul元素中。此外,不使用kendo.data.DataSource,只需使用标准的淘汰技术添加其他嵌套的ul和li元素。
更新:添加了添加项目按钮。添加了节点,但没有剑道样式。添加了一个hack来重新绑定淘汰节点。
// Here's my data model
var node = function(title){
this.title = ko.observable(title);
this.children = ko.observableArray();
}
var ViewModel = function() {
this.tvWidget = ko.observable();
this.children = ko.observableArray([new node('a'), new node('b'), new node('c')]);
this.children()[0].children([new node('d'), new node('e'), new node('f')]);
this.children()[0].children()[1].children([new node('d'), new node('e'), new node('f')]);
this.children()[2].children([new node('d'), new node('e'), new node('f')]);
};
ViewModel.prototype.addItem = function() {
this.children.push(new node('n'));
this.children()[0].children.push(new node('n'));
ko.cleanNode(document.getElementById('treeviewdiv'))
ko.applyBindings(vm, document.getElementById('treeviewdiv'))
};
var vm = new ViewModel();
ko.applyBindings(vm);
<link href="http://rniemeyer.github.io/knockout-kendo/css/kendo.default.min.css" rel="stylesheet"/>
<link href="http://rniemeyer.github.io/knockout-kendo/css/kendo.common.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="http://rniemeyer.github.io/knockout-kendo/js/kendo.all.min.js"></script>
<script src="http://rniemeyer.github.io/knockout-kendo/js/knockout-kendo.min.js"></script>
<div id="treeviewdiv">
<ul data-bind="kendoTreeView: {widget: tvWidget}, template: { name: 'node-template', foreach: children }"></ul>
</div>
<button data-bind="click: addItem">Add item</button>
<script type="text/html" id="node-template">
<li>
<span data-bind="text: title"></span>
<ul data-bind="template: { name: 'node-template', foreach: children }"></ul>
</li>
</script>