Extjs3.x 对普通panel数据加载的扩展

<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
				});











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值