一、原始grid
store、columnModel、editorGridPanel的声明
var store;
(function() {
var data= [ [ 'a', '1' ], [ 'b', '2' ], [ 'c', '3' ], [ 'd', '4' ] ];
store = new Ext.data.ArrayStore({
fields : [ {
name : 'kind'
}, {
name : 'value'
} ]
});
store.loadData(data);
})();
var cm = new Ext.grid.ColumnModel({
// specify any defaults for each column
defaults : {
sortable : true
// columns are not sortable by default
},
columns : [ new Ext.grid.RowNumberer({
width : 26
}), {
id : 'kind',
header : '种类',
dataIndex : 'kind',
width : 120
},{
id : 'value',
header : '值',
dataIndex : 'value',
width : 120,
header : '<span style="font-weight:bold">标称下限</span>',
tooltip : '可编辑列',
editor : new Ext.form.NumberField({
id : 'lowerOffsetEditor',
allowBlank : false
})
} ]
});
var gridPanel = new Ext.grid.EditorGridPanel({
region : "center",
frame : false,
store : store,
cm : cm,
loadMask : true,
tbar : [ '-', {
text : '保存',
id : 'savePhysical',
icon : '../../../resource/images/btnImages/disk.png',
handler : function(){
// 保存用js代码
}
} ]
});
二、使用listener动态改变编辑器
修改后gridPanel如下:
var gridPanel = new Ext.grid.EditorGridPanel({
region : "center",
frame : false,
store : store,
cm : cm,
loadMask : true,
tbar : [ '-', {
text : '保存',
id : 'savePhysical',
icon : '../../../resource/images/btnImages/disk.png',
handler : function(){
// 保存用js代码
}
},
listeners : {
'beforeedit' : function(e) {
var kind = e.record.get("kind");
if (kind == "a" || kind == "b") {
this.getColumnModel().setEditor(e.column, new Ext.form.TextField({
editable : true,
allowBlank : false,
regex : /^1E-(\d)+$/
}));
} else {
this.getColumnModel().setEditor(e.column, new Ext.form.NumberField({
allowBlank : false
}));
}
}
}