我正在使用kendo-knockout库。网格绑定到可观察的选项集。使用包含可观察数据数组和列定义的“英雄”选项初始化此可观察对象。
点击按钮后,observable会被另一个选项设置为“cars”,其中包含另一个可观察数据数组和另一列定义。
但是网格仍然显示英雄而不是汽车。
代码情况非常简单:
<button id="btn" data-bind="click: changeDataSource">change data source</button>
<div id="grid" data-bind="kendoGrid: heroOptions"></div>
var ViewModel = function () {
var self = this;
// first data source
self.heroes = ko.observableArray([ { name: ko.observable("spiderman"), age: ko.observable("29") }, { name: ko.observable("batman"), age: ko.observable("55") }, { name: ko.observable("hulk"), age: ko.observable("40") } ]);
// second data source
self.cars = ko.observableArray([ { model: ko.observable("Z8"), manufacturer: ko.observable("BMW"), country: ko.observable("Germany") }, { model: ko.observable("911"), manufacturer: ko.observable("Porsche"), country: ko.observable("Germany") }, { model: ko.observable("S600 AMG"), manufacturer: ko.observable("Mercedes"), country: ko.observable("Germany") } ]);
// observable for options (where the grid is bound to)
self.options = ko.observable();
// options for first data source
self.heroOptions = { data: self.heroes, columns: [{title:'Hero',field:'name'},{title:'Age',field:'age'}] };
// options for second data source
self.carOptions = { data: self.cars, columns: [{title:'Car',field:'model'}, {title:'Producer',field:'manufacturer'},{title:'Country',field:'country'}] };
// set initial grid options
self.options(self.heroOptions);
// method to bind grid to second data source
self.changeDataSource = function () {
self.options(self.carOptions);
};
}
var model = new ViewModel();
ko.applyBindings(model);
问题: 方法changeDataSource()使用self.carOptions填充self.options,但网格不会刷新。
我想我需要重新绑定/刷新或其他。