How to prevent Kendo UI Grid from rebinding many times when updating ViewModels

扶醉桌前 提交于 2019-12-10 03:54:31

问题


When you bind to a Kendo UI Grid with MVVM, databound will fire once and all is well. If you need to update that data after the fact, every time you change one piece of data on any viewmodel (or child viewmodel), the entire grid re-databinds. Thus, if you had some cell in the grid that is bound to a template and you have to change 2 or 3 properties on the viewmodel from some external ajax source, Databound will fire 2 or 3 times for each model that is changed, causing the entire viewable area to rebind. How can we update lots of data at once and only have databound fire once?


回答1:


How exactly you rebind the Grid? Basically if you change some of the models like this:

dataItem.set('SomeField','new value');
dataItem.set('someOtherField','other value');

This way the Grid will be indeed bound two times because of the MVVM. The change event is triggered each time you call set.

However if you update the values like this:

dataItem.SomeField='new value';
dataItem.someOtherField= 'other value';

The Grid wont react to the change and wont rebind re-read the values from the models you can force the Grid to do it through the refresh method.

$('#gridName').data().kendoGrid.refresh()



回答2:


I'm not sure if there is some way to temporarily tell the grid to stop listening to events and then at the end, re-sync once. If there is, please give that answer here! Otherwise, what I did instead is I didn't go through .set() for each item. Instead, I updated the data for all the rows by setting the data directly to the property. Then when I got to the very last row I was updating, I called .set() on the last property that needed to be updated. This will cause databound to fire only once and the entire grid will refresh itself with all the data that was changed. If you don't do it this way, then the more rows displayed on the page, the longer it takes to process. (It could take 20+ seconds before the user can do anything again.)




回答3:


It looks like the dataBinding event is where you can prevent a rebind on the grid.

Telerik Online Docs



来源:https://stackoverflow.com/questions/15579857/how-to-prevent-kendo-ui-grid-from-rebinding-many-times-when-updating-viewmodels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!