ExtJs中的表单提交和页面弹出表单

来源:互联网 发布:如何通过淘宝试用 编辑:程序博客网 时间:2024/05/02 00:24

1.表单提交

代码:

  form.jsp页面:

<formid="panel22"action="getTest.jsp"method="post"></form>

  form.js:

[javascript] view plaincopy
  1.   //创建表单面板  
  2.    var MyformPanel=Ext.create('Ext.form.Panel', {  
  3.                 frame: true,  
  4.                 title: 'FormFields Validation',  
  5.                 width: 340,  
  6.                 bodyPadding: 5,  
  7.                 renderTo:"panel22",    //渲染到页面的form中去  
  8.                 fieldDefaults: {  
  9.                     labelAlign: 'left',  
  10.                     labelWidth: 90,  
  11.                     anchor: '100%',  
  12.                     //错误提示显示在下方,还可以配置为side、title、none  
  13.                     msgTarget: 'under'  
  14.                 },  
  15.                 items:[{  
  16.                     xtype:'fieldset',  
  17.                   title:'用户信息',   //外框的title  
  18.                   collapsible:true,  
  19.                   autoHeight:true,  
  20.                   autoWidth:true,  
  21.                   defaults:{width:150,allowBlank:false,xtype:'textfield'},//提取共同属性  
  22.                 items: [{  
  23.                     xtype: 'textfield',  
  24.                     name: 'textfield1',  
  25.                     fieldLabel: '必须输入',  
  26.                     //不允许为空验证  
  27.                     allowBlank: false //1  
  28.                 }, {  
  29.                     xtype: 'textfield',  
  30.                     name: 'textfield2',  
  31.                     fieldLabel: '最多两个字符',  
  32.                     //输入的字符长度验证(至少输入2个字符)  
  33.                     minLength: 2 //2  
  34.                 }, {  
  35.                     xtype: 'textfield',  
  36.                     name: 'textfield3',  
  37.                     fieldLabel: '最长5个字符',  
  38.                     //输入的字符长度验证(最多输入2个字符)  
  39.                     maxLength: 5 //3  
  40.                 }, {  
  41.                     xtype: 'textfield',  
  42.                     name: 'textfield7',  
  43.                     fieldLabel: '正则表达式验证电话号码',  
  44.                     //通过正则表达式验证  
  45.                     regex: /^\d{3}-\d{3}-\d{4}$/, //4  
  46.                     regexText: 'Must be in the format xxx-xxx-xxxx'  
  47.                 }, {  
  48.                     xtype: 'textfield',  
  49.                     name: 'textfield4',  
  50.                     fieldLabel: '验证用户输入的是否为email',  
  51.                     //已经定义好的验证,请通过文档查看vtype  
  52.                     vtype: 'email' //5  
  53.                 }, {  
  54.                     xtype: 'textfield',  
  55.                     name: 'textfield6',  
  56.                     fieldLabel: '验证用户输入的是否是URL',  
  57.                     vtype: 'url' //8  
  58.                 }]               
  59.                }],  
  60.                buttons:[{text:"确定",handler:function(){  
  61.                //获取按钮的父表单  
  62.                  var form=this.up("form").getForm();  
  63.                  //alert(form);  
  64.                  if(form.isValid())  //判断是否通过验证  
  65.                  {  
  66.                     //获取页面的表单转化为dom对象后提交  
  67.                     Ext.get("panel22").dom.submit();   
  68. //获取页面的表单元素后提交  
  69.                  };  
  70.                  }  
  71.                },{text:"取消",handler:reset}],  
  72.                buttonAlign:'center'  
  73.             });  
  74. //            function logins(){  
  75. //              alert("aaaaa");  
  76. //         MyformPanel.form.submit();//提交  
  77. //              //alert("sdha");  
  78. //         }  
  79.            function reset(){  
  80.             MyformPanel.form.reset();//取消  
  81.            //alert("取消");  
  82.            }  
  83. });  

效果图:


2.页面弹出表单提交数据

 showform.js文件:

