Extjs4增删改查

Ext.onReady(function() {
	Ext.define('User', {
				extend : 'Ext.data.Model',
				fields : [{
							name : 'userId',
							type : 'int',
							useNull : true//这样数字如果值为空则不会自动转成0,则提交时注意后台bean类中的属性int要用对象类型,否则解析出错
						}, {
							name : 'loginName',
							type : 'string'
						}, {
							name : 'password',
							type : 'string'
						}, {
							name : 'remark',
							type : 'string'
						}, {
							name : 'roleId',
							type : 'float',
							useNull : true
						}, {
							name : 'rightId',
							type : 'float',
							useNull : true
						}, {
							name : 'platformNo',
							type : 'string'
						}, {
							name : 'groupId',
							type : 'float',
							useNull : true
						}, {
							name : 'net',
							type : 'string'
						}, {
							name : 'email',
							type : 'string'
						}, {
							name : 'linkman',
							type : 'string'
						}, {
							name : 'tel',
							type : 'string'
						}, {
							name : 'fax',
							type : 'string'
						}, {
							name : 'address',
							type : 'string'
						}],
				idProperty : 'userId'// 极为重要的配置。关系到表格修改数据的获取
			});

	var store = new Ext.data.Store({
				model : 'User',
				pageSize : 3,
				proxy : {
					type : 'ajax',
					url : 'baseUsers.action',
					reader : {
						type : 'json',
						root : 'pageBean.list',
						totalProperty : 'pageBean.total'
					}
				},
				autoLoad : false
			});
	var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
				clicksToEdit : 2
			});
	var grid = Ext.create('Ext.grid.Panel', {
		tbar : [ {
					xtype : 'button',
					text : '新增',
					handler : add
				},{
					xtype : 'button',
					text : '提交修改',
					handler : alter
				}, {
					xtype : 'button',
					text : '删除',
					handler : otherDelete
				}],
		title : 'All Products',
		store : store,
		columnLines : true,
		selModel : Ext.create('Ext.selection.CheckboxModel'),
		columns : [{
					header : 'userId',
					dataIndex : 'userId',
					hidden:true
				}, {
					header : 'loginName',
					dataIndex : 'loginName',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'password',
					dataIndex : 'password',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'remark',
					dataIndex : 'remark',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'roleId',
					dataIndex : 'roleId',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'rightId',
					dataIndex : 'rightId',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'platformNo',
					dataIndex : 'platformNo',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'groupId',
					dataIndex : 'groupId',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'net',
					dataIndex : 'net',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'email',
					dataIndex : 'email',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'linkman',
					dataIndex : 'linkman',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'tel',
					dataIndex : 'tel',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'fax',
					dataIndex : 'fax',
					editor : {
						allowBlank : false
					}
				}, {
					header : 'address',
					dataIndex : 'address',
					editor : {
						allowBlank : false
					}
				}],

		forceFit : true,
		dockedItems : [{
					xtype : 'pagingtoolbar',
					store : store, // same store GridPanel is
					// using
					dock : 'bottom',
					displayInfo : true
				}],
		renderTo : 'userMngDiv',
		plugins : [cellEditing]
			// autoRender:true
		});
	store.loadPage(1);
	var p = parent.Ext.getCmp('contentTabs');
	// alert(p);
	function alter() {
		var records = store.getUpdatedRecords();// 获取修改的行的数据,无法获取幻影数据
		var phantoms=store.getNewRecords( ) ;//获得幻影行
		records=records.concat(phantoms);//将幻影数据与真实数据合并
		if (records.length == 0) {
			Ext.MessageBox.show({
				title : "提示",
				msg : "没有任何数据被修改过!"
					// icon: Ext.MessageBox.INFO
				});
			return;
		} else {
			Ext.Msg.confirm("请确认", "是否真的要修改数据?", function(button, text) {
				if (button == "yes") {
					var data = [];
					// alert(records);
					Ext.Array.each(records, function(record) {
						data.push(record.data);
							// record.commit();// 向store提交修改数据,页面效果
						});

					Ext.Ajax.request({
						url : 'alterUsers.action',
						params : {
							alterUsers : Ext.encode(data)
						},
						method : 'POST',
						timeout : 2000,

						success : function(response, opts) {
							var success = Ext.decode(response.responseText).success;
							// 当后台数据同步成功时
							if (success) {
								Ext.Array.each(records, function(record) {
											// data.push(record.data);
											record.commit();// 向store提交修改数据,页面效果
										});
							} else {
								Ext.MessageBox.show({
									title : "提示",
									msg : "数据修改失败!"
										// icon: Ext.MessageBox.INFO
									});
							}
						}
					});
				}
			});

		}

	}
	// 传递对象删除
