首页 > 代码库 > Ext.js入门:Window对象与FormPanel(六)
Ext.js入门:Window对象与FormPanel(六)
Windows既可关联到Ext.WindowGroup或籍由Ext.WindowManager来管理, 提供分组(grouping),活动(activation),置前/置后(to front/back)或其它应用程序特定的功能。
缺省状态下,窗体都渲染到document.body。要强制constrain窗体以某个元素为依托就要使用renderTo方法。
<div id="win" class="x-hidden"></div>
var w=new Ext.Window({
el:"win",//主体显示的html元素
width:300,
height:200,
title:"标题"
});
w.show();
3.参数介绍:
1.closeAction:枚举值为:
close(默认值),当点击关闭后,关闭window窗口
hide,关闭后,只是hidden窗口
2.closable:true在右上角显示小叉叉的关闭按钮,默认为true
3.constrain:true则强制此window控制在viewport,默认为false
4.modal:true为模式窗口,后面的内容都不能操作,默认为false
5.plain://true则主体背景透明,false则主体有小差别的背景色,默认为false
w.show()
contentEl:"win",
width:300,
height:200,
items:new Ext.TabPanel({
activeTab:0,//当前标签为第1个tab(从0开始索引) 设置选项卡的激活状态
border:false,
items:[{title:"tab1",html:"tab1在windows窗口中"},{title:"tab2",html:"tab2在windows窗口中"}]//TabPanel中的标签页,以后再深入讨论
}),
plain:true,//true则主体背景透明,false则主体有小差别的背景色,默认为false
title:"标题"
});
w.show();
buttons:[{text:“确定”},{text:“取消”,handler:function(){w.close();}}],//footer部
buttonAlign:“center”,//footer部按钮排列位置,这里是中间
// collapsible:true,//右上角的收缩按钮
其他与Panel一样!
Ext.form.FormPanel = Ext.FormPanel;
<body>
<div id="form1"></div>
</body>
var form1 = new Ext.form.FormPanel({
width:350,
frame:true,//圆角和浅蓝色背景
renderTo:"form1",//呈现
title:"FormPanel",
bodyStyle:"padding:5px 5px 0", //边距
fieldLabel:"UserName",//文本框标题
// xtype:"textfield",//表单文本框
name:"user",
id:"user",
// width:200
},
fieldLabel:"PassWord",
// xtype:"textfield",
name:"pass",
id:"pass",
// width:200
}
],
buttons:[{text:"确定"},{text:"取消",handler:function(){alert("事件!");}}]
});
check
text(默认)
file
password等等
2.labelWidth:fieldlabel的占位宽
3.method:"get"或"post"
4.url:"提交的地址"
5.submit:提交函数
<head runat="server"> <title></title> <link href="http://www.mamicode.com/Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> <script src="http://www.mamicode.com/Ext/adapter/ext/ext-base.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-all.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-lang-zh_CN.js" type="text/javascript"></script> <script type="text/javascript"> Ext.onReady(function() { var frmLogin = new Ext.form.FormPanel({ title: "登录", width: 350, frame: true, bodyStyle:"padding:10px 10px 0 0", renderTo: "contaner", items:[{ xtype:"fieldset", title: ‘个人信息‘, collapsible: true, autoHeight:true, width:330, defaults: { width: 150 }, defaultType: "textfield", items:[ { fieldLabel: ‘爱好‘, name: ‘hobby‘, value: ‘上网、打游戏‘ }, { xtype:"combo", name:"sex", store:["男","女","保密"], fieldLabel:"性别", emptyText:"请选择性别" } ] }], buttonAlign:"center", buttons: [{ text: "登录" }, { text: "退出", handler: function() { frmLogin.hide(); } }] }); }); </script></head><body> <div id="contaner"></div></body></html>
---------------------------------------
form Ext.FormPanel
checkbox Ext.form.Checkbox
combo Ext.form.ComboBox
datefield Ext.form.DateField
field Ext.form.Field
fieldset Ext.form.FieldSet
hidden Ext.form.Hidden
htmleditor Ext.form.HtmlEditor
label Ext.form.Label
numberfield Ext.form.NumberField
radio Ext.form.Radio
textarea Ext.form.TextArea
textfield Ext.form.TextField
timefield Ext.form.TimeField
trigger Ext.form.TriggerField
2.checkboxName:string//当上面为true时,作为checkbox的name,方便表单操作
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <link href="http://www.mamicode.com/Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> <script src="http://www.mamicode.com/Ext/adapter/ext/ext-base.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-all.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-lang-zh_CN.js" type="text/javascript"></script> <script type="text/javascript"> Ext.onReady(function() { Ext.QuickTips.init(); //支持tips提示 Ext.form.Field.prototype.msgTarget = ‘side‘; //提示的方式 Ext.apply(Ext.form.VTypes, { password: function(val, field) { if (field.confirmTo) { var pwd = Ext.get(field.confirmTo); return (val == pwd.getValue()); } return true; } }); var frmLogin = new Ext.form.FormPanel({ title: "登录", width: 350, frame: true, bodyStyle: "padding:10px 10px 0 0", renderTo: "contaner", items: [{ xtype: "fieldset", title: ‘个人信息‘, collapsible: true, autoHeight: true, width: 330, defaults: { width: 150 }, defaultType: "textfield", items: [ { fieldLabel: ‘爱好‘, name: ‘hobby‘, value: ‘上网、打游戏‘ }, { xtype: "combo", name: "sex", store: ["男", "女", "保密"], fieldLabel: "性别", emptyText: "请选择性别" } ] },{ xtype: "fieldset", checkboxToggle: true, //有checkbox的选择框 checkboxName: "chkinfo",
autoHeight:true,//使自适应展开排版 title: "选填信息", defaultType: "textfield", width: 330, autoHeight: true, items: [ { fieldLabel: "UserName", name: "user", anchor: "95%", vtype: "email", //email格式验证 vtypeText: "不是有效的邮箱地址" }, { fieldLabel: "PassWord", name: "pass", id: "pass", inputType: "password", anchor: "95%", //对应整个的宽度 剩下的宽度的95%,留下5%作为后面提到的验证错误提示 allowBlank: false, blankText: "密码不允许为空!" }, { fieldLabel: "ConfirmPass", id: "confirmpass", name: "confirmpass", vtype: "password", vtypeText: "两次密码输入不一致!", confirmTo: "pass", inputType:"password", anchor: "95%" } ]}], buttonAlign: "center", buttons: [{ text: "登录" }, { text: "退出", handler: function() { frmLogin.hide(); } }] }); }); </script></head><body> <div id="contaner"></div></body></html>
我们可以用单独的js写表单验证,但是extjs已经为我们想到了(自己单独写反而不方便)。
在验证之前,我不得不提两个小知识点:
//大家在很多的extjs代码中都看到了这两个,他们都起提示作用的
Ext.QuickTips.init();//支持tips提示
Ext.form.Field.prototype.msgTarget=‘side‘;//提示的方式,枚举值为"qtip","title","under","side",id(元素id)
//side方式用的较多,右边出现红色感叹号,鼠标上去出现错误提示,其他的我就不介绍了,可自行验证
//大家可以分别去掉这两行代码,看效果就会明白他们的作用,(放在onReady的function(){}中)
1.allowBlank:false//false则不能为空,默认为true
2.blankText:string//当为空时的错误提示信息
var form1 = new Ext.form.FormPanel({ width:350, frame:true, renderTo:"form1", labelWidth:80, title:"FormPanel",
4.2用vtype格式进行简单的验证。
items:[ {fieldLabel:"不能为空", vtype:"email",//email格式验证 vtypeText:"不是有效的邮箱地址",//错误提示信息,默认值我就不说了 id:"blanktest", anchor:"90%" }
//form验证中vtype的默认支持类型
1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)
2.alphanum//只能输入字母和数字,无法输入其他
3.email//email验证,要求的格式是“langsin@gmail.com”
4.url//url格式验证,要求的格式类似于
Ext.apply(Ext.form.VTypes,{ password:function(val,field){//val指这里的文本框值,field指这个文本框组件,大家要明白这个意思 if(field.confirmTo){//confirmTo是我们自定义的配置参数,一般用来保存另外的组件的id值 var pwd=Ext.get(field.confirmTo);//取得confirmTo的那个id的值 return (val==pwd.getValue()); } return true; }});
5.Checkbox 和Radio简单示例
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <link href="http://www.mamicode.com/Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> <script src="http://www.mamicode.com/Ext/adapter/ext/ext-base.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-all.js" type="text/javascript"></script> <script src="http://www.mamicode.com/Ext/ext-lang-zh_CN.js" type="text/javascript"></script> <script type="text/javascript"> Ext.onReady(function() { var myform = new Ext.FormPanel({ frame: true, width: 500, layout: "form", labelWidth: 30, title: "checkBox示例", labelAlign: "left", renderTo: Ext.getBody(), items: [{ xtype: "panel", layout: "column", fieldLabel: "爱好", items: [ { columnWidth:.2, xtype:"checkbox", boxLabel:"足球" }, { columnWidth: .2, xtype: "checkbox", boxLabel: "篮球" } ] }, { xtype: "panel", layout: "column", fieldLabel: "性别", items: [ { columnWidth:.2, xtype:"radio", boxLabel:"男", name:"sex" }, { columnWidth: .2, xtype: "radio", boxLabel: "女", name:"sex" } ] }, { xtype: "panel", layout: "form", labelWidth: 50, height: 100, labelAlign:"top", items: [ { xtype:"htmleditor", fieldLabel:"备注", anchor:"99%" } ] } ] } ); }) </script></head><body> <form id="form1" runat="server"> <div> </div> </form></body></html>
1.checked:true//true则选中,默认为false
2.name:"**"//name值
3.value:""//初始化值,默认为undefine
6.htmleditor简单示例
Ext.onReady(function(){
Ext.QuickTips.init();
var myform=new Ext.FormPanel({
frame:true,
width:600,
layout:"form",
labelWidth:50,
title:"htmleditor简单示例",
labelAlign:"top",//items中的标签的位置
renderTo:Ext.getBody(),
items:[{
xtype:"htmleditor",
id:"he",
fieldLabel:"编辑器",
anchor:"99%"
}]
);
labelAlign:此参数是指form表单中items各项的label位置,默认值为left,枚举值有left,right,top
1.hideLabel:true//默认为false,还适用于有标签的所有表单组件
//下面的一组参数控制编辑器的工具栏选项,都是默认值为true
2.enableColors:true//默认为true,显示字体颜色,字体背景颜色
3.enableAlignments:true//左,中,右对齐
4.enableFont:true//字体
5.enableFontSize:false//字体大小,就是A旁边有个小箭头的
6.enableFormat:false//粗体,斜体,下划线
7.enableLinks:true//链接
8.enableLists:true//列表
9.enableSourceEdit:true//源代码编辑
Ext.js入门:Window对象与FormPanel(六)