码迷,mamicode.com
首页 > Windows程序 > 详细

Ext.js入门:Window对象与FormPanel(六)

时间:2016-09-04 20:43:46      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

一:Ext.Window类
二:Ext.Window类实例
三:Ext.FormPanel类
四:Ext.FormPanel类实例
 
包: Ext 定义的文件
Window.js 类全称: Ext.Window 继承自于: Ext.Panel
说明: 一种专用于程序中的“视窗”(window)的特殊面板。Window默认下是可拖动的draggable、浮动的窗体。窗体可以最大化到整个视图、恢复原来的大小,还可以最小化minimize
     Windows既可关联到Ext.WindowGroup或籍由Ext.WindowManager来管理, 提供分组(grouping),活动(activation),置前/置后(to front/back)或其它应用程序特定的功能。
     缺省状态下,窗体都渲染到document.body。要强制constrain窗体以某个元素为依托就要使用renderTo方法。
 
2.例:
//html代码
<div id="win" class="x-hidden"></div>
//js代码
var w=new Ext.Window({
           el:"win",//主体显示的html元素           

               width:300,
           height:200,
           title:"标题" 
 });
 w.show();

3.参数介绍:

因为前面已经介绍了panel组件,下面只介绍window组件的几个其他特别的配置参数
//几个前面没有介绍的window自己的配置参数
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

//需要显示下show()方法,默认的窗口是可拖动的,可关闭的,可拖动大小的
w.show()
 
4.实例介绍
1.嵌套了tabpanel的window
var w=new Ext.Window({
           contentEl:"win",
           width:300,
           height:200,
           el:“win”,
          minimizable:true,  //最小化
          maximizable:true,  //最大化
           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();
 
2.添加工具栏
// bbar:[{text:“确定”},{text:“取消”,handler:function(){w.close();}}],//bottom部
   buttons:[{text:“确定”},{text:“取消”,handler:function(){w.close();}}],//footer部
   buttonAlign:“center”,//footer部按钮排列位置,这里是中间
// collapsible:true,//右上角的收缩按钮

其他与Panel一样!

 
二:FormPanel
包:Extform 定义的文件: Form.js
类全称:Ext.form.FormPanel
继承自于: Ext.Panel
//查看源代码便知,两种方法是一样的
Ext.form.FormPanel = Ext.FormPanel;
1.实例:
<!--html代码-->
<body>
<div id="form1"></div>
</body>
}//js代码
var form1 = new Ext.form.FormPanel({
       width:350,
       frame:true,//圆角和浅蓝色背景
       renderTo:"form1",//呈现
       defaults:{xtype:“textfield”,width:200},
       title:"FormPanel",
       bodyStyle:"padding:5px 5px 0",   //边距
      items:[
          {
            fieldLabel:"UserName",//文本框标题
           // xtype:"textfield",//表单文本框
            name:"user",
            id:"user",
           // width:200
          },
         {
            fieldLabel:"PassWord",
          //  xtype:"textfield",
           inputType:“password”,//密码显示
            name:"pass",
            id:"pass",
           // width:200
          }
       ],
      buttonAlign:“center”,
       buttons:[{text:"确定"},{text:"取消",handler:function(){alert("事件!");}}]
    });
技术分享
 
关于inputType,参数如下:
//input的常用几种类型
radio
check
text(默认)
file
password等等
关于FormPanel的配置参数,请主要参考panel的参数,这里列举另外两个:
1.labelAlign:fieldlabel的排列位置,默认为"left",其他两个枚举值是"center","right"
2.labelWidth:fieldlabel的占位宽
3.method:"get"或"post"
4.url:"提交的地址"
5.submit:提交函数
 
实例2.FormPanel的fieldset应用
<head runat="server">
    <title></title>
     <link href="Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="Ext/adapter/ext/ext-base.js" type="text/javascript"></script>
    <script src="Ext/ext-all.js" type="text/javascript"></script>
    <script src="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>

技术分享

关于xtype的类型,在extjs的form表单(其他的请参考api)中已经定义的有:
Form components
---------------------------------------
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
 
实例3:可选的fieldset实例
1.checkboxToggle:true//true则呈现一个带checkbox的fieldset,选中则展开,否则相反,默认为false
2.checkboxName:string//当上面为true时,作为checkbox的name,方便表单操作
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <link href="Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="Ext/adapter/ext/ext-base.js" type="text/javascript"></script>
    <script src="Ext/ext-all.js" type="text/javascript"></script>
    <script src="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>

技术分享

 

实例4.表单验证实例(空验证,密码确认验证,email验证)
我们可以用单独的js写表单验证,但是extjs已经为我们想到了(自己单独写反而不方便)。
在验证之前,我不得不提两个小知识点:

//大家在很多的extjs代码中都看到了这两个,他们都起提示作用的
Ext.QuickTips.init();//支持tips提示
Ext.form.Field.prototype.msgTarget=‘side‘;//提示的方式,枚举值为"qtip","title","under","side",id(元素id)
    //side方式用的较多,右边出现红色感叹号,鼠标上去出现错误提示,其他的我就不介绍了,可自行验证
//大家可以分别去掉这两行代码,看效果就会明白他们的作用,(放在onReady的function(){}中)

 
4.1 看一个最简单的例子:空验证
//空验证的两个参数
1.allowBlank:false//false则不能为空,默认为true
2.blankText:string//当为空时的错误提示信息
js代码为:
var form1 = new Ext.form.FormPanel({
   width:350,
       frame:true,
       renderTo:"form1",
       labelWidth:80,
       title:"FormPanel",

4.2vtype格式进行简单的验证。

在此举邮件验证的例子,重写上面代码的items配置:
items:[
               {fieldLabel:"不能为空",
                vtype:"email",//email格式验证
                vtypeText:"不是有效的邮箱地址",//错误提示信息,默认值我就不说了
                id:"blanktest",
                anchor:"90%"
               }

技术分享

 4.3你可以修改上面的vtype为以下的几种extjs的vtype默认支持的验证:

//form验证中vtype的默认支持类型
1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)
2.alphanum//只能输入字母和数字,无法输入其他
3.email//email验证,要求的格式是“langsin@gmail.com”
4.url//url格式验证,要求的格式类似于 

4-4.确认密码验证(高级自定义验证)
前面的验证都是extjs已经提供的验证,我们也可以自定义验证函数,比上面要复杂点了。
我们修改前面的代码:
//先用Ext.apply方法添加自定义的password验证函数(也可以取其他的名字)
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="Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="Ext/adapter/ext/ext-base.js" type="text/javascript"></script>
    <script src="Ext/ext-all.js" type="text/javascript"></script>
    <script src="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简单示例

js代码:
//基本上同上
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参数
   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(六)

标签:

原文地址:http://www.cnblogs.com/sunliyuan/p/5840071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!