[javascript] view plaincopy
  1.  Ext.onReady(function(){  
  2.     //所有的操作定义在函数showform中  
  3.     var showform=function(){  
  4.     var add_winForm =  Ext.create('Ext.form.Panel', {  
  5.                 frame: true,   //frame属性  
  6.                 //title: 'Form Fields',  
  7.                 width: 340,  
  8.                 bodyPadding:5,  
  9.                 //renderTo:"panel21",  
  10.                 fieldDefaults: {  
  11.                     labelAlign: 'left',  
  12.                     labelWidth: 90,  
  13.                     anchor: '100%'  
  14.                 },  
  15.                 items: [{  
  16.                     //隐藏的文本框  
  17.                     xtype: 'hiddenfield'//1  
  18.                     name: 'hiddenfield1',  
  19.                     value: '隐藏的文本框'  
  20.                 }, {  
  21.                     //显示文本框,相当于label  
  22.                     xtype: 'displayfield'//2  
  23.                     name: 'displayfield1',  
  24.                     fieldLabel: 'Display field',  
  25.                     value: '显示文本框'  
  26.                      
  27.                 }, {  
  28.                     //输入文本框  
  29.                     xtype: 'textfield'//3  
  30.                     name: 'textfield1',  
  31.                     fieldLabel: 'Text field',  
  32.                     //value: '输入文本框',  
  33.                     allowBlank: false,  
  34.                     emptyText:'陈建强',  
  35.                     blankText:"提示"  
  36.                 }, {  
  37.                     //输入密码的文本框,输入的字符都会展现为.  
  38.                     xtype: 'textfield'//4  
  39.                     name: 'password1',  
  40.                     inputType: 'password',  
  41.                     fieldLabel: 'Password field'  
  42.                 }, {  
  43.                     //多行文本输入框  
  44.                     xtype: 'textareafield'//5  
  45.                     name: 'textarea1',  
  46.                     fieldLabel: 'TextArea',  
  47.                     id:"areaid",  
  48.                     value: '啦啦啦,我是卖报的小行家'  
  49.                 }, {  
  50.                     //上传文件文本框  
  51.                     xtype: 'filefield'//6  
  52.                     name: 'file1',  
  53.                     fieldLabel: 'File upload'  
  54.                 }, {  
  55.                     //时间文本框  
  56.                     xtype: 'timefield'//7  
  57.                     name: 'time1',  
  58.                     fieldLabel: 'Time Field',  
  59.                     minValue: '8:00 AM',  
  60.                     maxValue: '5:00 PM',  
  61.                     increment: 30  
  62.                 }, {  
  63.                     //日期文本框  
  64.                     xtype: 'datefield'//8  
  65.                     name: 'date1',  
  66.                     fieldLabel: 'Date Field',  
  67.                     value: new Date()  
  68.                 }, {  
  69.                     //下拉列表框  
  70.                     xtype: 'combobox'//9  
  71.                     fieldLabel: 'Combobox',  
  72.                     displayField: 'name',  
  73.                     store: Ext.create('Ext.data.Store', {  
  74.                         fields: [  
  75.                           { type: 'string', name: 'name' }  
  76.                           ],  
  77.                                           data: [  
  78.                           { "name""Alabama" },  
  79.                           { "name""Alaska" },  
  80.                           { "name""Arizona" },  
  81.                           { "name""Arkansas" },  
  82.                           { "name""California" }  
  83.                           ]  
  84.                     }),  
  85.                     queryMode: 'local',  
  86.                     typeAhead: true  
  87.                 }, {  
  88.                     //只能输入数字的文本框  
  89.                     xtype: 'numberfield',  
  90.                     name: 'numberfield1'//10  
  91.                     fieldLabel: 'Number field',  
  92.                     value: 20,  
  93.                     minValue: 0,  
  94.                     maxValue: 50  
  95.                 }, {  
  96.                     //复选框  
  97.                     xtype: 'checkboxfield'//11  
  98.                     name: 'checkbox1',  
  99.                     fieldLabel: 'Checkbox',  
  100.                     boxLabel: '复选框'  
  101.                 }, {  
  102.                     //单选框,注意name和下面的单选框相同  
  103.                     xtype: 'radiofield'//12  
  104.                     name: 'radio1',  
  105.                     value: 'radiovalue1',  
  106.                     fieldLabel: 'Radio buttons',  
  107.                     boxLabel: 'radio 1'  
  108.                 }, {  
  109.                     //单选框,注意name和上面的单选框相同  
  110.                     xtype: 'radiofield'//13  
  111.                     name: 'radio1',  
  112.                     value: 'radiovalue2',  
  113.                     fieldLabel: '',  
  114.                     labelSeparator: '',  
  115.                     hideEmptyLabel: false,  
  116.                     boxLabel: 'radio 2'  
  117.                 }, {  
  118.                     //拖动组件  
  119.                     xtype: 'multislider'//14  
  120.                     fieldLabel: 'Multi Slider',  
  121.                     values: [25, 50, 75],  
  122.                     increment: 5,  
  123.                     minValue: 0,  
  124.                     maxValue: 100  
  125.                 }, {  
  126.                     //拖动组件  
  127.                     xtype: 'sliderfield'//15  
  128.                     fieldLabel: 'Single Slider',  
  129.                     value: 50,  
  130.                     increment: 10,  
  131.                     minValue: 0,  
  132.                     maxValue: 100  
  133.                 }]  
  134. //                ,  
  135. //                buttons:[{text:"确定",handler:function(){  
  136. //                   varform1=this.up("form").getForm();  
  137. //                   //alert(form1);  
  138. //                  Ext.get("panel21").dom.submit();  
  139. //                }},{text:"取消",handler:function(){alert("事件!");}}],  
  140. //                buttonAlign:'center'  
  141.             });  
  142.               
  143.     //alert(add_winForm);  
  144.     //创建window面板,表单面板是依托window面板显示的  
  145.     var syswin = Ext.create('Ext.window.Window',{  
  146.               title : "新建属性",  
  147.               width : 350,  
  148.               //height : 120,  
  149.               //plain : true,  
  150.               iconCls : "addicon",  
  151.               // 不可以随意改变大小  
  152.               resizable : false,  
  153.               // 是否可以拖动  
  154.               // draggable:false,  
  155.               collapsible : true// 允许缩放条  
  156.               closeAction : 'close',  
  157.               closable : true,  
  158.               // 弹出模态窗体  
  159.               modal : 'true',  
  160.               buttonAlign : "center",  
  161.               bodyStyle : "padding:0 0 0 0",  
  162.               items : [add_winForm],  
  163.               buttons : [{  
  164.                          text : "保存",  
  165.                          minWidth : 70,  
  166.                          handler : function() {  
  167.                             if (add_winForm.getForm().isValid()) {  
  168.                                 add_winForm.getForm().submit({  
  169.                                           url :'testshowform.jsp',  
  170.                                            //等待时显示 等待  
  171.                                           waitTitle: '请稍等...',  
  172.                                           waitMsg: '正在提交信息...',  
  173.                                           params: {  
  174.                                               t: "add"  
  175.                                           },  
  176.                                           success: function(fp, o) {  
  177.        //alert(o);success函数,成功提交后,根据返回信息判断情况  
  178.                                               if (o.result== true) {  
  179.     Ext.MessageBox.alert("信息提示","保存成功!");  
  180.                                        syswin.close(); //关闭窗口  
  181.                                                  // Store1.reload();  
  182.                                               }else {  
  183.                                                  msg('信息提示''添加时出现异常!');  
  184.                                               }  
  185.                                           },  
  186.                                           failure: function() {  
  187.                                               msg('信息提示''添加失败!');  
  188.                                           }  
  189.                                        });  
  190.                             }  
  191.                          }  
  192.                      }, {  
  193.                          text : "关闭",  
  194.                          minWidth : 70,  
  195.                          handler : function() {  
  196.                             syswin.close();  
  197.                          }  
  198.                      }]  
  199.            });  
  200.     syswin.show();  
  201.     };  
  202.     showform(); //调用showform显示整个包含表单面板的window面板  
  203. });  

Testshowform.jsp页面:

[html] view plaincopy
  1. <%System.out.println("提交到了测试页面");%>  
  2. <%out.println("true"); %> //返回的“true”在success中的函数o.result中接收到的  

效果截图:


提交过程效果图:

0 0