1

我正在使用淘汰赛自定义 bindingHandler 根据 rniemeyer 的示例将数据绑定到 slickgrid,当代码运行时,如果您单击“添加”链接,则使用 myObservableArray.push(newRow) 将一行添加到网格中,但是当您尝试通过“替换”链接替换网格中的所有数据,网格未更新,添加按钮停止工作。我正在尝试通过调用 myObservableArray(multipleRows) 来更新可观察数组,这是正确的做法吗?

示例:http: //jsfiddle.net/davidoleary/bwSmy/

var viewModel = {
    items: ko.observableArray([]),
    addItem: function() {
        this.items.push(new Item(0, "New", 5.00));  // works
    },
    replace: function() {
        var newList = [new Item(0, "New", 6.00),new Item(0, "New", 7.00)]
        this.items(newList); // fails without throwing error
    }, 
}
4

3 回答 3

1

You missed the SetData in the update of the knockout custom binding. grid.setData(data,true);

here's what it would look like:

ko.bindingHandlers.slickGrid = {
    init: function(element, valueAccessor) {
        var settings = valueAccessor();
        var data = ko.utils.unwrapObservable(settings.data);
        var columns = ko.utils.unwrapObservable(settings.columns);
        var options = ko.utils.unwrapObservable(settings.options) || {};
        grid = new Slick.Grid(element, data, columns, options);
    },
    update: function(element, valueAccessor, allBindingAccessor, viewModel) {
        var settings = valueAccessor();
        var data = ko.utils.unwrapObservable(settings.data); //just for subscription
        grid.resizeCanvas(); // NB Very important for when a scrollbar appears        
        grid.setData(data,true); // This is what was missing
        grid.render();
    }
}

updated fiddle: http://jsfiddle.net/joybroto/bwSmy/4/

于 2013-06-06T01:33:18.523 回答
0

Knockout 和 SlickGrid 使用底层数据数组来执行它们的工作。您需要做的就是让它们使用相同的数组。经过

this.items(newList); 

你让 Knockout 使用其他数组。这里的好选项是要求 SlickGrid 做同样的事情:

grid.setData(newList, true);
grid.render();

这里是http://jsfiddle.net/bwSmy/6/查看其他选项

于 2013-06-06T02:01:47.833 回答
0

我不使用 Knockout,但我想它需要看起来像这样:

var newList = [new Item(0, "New", 6.00),new Item(0, "New", 7.00)];
this.items(ko.observableArray(newList));
于 2013-06-06T01:08:10.807 回答