2
votes

I am using RP Niemeyer`s kendo-knockout library. I am running kendoTreeView using dataSource in knockout observable.

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);

When I click the add button I am adding a new element to the observable array. The item is added but the view is not updated accordingly.

I am following the steps from here:

Kendo-Knockout: use knockout view model with kendo datasource

Am I doing something wrong? Or this functionality is not currently supported? If so, what are my options? I may remove the treeview from the DOM everytime I add/remove/update a node and then recreate it with the new datasource. But I hope the bindings to work or some more flexible solution. Thanks!

jsFiddle:

http://jsfiddle.net/6SEzp/42/

Update:

I was also able to load the treeView using the reference to the widget like this:

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());

Using this approach I want to add, remove and update items in the treeview. I am thinking something like manipulating the inline datasource and I hope that the view will update accordingly (something like the idea of observable datasource from my original post above). How can I do that ? Any working example will be great!

1

1 Answers

2
votes

According to the documentation, the knockout-kendo TreeView should be added to the outer most ul element. Also, instead of using a kendo.data.DataSource, simply add additional nested ul and li elements using standard knockout techniques.

UPDATE: Added an add item button. Nodes were added, but without the kendo styles. Added a hack to rebind the knockout node.

// 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>