ITeye博客

Ext表单组件实现用户注册

y1d2y3xyz · 2009-08-04 20:55 · Extjs · Web前端 · Markdown

先看一下总体的效果! 这个界面是我由http://java-cn.com/members/register.jsp的注册表单转换成ExtJS实现! 这个表单基本上用到了ExtJS的所有组件,我先通过这个表单把EXT的Form组件功能呈现给大家,然后我会在接下来的文章中,针对每个组件进行具体的讲解!代码看上去会比较多,但实现很容易,目前还是针对界面部分,虽然表单中会设计到store方面的,但我都是用的本地模式实现,至于结合后台语言的话我会在grid和store那部分专门去讲解! 我分俩个文件(form.html和register.js)和一个图片文件夹,先看源码:

form.html:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
	<head>
		<title>register.html</title>
		<style type="text/css">  
			body{background-color:#777777} 
			#form-ct{width: 700px;margin-left: auto;margin-right: auto;} 
		</style>  
		<link rel="stylesheet" type="text/css" href="../Ext/resources/css/ext-all.css" />
		<script type="text/javascript" src="../Ext/adapter/ext/ext-base.js"></script>
		<script type="text/javascript" src="../Ext/ext-all.js"></script>
		<script type="text/javascript" src="register.js"></script>
	</head>
	<body>
			<div id="form-ct"></div>
	</body>
</html>


register.js


Ext.apply(Ext.form.VTypes, {
			postNum:function(v) {
				return /^[1-9]\d{5}$/.test(v);
	    },
	    postNumText: '邮政编号必须为6位的数字,并且第一位不能为0',

			password : function(val, field) {
				if (field.initialPassField) {
					var pwd = Ext.getCmp(field.initialPassField);
					return (val == pwd.getValue());
				}
				return true;
			},
			passwordText : '倆次輸入的密碼不一致,請重新輸入',

			emailvalidator : function(val, field) {
				if (field.initialField) {
					var em = Ext.getCmp(field.initialField);
					return (val == em.getValue());
				}
				return true;
			},
			emailvalidatorText : '倆次輸入的郵箱地址不一致,請重新輸入'
		});

Ext.onReady(function() {
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'qtip';
	var imageDate = [['i01.jpg', 'images/i01.jpg'],
			['i02.jpg', 'images/i02.jpg'], ['i03.jpg', 'images/i03.jpg'],
			['i04.jpg', 'images/i04.jpg'], ['i05.jpg', 'images/i05.jpg'],
			['i06.jpg', 'images/i06.jpg'], ['i07.jpg', 'images/i07.jpg'],
			['i08.jpg', 'images/i08.jpg'], ['i09.jpg', 'images/i09.jpg'],
			['i10.jpg', 'images/i10.jpg']];
	var image_store = new Ext.data.SimpleStore({
				fields : ["image_name", "image_url"],
				data : imageDate
			});
	var proviceDate = [{pname:'江西',pvalue:'jiangxi'},{pname:'湖南',pvalue:'hunan'},
										 {pname:'湖北',pvalue:'hubei'},{pname:'安徽',pvalue:'anhui'},
										 {pname:'四川',pvalue:'sichuang'},{pname:'河北',pvalue:'hebei'},
										 {pname:'广东',pvalue:'guangdong'},{pname:'福建',pvalue:'fujian'},
										 {pname:'其他',pvalue:'others'}];
	var provice_store = new Ext.data.JsonStore({
		data :proviceDate,
		fields :[{name:'provice_name',mapping:'pname'},{name:'provice_value',mapping:'pvalue'}]
	})
	var industryDate = [{
				iname : '自由职业',
				ivalue : 'zyzy'
			}, {
				iname : '企业干部',
				ivalue : 'qygb'
			}, {
				iname : '政府官员',
				ivalue : 'zfgy'
			}, {
				iname : '公务员',
				ivalue : 'gwy'
			}, {
				iname : '工人',
				ivalue : 'gr'
			}, {
				iname : '农民',
				ivalue : 'nm'
			}, {
				iname : '主妇',
				ivalue : 'zf'
			}, {
				iname : '待业',
				ivalue : 'dy'
			}, {
				iname : '军人',
				ivalue : 'jr'
			}, {
				iname : '其他',
				ivalue : 'others'
			}];
	var industry_store = new Ext.data.JsonStore({
				data : industryDate,
				fields : [{
							name : 'industry_name',
							mapping : 'iname'
						}, {
							name : 'industry_value',
							mapping : 'ivalue'
						}]
			})
	var education_data = 
		[
			{education_value:"小学"},
			{education_value:"初中"}, 
			{education_value:'高中'}, 
			{education_value:'中專'}, 
			{education_value:'大專'},
			{education_value:'大學'}, 
			{education_value:'本科'}, 
			{education_value:'碩士'}, 
			{education_value:'博士'}
	];
	var education_store = new Ext.data.JsonStore({
		data:education_data,
		fields : [{name :'education_value',mapping : 'education_value'}]
	});
	var form = new Ext.FormPanel({
		title : '新会员注册',
		width : 700,
		autoHeight : true,
		renderTo : 'form-ct',
		bodyStyle : "padding:2px",
		border : false,
		frame : true,
		items : [{
			autoheight : true,
			xtype : 'fieldset',
			title : '必填注册信息',
			items : [{// 用户名信息
				layout : 'column',
				defaults : {
					columnwidth : '.5'
				},
				items : [{
							layout : 'form',
							items : {
								fieldlabel : '用户名',
								xtype : 'textfield',
								name : 'username',
								allowblank : false,
								blanktext : '用戶名不能為空',
								minlength : 4,
								minlengthtext : '用戶名長度不得小於4個字符長度',
								maxlength : 20,
								maxlengthtext : '用戶名的長度不得大於20個字符的長度',
								anchor : '95%'
							}
						}, {
							xtype : 'label',
							html : '<font color="red">*</font>&nbsp;长度限制为4-20小写字母数字'
						}]
			}, {	// 密码信息
						layout : 'column',
						items : [{
									columnwidth : '.4',
									layout : 'form',
									items : {
										fieldlabel : '密码',
										xtype : 'textfield',
										inputtype : 'password',
										allowblank : false,
										blanktext : '密碼不能為空',
										minlength : 6,
										minlengthtext : '密碼長度不得小於6個字符長度',
										name : 'pass',
										id : 'pass',
										anchor : '95%'
									}
								}, {
									columnwidth : '.6',
									layout : 'form',
									xtype : 'label',
									html : '<font color="red">*</font>&nbsp;至少6位,区分大小写'
								}]
					}, {// 重复密码信息
						layout : 'column',
						items : [{
									columnwidth : '.4',
									layout : 'form',
									items : {
										fieldlabel : '重复密码',
										xtype : 'textfield',
										name:'repass',
										inputtype : 'password',
										anchor : '95%',
										vtype : 'password',
										initialpassfield : 'pass'
									}
								}, {
									columnwidth : '.6',
									layout : 'form',
									xtype : 'label',
									html : '<font color="red">*</font>&nbsp;至少6位,区分大小写,并且和初始密码一致'
								}]
					}, {// 邮箱信息
						layout : 'column',
						items : [{
									columnwidth : '.5',
									layout : 'form',
									items : {
										fieldlabel : '邮箱',
										xtype : 'textfield',
										allowblank : false,
										blanktext : 'email地址不能為空',
										vtype : 'email',
										emailtext : '郵箱格式不匹配(user@domain.com)',
										name : 'email',
										id : 'email',
										anchor : '95%'
									}
								},{
									columnwidth : '.11',
									defaulttype : 'checkbox',
									defaults : {
										layout : 'form'
									},
									items : {
										checked : true,
										boxlabel : '不公开',
										name : 'email_pub'
									}
								}, {
									columnwidth : '.05',
									layout : 'form',
									xtype : 'label',
									html : '<font color="red">*</font>&nbsp;'
								}]
					}, {// 确认邮箱信息
						layout : 'column',
						defaults : {
							layout : 'form',
							columnWidth : '.5'
						},
						items : [{
									items : {
										fieldlabel : '确认邮箱',
										xtype : 'textfield',
										name:'reemail',
										vtype : 'emailvalidator',
										initialfield : 'email',
										anchor : '95%'
									}
								}, {
									xtype : 'label',
									html : '<font color="red">*</font>'
								}]
					}]
		}, new Ext.form.FieldSet({
			title : '高级用户设置选项信息',
			autoHeight : true,
			animCollapse :true,
			//collapsible : true,
			collapsed :true,
			checkboxToggle :true,
			items : [{
				layout : 'column',
				defaults : {
					layout : 'form',
					columnwidth : '.5'
				},
				items : [{
					items : {
						fieldlabel : '選擇頭像',
						xtype : 'combo',
						store : image_store,
						mode : 'local',
						emptytext : 'i01.jpg',//請選擇頭像
						forceselection : true,
						selectonfocus : true,
						triggeraction : 'all',
						typeahead : true,
						valuefield : 'image_url',
						displayfield : 'image_name',
						anchor : '95%',
						name : 'imgurl',
						listeners : {
							scope : this,
							change : function(e) {
								field = Ext.get('im');
								field.dom.src = e.value;
								e.blur;
							}
						}
					}
				}, {
					xtype : 'label',
					html : '<img id="im" src="images/i01.jpg" height=32 width=32 />'
				}]
			},{
				xtype :'textfield',
				name :'alias',
				fieldLabel :'昵称'
			},{
				layout : 'column',
				defaults : {
					layout : 'form',
					columnWidth : '.45'
				},
				items :[{
					items:{
						xtype :'textfield',
						name :'truename',
						fieldlabel :'真实姓名'
					}
				},{
					xtype :'checkbox',
					checked : true,
					boxlabel : '不公开',
					name : 'truename_pub'
				}]
			},{
				xtype: 'radiogroup',
        fieldLabel: '性别',
        anchor : '50%',
				items: [
          {boxlabel: '保密', name: 'sex',inputvalue :1, checked: true},
          {boxlabel: '男', name: 'sex',inputvalue :2},
          {boxlabel: '女', name: 'sex',inputvalue :3}
        ]
			},{
				xtype :'combo',
				fieldLabel: '省份',
				store : provice_store,
				mode : 'local',
				emptyText : '江西',
				forceSelection : true,
				selectOnFocus : true,
				triggerAction : 'all',
				typeAhead : true,
				valueField : 'provice_value',
				displayField : 'provice_name',
				anchor : '45%',
				name : 'provice'
			},{
				xtype : 'combo',
				fieldLabel :'行業',
				store : industry_store,
				mode : 'local',
				emptyText : '自由職業',
				forceSelection : true,
				selectOnFocus : true,
				triggerAction : 'all',
				typeAhead : true,
				valueField : 'industry_value',
				displayField : 'industry_name',
				anchor : '45%',
				name : 'work'
			},{
				xtype : 'combo',
				fieldLabel :'最高學歷',
				store :education_store,
				mode : 'local',
				emptyText : '小學',
				forceSelection : true,
				selectOnFocus : true,
				triggerAction : 'all',
				typeAhead : true,
				valueField : 'education_value',
				displayField : 'education_value',
				anchor : '45%',
				name : 'education'
			},{
				xtype:'datefield',
				fieldLabel:'出生日期',
				name:'brithday',
				minValue:'01/01/1900',
				minText:'出生日期不得在1900年01月01日以前',
				maxValue:new Date(),
				maxText:'出生日期错误或者超出范围',
				anchor : '45%'
			},{
				xtype:'textfield',
				fieldLabel:'网站',
				name:'web_link',
				vtype:'url',
				urlText:'网站格式不正确'
			},{
				xtype:'textfield',
				fieldLabel:'邮编',
				name:'post',
				vtype:'postNum',//自定义验证规则
				anchor : '45%'
			},{
				xtype:'textarea',
				name:'sign',
				fieldLabel:'签名',
				anchor : '75%'
			},{
				xtype:'htmleditor',
				name:'introduction',
				fieldLabel:'个人介绍',
				height:200,
				anchor : '95%'
			}]
		})],
		buttons : [{
					text : '提交',
					scope:this,
					handler:function(){
						alert(ext.encode(form.form.getvalues()));
					}
				}, {
					text : '重置',
					handler:function(){
						form.form.reset();
					}
				}]
	})
})

上面代码涉及到的知识包括以下几个方面: 1.form组件的应用,包括有 Checkbox,ComboBox,DateField,FieldSet,HtmlEditor,Label,Radio,TextArea,TextField,FormPanel这些,其中没有用到的有 Hidden,NumberField,TimeField,TriggerField这几个,其中在表单中邮编那里其实可以用NumberField的,但涉及到验证我还是用了textfield并通过扩展Vtype实现数字和长度验证! 2.有用到布局管理,formlayout和columnlayout 3.Vtype验证规则,验证规则很简单,也很容易扩展,其中我有扩张邮编严整,俩次密码相等验证,只要你对正则表达式熟悉的话扩展很方便! 4.store,simplestore,jsonstore都有涉及到,但不是这里的重点 5.相关辅助函数的用法,如 getCmp

总结:熟悉API,特别是对于刚接触的来说尤为重要!英语不好就开着金山词霸一个翻译把,我是这么做的,不过也可以找中文的API文档如果英文实在太烂的话!

源码在下面,要的自己下载,我这里是完全能运行的,并且能完全兼容IE7.0和FF,其他浏览器我没有试过!注意编码问题,要是直接在IDE里面打开JS文件中文乱码的话,就先用记事本打开COPY过去就行!

评论(2)

  1. #2 楼 · y1d2y3xyz · 2010-01-24 15:52

    f117f writes:

    请教一下,楼主你这是ext多少版本的 我在2.2的环境下 Firefox和IE都不能通过!

    我是2.2下开发的,代码肯定没问题的,都通过测试的

  2. #1 楼 · f117f · 2009-12-01 09:31

    请教一下,楼主你这是ext多少版本的 我在2.2的环境下 Firefox和IE都不能通过!

评论功能维护中,当前仅展示历史评论。