看起来在调用 GridView 的 init() (从 Grid 的 调用)之前,GridView 的商店“添加”事件的侦听器不会添加到视图中onRender()
。这发生在渲染网格时 (win.show())。您在 #2 (insert-insert-show) 上没有收到错误,因为它从未命中Ext.fly(rows[0]).addClass(this.firstRowCls);
GridView 中的processRows()
.
我稍微更改了代码以在 GridViews init 和 onAdd(商店的添加事件的处理程序)中打印出一些日志记录:
Ext.onReady(function(){
var cm = new Ext.grid.ColumnModel({
columns: [{
dataIndex: 'common',
editor: new Ext.form.TextField({allowBlank: false})
}]
});
var store = new Ext.data.Store({
reader: new Ext.data.JsonReader({
record: 'plant',
fields: [{name: 'common', type: 'string'}]
})
});
var grid = new Ext.grid.EditorGridPanel({
store: store,
cm: cm,
region: "center",
view: new Ext.grid.GridView({
init : function(grid) {
console.log("\tinit()");
this.grid = grid;
this.initTemplates();
this.initData(grid.store, grid.colModel);
this.initUI(grid);
},
onAdd : function(store, records, index) {
console.log("\t\tadding: ", records[0].id);
this.insertRows(store, index, index + (records.length-1));
}
})
});
var win = new Ext.Window({
layout: "border",
items: [grid],
width : 300,
height : 300
});
var Rec = grid.getStore().recordType;
/*
console.log("show-insert-insert");
win.show();
console.log("\t1st insert");
store.insert(0, new Rec());
console.log("\t2nd insert");
store.insert(0, new Rec());
*/
console.log("insert-insert-show");
console.log("\t1st insert");
store.insert(0, new Rec());
console.log("\t2nd insert");
store.insert(0, new Rec());
console.log("win.show()");
win.show();
});
以下是三种不同情况的控制台:
show-insert-insert
init()
1st insert
adding: ext-record-1
2nd insert
adding: ext-record-2
win.show()
insert-show-insert
1st insert
win.show()
init()
2nd insert
adding: ext-record-2
Ext.fly(rows[0]) is null
[Break On This Error]
Ext.fly(rows[0]).addClass(this.firstRowCls);
ext-all-debug.js (line 43959
insert-insert-show
1st insert
2nd insert
win.show()
init()
如您所见,它实际上将两个项目添加到 Grid(和 GridView)的唯一情况是在 show-insert-insert 案例中。到目前为止我发现的最佳解决方案就是做
win.show();
win.hide();
一开始。到目前为止,我还没有找到更好的解决方案。我希望这有帮助。