`

Extjs提交数据

    博客分类:
  • ext
阅读更多

Ext JS FormPanel 提交数据总结....

文章分类:Web前端

    今天研究FormPanel提交表单数据研究了半天.. 终于把表单提交成功了... 趁现在还记得问题,做一下总结:

 

1. 实用FormPanel如何提交表单:

    在Ext中FormPanel并中并不保存表单数据,其中的数据是由BasicForm保存,在提交表单的时候需要获取当前FormPanel中的BasicForm来进行提交. 

              获取FormPanel中的BasicForm对象代码如下:

   

Js代码 复制代码
  1. var pnlLogin = new Ext.FormPanel({   
  2.     //省略   
  3. });   
  4.   
  5. //获取BasicForm对象   
  6. pnlLogin.getForm();  
var pnlLogin = new Ext.FormPanel({
    //省略
});

//获取BasicForm对象
pnlLogin.getForm();

 

 在获取BasicForm对象后便可进行表单的提交操作...  此时需要查一下BasicForm 的API文档, 文档中说,需要调用submit();方法进行提交:

BasicForm submit方法API 写道
submit( Object options ) : BasicForm

Shortcut to do a submit action.

Parameters:

* options : Object
The options to pass to the action (see doAction for details)

Returns:

* BasicForm
this

 由API文档可以知道,submit方法实际上是调用了BasicForm的doAction()方法, 而doAction放法在API文档中的描述如下:

   

BasicForm doAction() API 写道
doAction( String/Object actionName, [Object options] ) : BasicForm

Performs a predefined action (Ext.form.Action.Submit or Ext.form.Action.Load) or a custom extension of Ext.form.Action to perform application-specific processing.

Parameters:

* actionName : String/Object
The name of the predefined action type, or instance of Ext.form.Action to perform.
* options : Object
(optional) The options to pass to the Ext.form.Action. All of the config options listed below are supported by both the submit and load actions unless otherwise noted (custom actions could also accept other config options):
o url : String

The url for the action (defaults to the form's url.)
o method : String

The form method to use (defaults to the form's method, or POST if not defined)
o params : String/Object

The params to pass (defaults to the form's baseParams, or none if not defined)
o headers : Object

Request headers to set for the action (defaults to the form's default headers)
o success : Function

The callback that will be invoked after a successful response. Note that this is HTTP success (the transaction was sent and received correctly), but the resulting response data can still contain data errors. The function is passed the following parameters:
+ form : Ext.form.BasicForm
The form that requested the action
+ action : Ext.form.Action
The Action class. The result property of this object may be examined to perform custom postprocessing.

o failure : Function

The callback that will be invoked after a failed transaction attempt. Note that this is HTTP failure, which means a non-successful HTTP code was returned from the server. The function is passed the following parameters:
+ form : Ext.form.BasicForm
The form that requested the action
+ action : Ext.form.Action
The Action class. If an Ajax error ocurred, the failure type will be in failureType. The result property of this object may be examined to perform custom postprocessing.

o scope : Object

The scope in which to call the callback functions (The this reference for the callback functions).
o clientValidation : Boolean

Submit Action only. Determines whether a Form's fields are validated in a final call to isValid prior to submission. Set to false to prevent this. If undefined, pre-submission field validation is performed.

Returns:

* BasicForm
this

 

这里actionName只能是 loadsubmit 当然提交的时候使用submit...

 

看了这么多的API文档, 其实关于表单提交操作并没有结束, 从doAction方法的描述中可以看出.. 这里实际上是调用了Ext.form.Action这个类, 而submit操作是调用了该类的子类Ext.form.Action.Submit...  绕了一大圈,终于把Ext中FormPanel是如何提交表单的原理搞的差不多了.. 那么下来就可以上代码了:

    

Java代码 复制代码
  1. var winLogin = new Ext.Window({   
  2.     title:'登录',   
  3.     renderTo:Ext.getBody(),   
  4.     width:350,   
  5.     bodyStyle:'padding:15px;',   
  6.     id:'login-win',   
  7.     buttonAlign:'center',   
  8.     modal:true,   
  9.     items:[{   
  10.         xtype:'form',   
  11.         defaultType:'textfield',   
  12.         bodyStyle : 'padding:5px',   
  13.         baseCls : 'x-plaints',   
  14.         url:'ajaxLogin.do',   
  15.         method:'POST',   
  16.         defaults:{   
  17.             anchor:'95%',   
  18.             allowBlank:false  
  19.         },   
  20.         items:[{   
  21.             id:'loginName',   
  22.             name:'loginName',   
  23.             fieldLabel:'用户名',   
  24.             emptyText:'请输入用户名',   
  25.             blankText:'用户名不能为空'  
  26.         },{   
  27.             id:'password',   
  28.             name:'password',   
  29.             fieldLabel:'密码',   
  30.             blankText:'密码不能为空'  
  31.         }]                             
  32.     }],   
  33.     buttons:[{   
  34.         text:'登录',   
  35.         handler:function(){   
  36.             //获取表单对象   
  37.             var loginForm = this.ownerCt.findByType('form')[0].getForm();   
  38.             alert(loginForm.getValues().loginName);   
  39.             loginForm.doAction('submit', {   
  40.                 url:'ajaxLogin.do',   
  41.                 method:'POST',                         
  42.                 waitMsg:'正在登陆...',   
  43.                 timeout:10000,//10秒超时,   
  44.                 <SPAN style="COLOR: #ff0000">params:loginForm.getValues(),//获取表单数据</SPAN>   
  45.                 success:function(form, action){   
  46.                     var isSuc = action.result.success;   
  47.                     if(isSuc) {   
  48.                         //提示用户登陆成功   
  49.                         Ext.Msg.alert('消息''登陆成功..');   
  50.                     }                                          
  51.                 },   
  52.                 failure:function(form, action){   
  53.                     alert('登陆失败');   
  54.                 }   
  55.             });   
  56.             this.ownerCt.close();   
  57.         }   
  58.     }, {   
  59.         text:'重置',   
  60.         handler:function(){   
  61.             alert('reset');   
  62.             this.ownerCt.findByType('form')[0].getForm().reset();   
  63.         }   
  64.     }]                         
  65. });   
  66. winLogin.show();  
var winLogin = new Ext.Window({
	title:'登录',
	renderTo:Ext.getBody(),
	width:350,
	bodyStyle:'padding:15px;',
	id:'login-win',
	buttonAlign:'center',
	modal:true,
	items:[{
		xtype:'form',
		defaultType:'textfield',
		bodyStyle : 'padding:5px',
		baseCls : 'x-plaints',
		url:'ajaxLogin.do',
		method:'POST',
		defaults:{
			anchor:'95%',
			allowBlank:false
		},
		items:[{
			id:'loginName',
			name:'loginName',
			fieldLabel:'用户名',
			emptyText:'请输入用户名',
			blankText:'用户名不能为空'
		},{
			id:'password',
			name:'password',
			fieldLabel:'密码',
			blankText:'密码不能为空'
		}]							
	}],
	buttons:[{
		text:'登录',
		handler:function(){
			//获取表单对象
			var loginForm = this.ownerCt.findByType('form')[0].getForm();
			alert(loginForm.getValues().loginName);
			loginForm.doAction('submit', {
				url:'ajaxLogin.do',
				method:'POST',						
				waitMsg:'正在登陆...',
				timeout:10000,//10秒超时,
				params:loginForm.getValues(),//获取表单数据
				success:function(form, action){
					var isSuc = action.result.success;
					if(isSuc) {
						//提示用户登陆成功
						Ext.Msg.alert('消息', '登陆成功..');
					}										
				},
				failure:function(form, action){
					alert('登陆失败');
				}
			});
			this.ownerCt.close();
		}
	}, {
		text:'重置',
		handler:function(){
			alert('reset');
			this.ownerCt.findByType('form')[0].getForm().reset();
		}
	}]						
});
winLogin.show();

 注意红色的部分...  这里是得到BaiscForm中所有表单元素中的值,并且已String/Object键值对的形式保存。。 该方法在api文档中的描述如下:

BasicForm getValues API 写道
getValues( [Boolean asString] ) : String/Object

Returns the fields in this form as an object with key/value pairs as they would be submitted using a standard form submit. If multiple fields exist with the same name they are returned as an array.
Parameters:

* asString : Boolean
(optional) false to return the values as an object (defaults to returning as a string)

Returns:

* String/Object

 如此提交解决了提交表单时无法发送数据的问题.... 

到这里终于解决了 如何提交表单的问题...

 

2. 为什么没有执行submit中的success方法, failure方法是在什么时候会被执行..

    这里还是需要 查Action类中的success属性的API文档描述...

Action success属性 API 写道
success : Function

The function to call when a valid success return packet is recieved. The function is passed the following parameters:

* form : Ext.form.BasicForm
The form that requested the action
* action : Ext.form.Action
The Action class. The result property of this object may be examined to perform custom postprocessing.

 这里 success方法需要两个参数, 尤其是第二个参数的描述: 尤其result, 这里是可以点击的

点击后随即跳到了Action result属性的描述: 

Action result属性 API 写道
result : Object

The decoded response object containing a boolean success property and other, action-specific properties.

 有此描述可知,服务器返回的响应中需要包含一个 boolean 型的 success 字段, 该字段会保存在result中,Action会通过获取对该字段的描述 来判断是否执行 success 方法。。

    那么服务器如何返回boolean型的success字段呢?   服务器段部分代码如下:

Java代码 复制代码
  1. try {   
  2.     //返回成功标识   
  3.     <SPAN style="COLOR: #ff0000">response.getWriter().println("{success:true}");</SPAN>   
  4.     response.getWriter().flush();   
  5. catch (IOException e) {   
  6.     e.printStackTrace();   
  7. finally {   
  8.     try {   
  9.         response.getWriter().close();   
  10.     } catch (IOException e) {   
  11.         e.printStackTrace();   
  12.     }   
  13. }  
try {
	//返回成功标识
	response.getWriter().println("{success:true}");
	response.getWriter().flush();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		response.getWriter().close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 

就这些东西让我研究了一下午...  实在头大...   泡杯茶  休息会先....

分享到:
评论

相关推荐

    ExtJs实现数据加载和提交经典代码

    在使用使用FormPanel时我们通常需要使用它的form对象来加载数据或提交数据。FormPanel中的 form对象为Ext.form.BasicForm类型的对象,它有load和submit方法分别用于加载数据和提交数据。而这两个方法都是通过调用 ...

    extjs表单提交例子

    保存提交代码,extjs4.0 // 重置 和 保存 按钮. buttons: [{ text: '重置', handler: function() { this.up('form').getForm().reset(); } }, { text: '保存', /*formBind: true, //only enabled once the ...

    extjs_php向后台提交json_post的接收方法实例

    extjs_php向后台提交json_post的接收方法实例 php接口json数据用$_post[]方法无效时使用

    EXTJS的COMBOBOX级联实现和数据提交VALUE[文].pdf

    EXTJS的COMBOBOX级联实现和数据提交VALUE[文].pdf

    extjs开发平台.rar

    本平台是经本人对...数据格式采用json格式上传下载,实现前端表格提交数据后台可能相应的更新数据的功能。本人水平有限,有不足的地方,请多包涵,本产品仅供学习,需要实际开发应用还有些bug,需要自己去改。

    ExtJS4中文教程2 开发笔记 chm

    Extjs4.0动态填充combobox数据 Extjs4中up()和down()的用法 ExtJS4学习笔记(一)---window的创建 ExtJS4学习笔记(七)---带搜索的Grid(SearchGrid) ExtJS4学习笔记(三)---VBox的使用 ExtJS4学习笔记(九)---ExtJS4 ...

    ExtJs4.1 treegrid CRUD 读取、新增、修改、删除

    实现treegrid组件的(CRUD)读取、新增、修改、删除 //设置grid单元格处于编辑状态 ...//异步Ajax提交新增数据 insertdb:function(newrecords) //异步Ajax提交修改数据 updatedb:function(updaterecords)

    extjs中的xtype的所有类型介绍

    17. displayfield - xtype: 'displayfield', 描述: 仅显示,不校验/不被提交的文本框 18. radiogroup - xtype: 'radiogroup', 描述: 编组的单选按钮(Since 2.2) 图表组件 1. chart - xtype: 'chart', 描述: 图表...

    轻松搞定Extjs_原创

    三、提交表单至服务器 97 四、小结 100 第十六章:更多表单组件 102 一、您能说出哪些表单组件呢? 102 二、表单组件关系图 102 三、组件配置选项介绍 103 四、完整源代码 107 五、小结 112 第十七章:悬停提示与...

    Extjs4.2 调用Asp.net webServics

    原Extjs4.2似乎不支持Send数据时转JSON,资源里的修改Ext-all.js文件,添加参数extraParams:{wt:"json"}send时会转Json数据提交。 解决“无效的 JSON 基元”的问题

    精通JS脚本之ExtJS框架.part2.rar

    12.1.3 Ext.Ajax.request提交XML数据 12.2 Ext.Updater基础 12.2.1 Ext.Updater.update方法 12.2.2 Ext.Updater.update操作示例 12.3 利用Ajax优化Web应用框架 12.3.1 多级联动菜单 12.3.2 Ajax修改用户密码 ...

    Ajax+JSON 提交数据的演示

    Ajax+JSON 改善Form提交数据的 UI 交互设计

    ExtJs4.1+Jbpm4+SSH2+Oracle10g实际项目源码

    业务流程执行:流程的启动、任务的提交、回退、撤销、取回、处理任务等操作、 实用需求实现:执行权限、工作流留痕、事务提醒 只需一套实现,你的业务只要在挂接中配置则可以具有工作流功能 业务方面:OA项目实战

    ExtJSWeb应用程序开发指南(第2版)

    4.3.3 Ajax模式的表单数据提交 4.3.4 标准模式的表单数据提交 4.3.5 使用Direct技术 4.4 本章小结 第5章 面板及布局类 5.1 面板panel 5.1.1 认识Ext.panel.Panel 5.1.2 Ext.panel.Panel的主要功能 5.1.3 ...

    Extjs优化(二)Form表单提交通用实现

    a.formPanel.getForm().submit({ scope: b, url: a.url, method: “post”, params: a.params, waitMsg: “正在提交数据…”, success: function(c, d) { Ext.ux.Toast.msg(“操作信息”, “成功信息保存!...

    精通JS脚本之ExtJS框架.part1.rar

    12.1.3 Ext.Ajax.request提交XML数据 12.2 Ext.Updater基础 12.2.1 Ext.Updater.update方法 12.2.2 Ext.Updater.update操作示例 12.3 利用Ajax优化Web应用框架 12.3.1 多级联动菜单 12.3.2 Ajax修改用户密码 ...

    ExtJs与Java通信

    最完善的ExtJS与Java后台通信,两种数据传输第一种采用的是JSon,第二种是采用直接提交Form表单,个人觉得ExtJS比Flex与Java通信要简单写,呵呵。谁有WebService学习资料告诉我啊,要自己写WebService了哈。

    ExtJS+SSH 物流管理系统

    接收到中心库房提交的验货单与发送过来的货物和分发单,比较相同则可作入库操作,并置中心库房出库订单为配送站到货订单。 7、分站管理员:完成商品的投递,具体包含:给配送员派单,并作结单操作。订单状态由配送站...

    Extjs实现进度条的两种便捷方式

    提交数据前用Ext.Msg.wait(‘提示’,’正在处理数据,请稍候’);弹出等待条,数据处理成功后用Ext.Msg.hide();将等待去掉,例如: Ext.Msg.wait(‘提示’,’正在处理数据,请稍候’); 代码如下: Ext.Ajax.request({ ...

    Extjs4+jbpm4+SSH+oracle.zip

    业务流程执行:流程的启动、任务的提交、回退、撤销、取回、处理任务等操作、 实用需求实现:执行权限、工作流留痕、事务提醒 只需一套实现,你的业务只要在挂接中配置则可以具有工作流功能 业务方面:OA项目实战...

Global site tag (gtag.js) - Google Analytics