Menu

  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay
  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay

ExtJS 4常用组件之表单

13
Mar
2013

ExtJS 4常用组件之表单

By Alex
/ in JavaScript
0 Comments
简介

表单面板(FormPanel)类是HTML表单的容器,ExtJS 4中的表单面板由Fields、 FieldContainer、FieldSet、Label、Action等组件构成。

表单字段(控件)

ExtJS 4引入了Ext.form.field包,所有字段类均存放在该包中。通过下面的代码可以为表单声明若干字段:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
Ext.create( 'Ext.form.Panel', {
    frame : true,
    title : 'Form Fields',
    width : 340,
    bodyPadding : 5,
    //应用到所有字段的默认项
    fieldDefaults : {
        labelAlign : 'left', //字段的标签的位置
        labelWidth : 90, //字段标签的宽度
        anchor : '100%' //占据全部可用宽度
    },
    items : [
        {
            //隐藏字段,不显示给用户,但是会提交,通常用来存放ID
            xtype : 'hiddenfield',
            name : 'hiddenfield1',
            value : 'Hidden field value'
        }, {
            //显示字段,只读信息
            xtype : 'displayfield',
            name : 'displayfield1',
            fieldLabel : 'Display field',
            value : 'Display field'
        }, {
            //单行输入框
            xtype : 'textfield',
            name : 'textfield1',
            fieldLabel : 'Text field',
            value : 'Text field value'
        }, {
            //密码输入框
            xtype : 'textfield',
            name : 'password1',
            inputType : 'password',
            fieldLabel : 'Password field'
        }, {
            //多行文本
            xtype : 'textareafield',
            name : 'textarea1',
            fieldLabel : 'TextArea',
            value : 'Textarea value'
        },
        //下面是若干Ext.form.field.Trigger的子类型,Trigger是Ext.form.field.Text的子类
        //File、Picker、Spinner是Trigger的子类型
        //Picker包含一个按钮,点击后出现一个弹框,可以从中选择字段的值。包括下拉菜单、日期控件等
        //Spinner包含一个可以上下点击的按钮。包括数字字段等
        {
            //文件上传控件
            xtype : 'filefield',
            name : 'file1',
            fieldLabel : 'File upload'
        },
        {
            //时间控件
            xtype : 'timefield',
            name : 'time1',
            fieldLabel : 'Time Field',
            minValue : '8:00 AM', //最小值
            maxValue : '5:00 PM', //最大值
            increment : 30
            //步进
        },
        {
            //日期控件
            xtype : 'datefield',
            name : 'date1',
            fieldLabel : 'Date Field',
            value : new Date()
        },
        {
            //下拉框,需要一个Store来加载数据
            xtype : 'combobox',
            fieldLabel : 'Combobox',
            displayField : 'name',
            store : Ext.create( 'Ext.data.Store', {
                fields : [
                    {
                        type : 'string',
                        name : 'name'
                    }
                ],
                data : [
                    {
                        "name" : "Alabama"
                    }, {
                        "name" : "Alaska"
                    }, {
                        "name" : "Arizona"
                    }, {
                        "name" : "Arkansas"
                    }, {
                        "name" : "California"
                    }
                ]
            } ),
            queryMode : 'local',
            typeAhead : true
        },
        {
            //数字字段
            xtype : 'numberfield',
            name : 'numberfield1',
            fieldLabel : 'Number field',
            value : 20,
            minValue : 0,
            maxValue : 50,
            //下面3个配置导致数字框与文本框看起来一样
            hideTrigger : true, //隐藏触发按钮
            keyNavEnabled : false, //禁止键盘导航
            mouseWheelEnabled : false
            //禁止鼠标中键选值
        },
        //复选框
        {
            xtype : 'checkboxfield',
            name : 'checkbox1',
            fieldLabel : 'Checkbox',
            boxLabel : 'box label'
        },
        //复选框组,两列分布复选框
        {
            xtype : 'checkboxgroup',
            fieldLabel : 'TwoColCheckBoxGroup',
            columns : 2,
            vertical : true,
            items : [
                { boxLabel: 'Item 1', name: 'rb', inputValue: '1' },
                { boxLabel: 'Item 2', name: 'rb', inputValue: '2', checked: true },
                { boxLabel: 'Item 3', name: 'rb', inputValue: '3' },
                { boxLabel: 'Item 4', name: 'rb', inputValue: '4' },
                { boxLabel: 'Item 5', name: 'rb', inputValue: '5' },
                { boxLabel: 'Item 6', name: 'rb', inputValue: '6' }
            ]
        },
        //单选框,是复选框的子类型
        {
            xtype : 'radiofield',
            name : 'radio1',
            value : 'radiovalue1',
            fieldLabel : 'Radio buttons',
            boxLabel : 'radio 1'
        },
        //复选框组,与复选框组类似
        {
            xtype : 'radiogroup',
            fieldLabel : 'TwoColRadioGroup',
            columns : 2,
            vertical : true,
            items : [
                { boxLabel: 'Item 1', name: 'rb', inputValue: '1' },
                { boxLabel: 'Item 2', name: 'rb', inputValue: '2', checked: true},
                { boxLabel: 'Item 3', name: 'rb', inputValue: '3' },
                { boxLabel: 'Item 4', name: 'rb', inputValue: '4' },
                { boxLabel: 'Item 5', name: 'rb', inputValue: '5' },
                { boxLabel: 'Item 6', name: 'rb', inputValue: '6' }
            ]
        },
        {
            //多滑块
            xtype : 'multislider',
            fieldLabel : 'Multi Slider',
            values : [
                25, 50, 75
            ],
            increment : 5,
            minValue : 0,
            maxValue : 100
        },
        {
            //单滑块
            xtype : 'sliderfield',
            fieldLabel : 'Single Slider',
            value : 50,
            increment : 10,
            minValue : 0,
            maxValue : 100
        }
    ]
} );
表单验证