//	function deleteUsers() {
//		var data = grid.getSelectionModel().getSelection();
//		// alert(data);
//		if (data.length == 0) {
//			Ext.MessageBox.show({
//				title : "提示",
//				msg : "请先选择您要操作的行!"
//					// icon: Ext.MessageBox.INFO
//				});
//			return;
//		} else {
//			Ext.Msg.confirm("请确认", "是否真的要删除数据?", function(button, text) {
//				if (button == "yes") {
//					var ids = [];
//					Ext.Array.each(data, function(record) {
//								ids.push(record.data);
//							});
//					Ext.Ajax.request({
//						url : 'deleteUsers.action',
//						params : {
//							deleteUsers : Ext.encode(ids)
//						},
//						method : 'POST',
//						// timeout : 2000,//默认30秒
//						success : function(response, opts) {
//							var success = Ext.decode(response.responseText).success;
//							// 当后台数据同步成功时
//							if (success) {
//								Ext.Array.each(data, function(record) {
//											store.remove(record);// 页面效果
//										});
//							} else {
//								Ext.MessageBox.show({
//									title : "提示",
//									msg : "数据删除失败!"
//										// icon: Ext.MessageBox.INFO
//									});
//							}
//
//						}
//					});
//				}
//			});
//
//		}
//	}
	// 编码ID删除
	function otherDelete() {

		var data = grid.getSelectionModel().getSelection();
		// alert(data);
		if (data.length == 0) {
			Ext.MessageBox.show({
				title : "提示",
				msg : "请先选择您要操作的行!"
					// icon: Ext.MessageBox.INFO
				});
			return;
		} else {
			Ext.Msg.confirm("请确认", "是否真的要删除数据?", function(button, text) {
				if (button == "yes") {
					var ids = [];
					Ext.Array.each(data, function(record) {
								var userId=record.get('userId');
								//如果删除的是幻影数据,则id就不传递到后台了,直接在前台删除即可
								if(userId){
									ids.push(userId);
								}
								
							});

					Ext.Ajax.request({
						url : 'deleteUsers.action',
						params : {
							deleteIds : ids.join(',')
						},
						method : 'POST',
						// timeout : 2000,//默认30秒
						success : function(response, opts) {

							// store.loadPage(1);

							var success = Ext.decode(response.responseText).success;
							// 当后台数据同步成功时
							if (success) {
								Ext.Array.each(data, function(record) {
											store.remove(record);// 页面效果
										});
							} else {
								Ext.MessageBox.show({
									title : "提示",
									msg : "数据删除失败!"
										// icon: Ext.MessageBox.INFO
									});
							}

						}
					});
				}
			});

		}

	}
	function add(){
		store.insert(0,new User());
	}
});

