---
title: "Ext form"
author: "thw"
date: "2010-12-31T17:21:44Z"
tags: ["Ajax", "C", "C++", "EXT", "json"]
categories: ["Web前端"]
slug: "ext-form"
draft: false
lastmod: "2011-01-20T15:18:49Z"
id: 857084
url: "https://thw.iteye.com/blog/857084"
---

Ext中创建Form有两种方式，一种是使用Ext.form.BaseForm，另一种是使用Ext.form.FormPanel。  
 1 使用Ext.form.BaseForm封装HTML原生的form：  
 jsp页面：  
&lt;form  id="queryForm" action="" &gt;  
 &lt;table cellpadding="2" cellspacing="1" &gt;  
  &lt;tr&gt;  
   &lt;td&gt;&nbsp;物料编码：&lt;/td&gt;  
   &lt;td&gt;&lt;input type="text" id="marcNo" style="width: 180px;"&gt;&lt;/td&gt;  
   &lt;td&gt;&nbsp;品名：&lt;/td&gt;  
   &lt;td&gt;&lt;input type="text" id="marcName" style="width: 100px"&gt;&lt;/td&gt;  
   &lt;td&gt;&nbsp;创建时间：&lt;/td&gt;  
   &lt;td&gt;&lt;input id="startDate" type="text" name="startDate" style="width: 85px" &gt;&lt;/td&gt;  
   &lt;td style="text-align: center;"&gt;至&lt;/td&gt;  
   &lt;td style="text-align: left;"&gt;&lt;input id="endDate" type="text" name="endDate" style="width: 85px" &gt;&lt;/td&gt;  
  &lt;/tr&gt;  
  &lt;tr&gt;  
   &lt;td&gt;&nbsp;物料描述：&lt;/td&gt;  
   &lt;td&gt;&lt;input type="text"  id="marcDesc" style="width: 180px"&gt;&lt;/td&gt;  
   &lt;td&gt;&nbsp;状态：&lt;/td&gt;  
   &lt;td&gt;  
    &lt;select id="statusRmk" name="statusRmk"&gt;  
     &lt;option value=""&gt;请选择&lt;/option&gt;  
     &lt;option value="1"&gt;正常使用&lt;/option&gt;  
     &lt;option value="2"&gt;冻结状态&lt;/option&gt;  
     &lt;option value="3"&gt;删除&lt;/option&gt;  
    &lt;/select&gt;  
   &lt;/td&gt;  
   &lt;td&gt;&nbsp;类别：&lt;/td&gt;  
   &lt;td&gt;  
    &lt;select id="marcKind" name="marcKind"&gt;  
     &lt;option value=""&gt;请选择&lt;/option&gt;  
     &lt;option value="1" &gt;类别1&lt;/option&gt;  
     &lt;option value="2" &gt;类别2&lt;/option&gt;  
     &lt;option value="3" &gt;类别3&lt;/option&gt;  
    &lt;/select&gt;  
   &lt;/td&gt;  
   &lt;td colspan="2" style="text-align: right;padding-left: 5px;"&gt;  
    &lt;div id="toolBar"&gt;&lt;/div&gt;  
   &lt;/td&gt;  
  &lt;/tr&gt;  
 &lt;/table&gt;   
&lt;/form&gt;   
 js代码  
&lt;script&gt;  
 var queryForm,marcNo,marcDesc,marcName,statusRmk,marcKind,startDate,endDate;  
 /\**  
 * 初始化ext Form  
 \*/  
 function initForm(){  
  //物料编码    
  marcNo = new Ext.form.TextField({  
      applyTo: 'marcNo',  
      name: 'marcNo',  
      allowBlank: true,  
      maxLength: 20,  
      width: 180,  
      enableKeyEvents: true,  
      listeners: {  
       keyup: function(diviNo,e){  
        marcNo.setValue(marcNo.getValue().toUpperCase());  
       }  
      }  
     });  
     //物料描述  
  marcDesc = new Ext.form.TextField({  
      applyTo: 'marcDesc',  
      name: 'marcDesc',  
      allowBlank: true,  
      maxLength: 100,  
      width: 180  
  });  
   //品名  
  marcName = new Ext.form.TextField({  
      applyTo: 'marcName',  
      name: 'marcName',  
      allowBlank: true,  
      maxLength: 100,  
      width: 100  
     });  
   //状态  
  statusRmk = new Ext.form.ComboBox({  
   transform: 'statusRmk',  
   name: 'statusRmk',  
   hiddenName: 'statusRmk',  
         typeAhead: true,  
         mode: 'local',  
         editable: false,  
         allowBlank: true,  
         triggerAction: 'all',  
         selectOnFocus: true,  
         width: 100  
  });  
  //类别  
  marcKind = new Ext.form.ComboBox({  
   transform: 'marcKind',  
   name: 'marcKind',  
   hiddenName: 'marcKind',  
         typeAhead: true,  
         mode: 'local',  
         editable: false,  
         allowBlank: true,  
         triggerAction: 'all',  
         selectOnFocus: true,  
         width: 85  
  });  
     //时间段-开始时间  
     startDate = new Ext.form.DateField({  
      id: 'startDate',  
      applyTo: 'startDate',  
      name: 'startDate',  
      allowBlank: true,  
      width: 85,  
      format: 'Y/m/d',  
      invalidText: '{0}不符合{1}的格式要求!',  
      vtype: 'daterange',  
      endDateField: 'endDate',  
      altFormats: 'Y/m/d'  
     });  
     //时间段-结束时间  
     endDate = new Ext.form.DateField({  
      id: 'endDate',  
      applyTo: 'endDate',  
      name: 'endDate',  
      allowBlank: true,  
      width: 85,  
      format: 'Y/m/d',  
      invalidText: '{0}不符合{1}的格式要求!',  
      listeners: {  
         
      },  
      vtype: 'daterange',  
      startDateField: 'startDate',  
      altFormats: 'Y/m/d'  
     });  
       
     queryForm = new Ext.form.BasicForm('queryForm'); //创建Form对象  
  queryForm.add(marcNo,marcDesc,marcName,statusRmk,marcKind,startDate,endDate); //添加元素  
 }  