ExtJS 为表单提供了单独的验证机制,与模型的验证机制无关。下面的例子演示了常用的表单验证方式:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Ext.create( 'Ext.form.Panel', {
    frame : true,
    width : 340,
    bodyPadding : 5,
    fieldDefaults : {
        labelAlign : 'left',
        labelWidth : 90,
        anchor : '100%',
        //错误信息显示的位置:side, under, top
        msgTarget : 'under'
    },
    items : [
        {
            xtype : 'textfield',
            name : 'textfield1',
            fieldLabel : 'Required',
            //非空校验
            allowBlank : false
    }   ,
        {
            xtype : 'textfield',
            name : 'textfield2',
            fieldLabel : 'Min 2',
            //最小长度校验
            minLength : 2
        },
        {
            xtype : 'textfield',
            name : 'textfield3',
            fieldLabel : 'Max 5',
            //最大长度校验
            maxLength : 5
        },
        {
            xtype : 'textfield',
            name : 'textfield7',
            fieldLabel : 'Regex - Phone',
            //正则式校验
            regex : /^\d{3}-\d{3}-\d{4}$/,
            regexText : 'Must be in the format xxx-xxx-xxxx'
        },
        //ExtJS提供了一些预置的验证规则,称为vtype
        {
            xtype : 'textfield',
            name : 'textfield4',
            fieldLabel : 'Email',
            //电子邮件验证
            vtype : 'email'
        },
        {
            xtype : 'textfield',
            name : 'textfield5',
            fieldLabel : 'Alpha',
            //仅支持A-Za-z和下划线
            vtype : 'alpha'
        },
        {
            xtype : 'textfield',
            name : 'textfield6',
            fieldLabel : 'AlphaNum',
            //仅支持alpha和数组0-9
            vtype : 'alphanum'
        },
        {
            xtype : 'textfield',
            name : 'textfield6',
            fieldLabel : 'Url',
            //网址验证
            vtype : 'url'
        }
    ]
} );

 实现一个自定义的vtype也很简单:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Ext.apply( Ext.form.field.VTypes, {
    IPAddress : function( v )
    {
        return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test( v );
    },
    //错误信息
    IPAddressText : 'Must be a numeric IP address',
    IPAddressMask : /[\d\.]/i
} );
 
//可以这样使用该验证类型
var field = {
    xtype : 'textfield',
    name : 'textfield8',
    fieldLabel : 'Custom: IP Address',
    vtype : 'IPAddress'
};
标签

不关联字段的单纯标签也是支持的:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
Ext.create( 'Ext.form.Panel', {
    width : 100,
    bodyPadding : 10,
    items : [
        {
            xtype : 'label',
            forId : 'myFieldId',
            text : 'Just a Label',
            margins : '0 0 0 10'
        }
    ]
} );
表单动作(Action)

