在不触发事件的情况下替换剑道ObservableArray的内容

Replace content of Kendo ObservableArray without event firing

本文关键字:ObservableArray 替换 情况下 事件      更新时间:2023-09-26

我有一个KendoUI网格,其中包含ObservableArray中的数据。

var aKendoObservableArray = new kendo.data.ObservableArray([ .....]);
// defining the grid ....
dataSource: {
                data: aKendoObservableArray,
                pageSize: 10,
                schema: {
                    model: {
                        id: "_jobInstanceId" // the identifier of the model
                    }
                }
            },

既然我已经从服务器获得了新数据,我想替换这个数组的内容。我尝试过拼接数组来清除它,并逐个添加新内容。这导致了巨大的性能下降,因为kendo试图在每次添加时确定如何对网格进行分页。有没有一种方法可以用只触发一次的更改事件来替换Kendo UI ObservableArray的内容?

KendoUI论坛有这个问题的答案。我们需要清理数组(触发一个remove事件)并一次推送所有元素(触发一次add事件)

http://www.telerik.com/forums/passing-array-to-observablearray-push

因此,我需要的是

// clean the array
aKendoObservableArray.splice(0, aKendoObservableArray.length);
aKendoObservableArray.push.apply(aKendoObservableArray, [{...},{...},{...},{...},{...}];

最终,我必须这样做才能删除一个项目:

var idToRemove = $(this).parent().attr('data-id');
// remove all items that do not share the ID the user is trying to delete
for (var i = 0; i < e.model.MyArrayOfItems.length; i++) {
    if (e.model.MyArrayOfItems[i].idToMatch == idToRemove)
        e.model.MyArrayOfItems.splice(i, 1);
}