Ext.onReady(function() {
Ext.define('User', {
extend : 'Ext.data.Model',
fields : [{
name : 'userId',
type : 'int',
useNull : true//这样数字如果值为空则不会自动转成0,则提交时注意后台bean类中的属性int要用对象类型,否则解析出错
}, {
name : 'loginName',
type : 'string'
}, {
name : 'password',
type : 'string'
}, {
name : 'remark',
type : 'string'
}, {
name : 'roleId',
type : 'float',
useNull : true
}, {
name : 'rightId',
type : 'float',
useNull : true
}, {
name : 'platformNo',
type : 'string'
}, {
name : 'groupId',
type : 'float',
useNull : true
}, {
name : 'net',
type : 'string'
}, {
name : 'email',
type : 'string'
}, {
name : 'linkman',
type : 'string'
}, {
name : 'tel',
type : 'string'
}, {
name : 'fax',
type : 'string'
}, {
name : 'address',
type : 'string'
}],
idProperty : 'userId'// 极为重要的配置。关系到表格修改数据的获取
});


var store = new Ext.data.Store({
model : 'User',
pageSize : 3,
proxy : {
type : 'ajax',
url : 'baseUsers.action',
reader : {
type : 'json',
root : 'pageBean.list',
totalProperty : 'pageBean.total'
}
},
autoLoad : false
});
var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit : 2
});
var grid = Ext.create('Ext.grid.Panel', {
tbar : [ {
xtype : 'button',
text : '新增',
handler : add
},{
xtype : 'button',
text : '提交修改',
handler : alter
}, {
xtype : 'button',
text : '删除',
handler : otherDelete
}],
title : 'All Products',
store : store,
columnLines : true,
selModel : Ext.create('Ext.selection.CheckboxModel'),
columns : [{
header : 'userId',
dataIndex : 'userId',
hidden:true
}, {
header : 'loginName',
dataIndex : 'loginName',
editor : {
allowBlank : false
}
}, {
header : 'password',
dataIndex : 'password',
editor : {
allowBlank : false
}
}, {
header : 'remark',
dataIndex : 'remark',
editor : {
allowBlank : false
}
}, {
header : 'roleId',
dataIndex : 'roleId',
editor : {
allowBlank : false
}
}, {
header : 'rightId',
dataIndex : 'rightId',
editor : {
allowBlank : false
}
}, {
header : 'platformNo',
dataIndex : 'platformNo',
editor : {
allowBlank : false
}
}, {
header : 'groupId',
dataIndex : 'groupId',
editor : {
allowBlank : false
}
}, {
header : 'net',
dataIndex : 'net',
editor : {
allowBlank : false
}
}, {
header : 'email',
dataIndex : 'email',
editor : {
allowBlank : false
}
}, {
header : 'linkman',
dataIndex : 'linkman',
editor : {
allowBlank : false
}
}, {
header : 'tel',
dataIndex : 'tel',
editor : {
allowBlank : false
}
}, {
header : 'fax',
dataIndex : 'fax',
editor : {
allowBlank : false
}
}, {
header : 'address',
dataIndex : 'address',
editor : {
allowBlank : false
}
}],


forceFit : true,
dockedItems : [{
xtype : 'pagingtoolbar',
store : store, // same store GridPanel is
// using
dock : 'bottom',
displayInfo : true
}],
renderTo : 'userMngDiv',
plugins : [cellEditing]
// autoRender:true
});
store.loadPage(1);
var p = parent.Ext.getCmp('contentTabs');
// alert(p);
function alter() {
var records = store.getUpdatedRecords();// 获取修改的行的数据,无法获取幻影数据
var phantoms=store.getNewRecords( ) ;//获得幻影行
records=records.concat(phantoms);//将幻影数据与真实数据合并
if (records.length == 0) {
Ext.MessageBox.show({
title : "提示",
msg : "没有任何数据被修改过!"
// icon: Ext.MessageBox.INFO
});
return;
} else {
Ext.Msg.confirm("请确认", "是否真的要修改数据?", function(button, text) {
if (button == "yes") {
var data = [];
// alert(records);
Ext.Array.each(records, function(record) {
data.push(record.data);
// record.commit();// 向store提交修改数据,页面效果
});


Ext.Ajax.request({
url : 'alterUsers.action',
params : {
alterUsers : Ext.encode(data)
},
method : 'POST',
timeout : 2000,


success : function(response, opts) {
var success = Ext.decode(response.responseText).success;
// 当后台数据同步成功时
if (success) {
Ext.Array.each(records, function(record) {
// data.push(record.data);
record.commit();// 向store提交修改数据,页面效果
});
} else {
Ext.MessageBox.show({
title : "提示",
msg : "数据修改失败!"
// icon: Ext.MessageBox.INFO
});
}
}
});
}
});


}


}
// 传递对象删除
// function deleteUsers() {
// var data = grid.getSelectionModel().getSelection();
// // alert(data);
// if (data.length == 0) {
// Ext.MessageBox.show({
// title : "提示",
// msg : "请先选择您要操作的行!"
// // icon: Ext.MessageBox.INFO
// });
// return;
// } else {
// Ext.Msg.confirm("请确认", "是否真的要删除数据?", function(button, text) {
// if (button == "yes") {
// var ids = [];
// Ext.Array.each(data, function(record) {
// ids.push(record.data);
// });
// Ext.Ajax.request({
// url : 'deleteUsers.action',
// params : {
// deleteUsers : Ext.encode(ids)
// },
// method : 'POST',
// // timeout : 2000,//默认30秒
// success : function(response, opts) {
// var success = Ext.decode(response.responseText).success;
// // 当后台数据同步成功时
// if (success) {
// Ext.Array.each(data, function(record) {
// store.remove(record);// 页面效果
// });
// } else {
// Ext.MessageBox.show({
// title : "提示",
// msg : "数据删除失败!"
// // icon: Ext.MessageBox.INFO
// });
// }
//
// }
// });
// }
// });
//
// }
// }
// 编码ID删除
function otherDelete() {


var data = grid.getSelectionModel().getSelection();
// alert(data);
if (data.length == 0) {
Ext.MessageBox.show({
title : "提示",
msg : "请先选择您要操作的行!"
// icon: Ext.MessageBox.INFO
});
return;
} else {
Ext.Msg.confirm("请确认", "是否真的要删除数据?", function(button, text) {
if (button == "yes") {
var ids = [];
Ext.Array.each(data, function(record) {
var userId=record.get('userId');
//如果删除的是幻影数据,则id就不传递到后台了,直接在前台删除即可
if(userId){
ids.push(userId);
}

});


Ext.Ajax.request({
url : 'deleteUsers.action',
params : {
deleteIds : ids.join(',')
},
method : 'POST',
// timeout : 2000,//默认30秒
success : function(response, opts) {


// store.loadPage(1);


var success = Ext.decode(response.responseText).success;
// 当后台数据同步成功时
if (success) {
Ext.Array.each(data, function(record) {
store.remove(record);// 页面效果
});
} else {
Ext.MessageBox.show({
title : "提示",
msg : "数据删除失败!"
// icon: Ext.MessageBox.INFO
});
}


}
});
}
});


}


}
function add(){
store.insert(0,new User());
}
});
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值