可以处理两种表单动作(Action):加载数据、提交数据。

下面的表单包含两个按钮,分别用来加载和提交:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Ext.create( 'Ext.form.Panel', {
    width : 300,
    bodyPadding : 5,
    fieldDefaults : {
        labelAlign : 'left',
        labelWidth : 90,
        anchor : '100%'
    },
    items : [
        {
            xtype : 'hiddenfield',
            name : 'bookId'
        }, {
            xtype : 'textfield',
            name : 'bookName',
            fieldLabel : 'Title'
        }, {
            xtype : 'textfield',
            name : 'bookAuthor',
            fieldLabel : 'Author'
        }
    ],
    buttons : [
        {
            text : 'Load',
            handler : function()
            {
                var form = this.up( 'form' ).getForm();
                //通过form.Basic来加载数据到表单
                //期望的数据格式:{ success: true, data: {bookId: 10 ... } ,... }
                form.load( {
                    url : 'data/form.json',
                    failure : function( form, action )
                    {}
                } );
            }
        }, {
            text : 'Submit',
            handler : function()
            {
                var form = this.up( 'form' ).getForm();
                form.submit( {
                    url : 'form-submit.php',
                    //提交表单后显示的信息
                    waitMsg : 'Sending the info...',
                    success : function( fp, o )
                    {}
                } );
            }
        }
    ]
} );
← ExtJS 4常用组件之表格
ExtJS 4常用组件之树面板 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Related Posts

  • CommonJS规范简介
  • HTML5视频监控技术预研
  • 基于AngularJS开发Web应用
  • ExtJS 4中的选取器控件
  • 定制ExtJS 4主题

Recent Posts

  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
  • A Comprehensive Study of Kotlin for Java Developers
  • 背诵营笔记
  • 利用LangChain和语言模型交互
  • 享学营笔记
ABOUT ME

汪震 | Alex Wong

江苏淮安人,现居北京。目前供职于腾讯云,专注容器方向。

GitHub:gmemcc

Git:git.gmem.cc

Email:gmemjunk@gmem.cc@me.com

ABOUT GMEM

绿色记忆是我的个人网站,域名gmem.cc中G是Green的简写,MEM是Memory的简写,CC则是我的小天使彩彩名字的简写。

我在这里记录自己的工作与生活,同时和大家分享一些编程方面的知识。

