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

一:Ext.Window类

二:Ext.Window类实例

三:Ext.FormPanel类

四:Ext.FormPanel类实例

1.类Ext.Window

包: 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

类:Ext.form.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.2用vtype格式进行简单的验证。

在此举邮件验证的例子,重写上面代码的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验证,要求的格式是“[email protected]”
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//源代码编辑

时间: 2024-08-06 03:46:19

Ext.js入门:Window对象与FormPanel(六)的相关文章

js中window对象详解以及页面跳转

js中window对象详解以及页面跳转 转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%90%9C/39219.shtml 1.window.top.window.location = "index.asp"; 2.window.top.location.href="index.asp" 3. window.top.location.replace("index.asp");

js中window对象的opener属性的一个坑

2018-05-08 17:48:33 今天我编写js代码时碰到了一个让我纠结了很久的小问题,在此记录一下当做笔记, 这个问题就是:在我自己写的子窗口中用opener属性却获取不到父窗口的window对象. 现在已经解决这个问题,请看下文 这是父窗口(windows.html)的代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lo

JS浏览器window对象、计时器、Screen

一.window对象 二.计时方法setInterval()-间隔指定的毫秒数不停执行指定代码:clearInterval()方法用于停止setInterval()方法执行的函数代码:setTimeout()-暂停指定的毫秒数后执行指定的代码:clearTimeout()-方法用于停止setTimeout()方法执行的函数代码: 三.Screen

JS的Window对象

Window顾名思义就是窗口, JavaScript Window 就是浏览器对象模型 , 作用是使JavaScript 有能力与浏览器"对话". Window 对象 所有浏览器都支持 window 对象.它表示浏览器窗口. 所有 JavaScript 全局对象.函数以及变量均自动成为 window 对象的成员. 全局变量是 window 对象的属性. 全局函数是 window 对象的方法. 甚至 HTML DOM 的 document 也是 window 对象的属性之一: windo

js中Window 对象及其的方法

window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 window 这个前缀. location.hostname 返回 web 主机的域名 location.pathname 返回当前页面的路径和文件名 location.port 返回 web 主机的端口 (80 或 443) location.protocol 返回所使用的 web 协议(http://

Ext.js入门:面板(四)

一:Ext.Panel类简介 1.Ext.Panel类简介 类 Ext.Panel 包: Ext 定义的文件: Panel.js 类全称: Ext.Panel 继承自于: Ext.Container 说明:面板是一种面向用户界面构建应用程序最佳的单元块,一种特定功能和结构化组件. 面板包含有底部和顶部的工具条,连同单独的头部.底部和body部分.它提供内建都得可展开和可闭合的行为, 连同多个内建的可制定的行为的工具按钮.面板可简易地置入任意的容器或布局,而面板和渲染管线(rendering pi

Ext.js入门:常用组件与综合案例(七)

一:datefield简单示例 二:timefield简单示例 三:numberfield简单示例 四:FormPanel提交 datefield简单示例: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="Ext/resources/css/ext-all.css&

JS的window对象详解

一.说明 他是JS中最大的对象,它描述的是一个浏览器窗口,一般要引用他的属性和方法时,不需要用"Window.XXX"这种形式,而是直接使用"XXX".一个框架页面也是一个窗口. 二.Window窗口对象有如下属性 1.name 窗口的名称,由打开它的连接(<a target="...">)或框架页(<frame name="...">)或某一个窗口调用的 open() 方法(见下)决定.一般我们不会用

Ext.js入门:模板(三)

1.Ext.DomHelper简介2.Template语法使用简介3.Template简单应用4.Template中使用转换函数5.使用模板的自定义接口6.XTemplate应用7.复杂模板 1.Ext.DomHelper     处理DOM或模板(Templates)的实用类. 能以JavaScript较清晰地编写HTML片段(HTML fragments)或DOM. 如以下范例产生五个子元素的无须列表,追加到当前元素‘my-div’: 常用的方法: Template语法使用简介   在一些复