在使用ext grid的过程中,修改了一条记录,grid数据重新加载了,但是选中项的值并没有改变,如何处理呢,有两种方案
第一种:
更新数据后对grid的SelectionModel进行清空处理
代码: grid.getSelectionModel().deselectAll();
第二种:
经过分析我觉得可能是grid中的SelectionModel中的记录并没有改变,所以才会到这这样的情况,正常流程应该是store重新加载后,需要更新SelectionModel中选中的记录。
于是查看Ext.selection.Model的源码,找到为store绑定事件的方法getStoreListeners,方法内容如下。
getStoreListeners: function() {
var me = this;
return {
add: me.onStoreAdd,
clear: me.onStoreClear,
bulkremove: me.onStoreRemove,
update: me.onStoreUpdate,
load: me.onStoreLoad,
idchanged: me.onModelIdChanged,
refresh: me.onStoreRefresh
};
},
发现监听了store的load事件,于是猜想问题应该出在onStoreLoad方法上。于是找到这个方法
/**
* @abstract
* @private
*/
onStoreLoad: Ext.emptyFn,
发现Ext对这个方法的声明是abstract,这个方法是一个空的方法。由此发现Ext的SelectionModel并没有对store重新加载后进行任何的处理。所以我们就自己处理,重写
Ext.selection.Model
的onStoreLoad方法,在这个方法中更新选中的记录,代码如下
-
-
-
-
- Ext.override(Ext.selection.Model,{
- onStoreLoad:function(store, records, successful, eOpts){
- var me = this,
- length = me.selected.getCount( );
-
-
- if(length===0)return;
-
-
- me.selected.eachKey(function(key,item){
- var model = store.getById(key);
-
-
- if(model){
- me.selected.add(model);
- }else{
- me.selected.removeAtKey(key);
- }
- })
-
- }
- });
加上上面的代码后问题解决。