<pre name="code" class="javascript">// 扩展panel加载方法
Ext.override(Ext.Panel, {
loadData : function(conf) {
var ct = this;
// 遍历该表单下所有的子项控件,并且为它赋值
var setByName = function(container, data) {
var items = container.items;
if (items != null) {
for (var i = 0; i < items.getCount(); i++) {
var comp = items.get(i);
if (comp.items) {
setByName(comp, data);
continue;
}
// 判断组件的类型,并且根据组件的名称进行json数据的自动匹配
var xtype = comp.getXType();
try {
if (xtype == 'textfield' || xtype == 'textarea'
|| xtype == 'radio' || xtype == 'checkbox'
|| xtype == 'datefield' || xtype == 'combo'
|| xtype == 'hidden' || xtype == 'numberfield'
|| xtype == 'datetimefield'
|| xtype == 'htmleditor' || xtype == 'label'
|| xtype == 'fckeditor' || xtype == 'EditorEx') {
var name = comp.getName();
if (name) {
if (conf.preName) {
name = name.substring(conf.preName.length
+ 1);
}
var val = eval('data.' + name);
if (val) {
if (xtype == 'datefield') {
val = val
.substring(0, val.indexOf('.'));
comp.setValue(Date.parseDate(val,
'Y-m-d H:i:s'));
} else if (xtype == 'textarea') {
comp.setValue(convertEnter(val));
} else {
comp.setValue(val);
}
}
}
}
} catch (e) {
// alert(e);
}
}
}
};
if (conf.isMask) {
if (!ct.loadMask) {
ct.loadMask = new Ext.LoadMask(Ext.getBody());
ct.loadMask.show();
}
}
if (!conf.data) {
Ext.Ajax.request({
method : 'POST',
url : conf.url,
scope : this,
success : function(response, options) {
var json = Ext.util.JSON
.decode(response.responseText);
var data = null;
if (conf.root) {
data = eval('json.' + conf.root);
} else {
data = json;
}
setByName(ct, data);
if (ct.loadMask) {
ct.loadMask.hide();
ct.loadMask = null;
}
if (conf.success) {
conf.success.call(ct, response, options);
}
},// end of success
failure : function(response, options) {
if (ct.loadMask) {
ct.loadMask.hide();
ct.loadMask = null;
}
if (conf.failure) {
conf.failure.call(ct, response, options);
}
}
});
} else {
setByName(ct, conf.data);
if (ct.loadMask) {
ct.loadMask.hide();
ct.loadMask = null;
}
}
}
});
用法
远程加载
Ext.getCmp('processform').loadData({
root : 'root',//
isMask : false,
preName : 'student',//组件name属性 前缀名 比如 student.name 取student
url : '',
success : function(ct, response, options) {
var result = Ext.decode(ct.responseText);
if (!result.success) {
p.disable();
}
},
failure : function(ct, response, options) {
Ext.ux.Toast.msg('编辑', '载入失败');
}
});
静态数据加载
form.loadData({
root : 'root',
data : [{},{}],
preName : 'student',//组件name属性 前缀名 比如 student.name 取student
});