GMEM HISTORY
v2.00:微风
v1.03:单车旅行
v1.02:夏日版
v1.01:未完成
v0.10:彩虹天堂
v0.01:阳光海岸
MIRROR INFO
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Recent Posts
  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
    In this blog post, I will walk ...
  • A Comprehensive Study of Kotlin for Java Developers
    Introduction Purpose of the Study Understanding the Mo ...
  • 背诵营笔记
    Day 1 Find Your Greatness 原文 Greatness. It’s just ...
  • 利用LangChain和语言模型交互
    LangChain是什么 从名字上可以看出来,LangChain可以用来构建自然语言处理能力的链条。它是一个库 ...
  • 享学营笔记
    Unit 1 At home Lesson 1 In the ...
  • K8S集群跨云迁移
    要将K8S集群从一个云服务商迁移到另外一个,需要解决以下问题: 各种K8S资源的迁移 工作负载所挂载的数 ...
  • Terraform快速参考
    简介 Terraform用于实现基础设施即代码(infrastructure as code)—— 通过代码( ...
  • 草缸2021
    经过四个多月的努力,我的小小荷兰景到达极致了状态。

  • 编写Kubernetes风格的APIServer
    背景 前段时间接到一个需求做一个工具,工具将在K8S中运行。需求很适合用控制器模式实现,很自然的就基于kube ...
  • 记录一次KeyDB缓慢的定位过程
    环境说明 运行环境 这个问题出现在一套搭建在虚拟机上的Kubernetes 1.18集群上。集群有三个节点: ...
  • eBPF学习笔记
    简介 BPF,即Berkeley Packet Filter,是一个古老的网络封包过滤机制。它允许从用户空间注 ...
  • IPVS模式下ClusterIP泄露宿主机端口的问题
    问题 在一个启用了IPVS模式kube-proxy的K8S集群中,运行着一个Docker Registry服务 ...
  • 念爷爷
      今天是爷爷的头七,十二月七日、阴历十月廿三中午,老人家与世长辞。   九月初,回家看望刚动完手术的爸爸,发

  • 6 杨梅坑

  • liuhuashan
    深圳人才公园的网红景点 —— 流花山

  • 1 2020年10月拈花湾

  • 内核缺陷触发的NodePort服务63秒延迟问题
    现象 我们有一个新创建的TKE 1.3.0集群,使用基于Galaxy + Flannel(VXLAN模式)的容 ...
  • Galaxy学习笔记
    简介 Galaxy是TKEStack的一个网络组件,支持为TKE集群提供Overlay/Underlay容器网 ...
TOPLINKS
  • Zitahli's blue 91 people like this
  • 梦中的婚礼 64 people like this
  • 汪静好 61 people like this
  • 那年我一岁 36 people like this
  • 为了爱 28 people like this
  • 小绿彩 26 people like this
  • 彩虹姐姐的笑脸 24 people like this
  • 杨梅坑 6 people like this
  • 亚龙湾之旅 1 people like this
  • 汪昌博 people like this
  • 2013年11月香山 10 people like this
  • 2013年7月秦皇岛 6 people like this
  • 2013年6月蓟县盘山 5 people like this
  • 2013年2月梅花山 2 people like this
  • 2013年淮阴自贡迎春灯会 3 people like this
  • 2012年镇江金山游 1 people like this
  • 2012年徽杭古道 9 people like this
  • 2011年清明节后扬州行 1 people like this
  • 2008年十一云龙公园 5 people like this
  • 2008年之秋忆 7 people like this
  • 老照片 13 people like this
  • 火一样的六月 16 people like this
  • 发黄的相片 3 people like this
  • Cesium学习笔记 90 people like this
  • IntelliJ IDEA知识集锦 59 people like this
  • Bazel学习笔记 38 people like this
  • 基于Kurento搭建WebRTC服务器 38 people like this
  • PhoneGap学习笔记 32 people like this
  • NaCl学习笔记 32 people like this
  • 使用Oracle Java Mission Control监控JVM运行状态 29 people like this
  • Ceph学习笔记 27 people like this
  • 基于Calico的CNI 27 people like this
Tag Cloud
ActiveMQ AspectJ CDT Ceph Chrome CNI Command Cordova Coroutine CXF Cygwin DNS Docker eBPF Eclipse ExtJS F7 FAQ Groovy Hibernate HTTP IntelliJ IO编程 IPVS JacksonJSON JMS JSON JVM K8S kernel LB libvirt Linux知识 Linux编程 LOG Maven MinGW Mock Monitoring Multimedia MVC MySQL netfs Netty Nginx NIO Node.js NoSQL Oracle PDT PHP Redis RPC Scheduler ServiceMesh SNMP Spring SSL svn Tomcat TSDB Ubuntu WebGL WebRTC WebService WebSocket wxWidgets XDebug XML XPath XRM ZooKeeper 亚龙湾 单元测试 学习笔记 实时处理 并发编程 彩姐 性能剖析 性能调优 文本处理 新特性 架构模式 系统编程 网络编程 视频监控 设计模式 远程调试 配置文件 齐塔莉
Recent Comments
  • qg on Istio中的透明代理问题
  • heao on 基于本地gRPC的Go插件系统
  • 黄豆豆 on Ginkgo学习笔记
  • cloud on OpenStack学习笔记
  • 5dragoncon on Cilium学习笔记
  • Archeb on 重温iptables
  • C/C++编程:WebSocketpp(Linux + Clion + boostAsio) – 源码巴士 on 基于C/C++的WebSocket库
  • jerbin on eBPF学习笔记
  • point on Istio中的透明代理问题
  • G on Istio中的透明代理问题
  • 绿色记忆:Go语言单元测试和仿冒 on Ginkgo学习笔记
  • point on Istio中的透明代理问题
  • 【Maven】maven插件开发实战 – IT汇 on Maven插件开发
  • chenlx on eBPF学习笔记
  • Alex on eBPF学习笔记
  • CFC4N on eBPF学习笔记
  • 李运田 on 念爷爷
  • yongman on 记录一次KeyDB缓慢的定位过程
  • Alex on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • haolipeng on 基于本地gRPC的Go插件系统
  • 吴杰 on 基于C/C++的WebSocket库
©2005-2025 Gmem.cc | Powered by WordPress | 京ICP备18007345号-2