&lt;/script&gt;  
 2 使用Ext.form.FormPanel：  
 jsp代码：  
 &lt;!-- 用户类型 --&gt;  
 &lt;select id="userKind" name="userKind"&gt;  
  &lt;erpit:options key="USER\_KIND" selectVal="${wuser.userKind}"&gt;&lt;/erpit:options&gt;  
 &lt;/select&gt;  
 js代码  
 //制定form表单各个子项  
    
 var userId=new Ext.form.Hidden({  
      id : 'userId',  
      name:'userId',  
      value:'&lt;c:out value="${wuser.id}" /&gt;'  
     });  
 var userNo=new Ext.form.TextField({  
      id : 'userNo',  
      name:'userNo',  
      fieldLabel :'帐    号',  
      value:'&lt;c:out value="${wuser.userNo}" /&gt;',  
         allowBlank: false,  
         maxLength:20,  
      listeners :{  
       blur :function(userNo){    
        userNo.setValue(userNo.getValue().toUpperCase());  
       }  
      }  
     });

 var userPassword=new Ext.form.TextField({  
      id : 'userPassword',  
      name:'userPassword',  
      fieldLabel :'密    码'  
     });  
       
 var email=new Ext.form.TextField({  
      id : 'email',  
      name:'email',  
      fieldLabel :'电子邮件',  
         allowBlank: false,  
         value:'&lt;c:out value="${wuser.email}" /&gt;',  
         maxLength:20,  
      vtype:'email'  
     });  
       
 var memo=new Ext.form.TextArea({  
      id : 'memo',  
      name:'memo',  
      fieldLabel :'备    注',  
      value:'&lt;c:out value="${wuser.memo}" /&gt;',  
      width:400,  
      height:100  
     });  
       
 var userKind=new Ext.form.ComboBox({  
  transform:'userKind',  
  id:'userKind',  
  valueField:'no',  
  fieldLabel :'用户类别',  
                displayField:'desc',  
                typeAhead: true,  
             mode: 'local',  
             editable: true,  
             allowBlank: false,  
             triggerAction: 'all',  
             selectOnFocus: true  
     });

 //制定form表单   
 var centerForm = new Ext.form.FormPanel({  
      id:'centerForm',  
      frame:true,  
      labelAlign:'right',  
      layout:'form',  
      labelWidth:80,  
      items:\[userId,userNo,perNo,userName,userPassword,email,tel,userKind,memo]  
     });

 

 

3 Ext form加载Record中的数据：form.loadRecord(record);采用此方法可以实现grid行中的数据与form关联。

 

4 Ext form提交

（1）采用Ext.Ajax.request提交

                       Ext.Ajax.request({  
                            url: '&lt;c:out value="${path}" /&gt;/wcolm.htm?method=saveData',  
                            params: {  
                                colId :colmId.getValue(), //Ext form组件  
                                detailData:Ext.encode(dataArr)  
                            },  
                            success: function(response, options){  
                                var rtnObj = Ext.util.JSON.decode(response.responseText);  
                                if(rtnObj.success){    
                                    yyExt.info('系統提示',"资料保存成功!");  
                                }else{  
                                    yyExt.alert("系统提示","保存资料出错,"+unescape(rtnObj.info),"ERROR");  
                                }  
                            },  
                            failure: function(response, options){  
                                var rtnObj = Ext.util.JSON.decode(response.responseText);  
                                yyExt.alert("系统提示","保存资料出错,"+unescape(rtnObj.info),"ERROR");  
                                return false;  
                            }                        
                        });

（2）采用Ext BaseForm的 submit方法

                if(centerForm.isValid()){  
                    centerForm.submit({  
                        method:"post",  
                        url: '&lt;c:out value="${path}" /&gt;/user.htm?method=userSave',  
                        params:{  
                            'data':Ext.encode(centerForm.getValues()), //这里后台取值 需要解析datajson字符串  
                            'diviIds':indexFun.getAllDiviId(),  
                            'roleIds':indexFun.getAllRoleId()  
                        },  
                        waitMsg:'Loading...',  
                        failure:function(form,action){  
                            if(action.result==null){  
                                Ext.Msg.alert('提示',"保存失败");  
                            }else{  
                                Ext.Msg.alert('保存失败',action.result.info);  
                            }  
                        },  
                        success: function(form,action){  
                            yyExt.info("提示","操作成功");  
                        }  
                    });  
                }