刪除與修改的操作,分兩塊進(jìn)行。頁(yè)面刪除與后臺(tái)刪除。因此在頁(yè)面上進(jìn)行刪除或修改后成功后,后臺(tái)只傳遞一個(gè)SUCCESS標(biāo)記,若后臺(tái)同步成功,則在頁(yè)面的store中執(zhí)行刪除或者修改。減少網(wǎng)絡(luò)中的數(shù)據(jù)傳輸。
但是這樣有一個(gè)bug,就是當(dāng)用戶新增了幻影數(shù)據(jù),并且與真實(shí)數(shù)據(jù)一起修改填寫提交后,后臺(tái)已經(jīng)寫入數(shù)據(jù)庫(kù)。然后此時(shí)再刪除之前添加的幻影數(shù)據(jù),會(huì)導(dǎo)致id(代表一條記錄的主鍵,本文代碼中是userId)傳遞不到后臺(tái),導(dǎo)致無(wú)法查詢刪除。解決方案是當(dāng)后臺(tái)處理成功后,前臺(tái)重新加載頁(yè)面,但是這樣網(wǎng)絡(luò)負(fù)擔(dān)就加重了。使之前的優(yōu)勢(shì)喪失
- Ext.onReady(function() {
- Ext.define('User', {
- extend : 'Ext.data.Model',
- fields : [{
- name : 'userId',
- type : 'int',
- useNull : true//這樣數(shù)字如果值為空則不會(huì)自動(dòng)轉(zhuǎn)成0,則提交時(shí)注意后臺(tái)bean類中的屬性int要用對(duì)象類型,否則解析出錯(cuò)
- }, {
- 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'// 極為重要的配置。關(guān)系到表格修改數(shù)據(jù)的獲取
- });
-
- 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();// 獲取修改的行的數(shù)據(jù),無(wú)法獲取幻影數(shù)據(jù)
- var phantoms=store.getNewRecords( ) ;//獲得幻影行
- records=records.concat(phantoms);//將幻影數(shù)據(jù)與真實(shí)數(shù)據(jù)合并
- if (records.length == 0) {
- Ext.MessageBox.show({
- title : "提示",
- msg : "沒(méi)有任何數(shù)據(jù)被修改過(guò)!"
- // icon: Ext.MessageBox.INFO
- });
- return;
- } else {
- Ext.Msg.confirm("請(qǐng)確認(rèn)", "是否真的要修改數(shù)據(jù)?", function(button, text) {
- if (button == "yes") {
- var data = [];
- // alert(records);
- Ext.Array.each(records, function(record) {
- data.push(record.data);
- // record.commit();// 向store提交修改數(shù)據(jù),頁(yè)面效果
- });
-
- 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;
- // 當(dāng)后臺(tái)數(shù)據(jù)同步成功時(shí)
- if (success) {
- Ext.Array.each(records, function(record) {
- // data.push(record.data);
- record.commit();// 向store提交修改數(shù)據(jù),頁(yè)面效果
- });
- } else {
- Ext.MessageBox.show({
- title : "提示",
- msg : "數(shù)據(jù)修改失敗!"
- // icon: Ext.MessageBox.INFO
- });
- }
- }
- });
- }
- });
-
- }
-
- }
- // 傳遞對(duì)象刪除
- // function deleteUsers() {
- // var data = grid.getSelectionModel().getSelection();
- // // alert(data);
- // if (data.length == 0) {
- // Ext.MessageBox.show({
- // title : "提示",
- // msg : "請(qǐng)先選擇您要操作的行!"
- // // icon: Ext.MessageBox.INFO
- // });
- // return;
- // } else {
- // Ext.Msg.confirm("請(qǐng)確認(rèn)", "是否真的要?jiǎng)h除數(shù)據(jù)?", 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,//默認(rèn)30秒
- // success : function(response, opts) {
- // var success = Ext.decode(response.responseText).success;
- // // 當(dāng)后臺(tái)數(shù)據(jù)同步成功時(shí)
- // if (success) {
- // Ext.Array.each(data, function(record) {
- // store.remove(record);// 頁(yè)面效果
- // });
- // } else {
- // Ext.MessageBox.show({
- // title : "提示",
- // msg : "數(shù)據(jù)刪除失敗!"
- // // icon: Ext.MessageBox.INFO
- // });
- // }
- //
- // }
- // });
- // }
- // });
- //
- // }
- // }
- // 編碼ID刪除
- function otherDelete() {
-
- var data = grid.getSelectionModel().getSelection();
- // alert(data);
- if (data.length == 0) {
- Ext.MessageBox.show({
- title : "提示",
- msg : "請(qǐng)先選擇您要操作的行!"
- // icon: Ext.MessageBox.INFO
- });
- return;
- } else {
- Ext.Msg.confirm("請(qǐng)確認(rèn)", "是否真的要?jiǎng)h除數(shù)據(jù)?", function(button, text) {
- if (button == "yes") {
- var ids = [];
- Ext.Array.each(data, function(record) {
- var userId=record.get('userId');
- //如果刪除的是幻影數(shù)據(jù),則id就不傳遞到后臺(tái)了,直接在前臺(tái)刪除即可
- if(userId){
- ids.push(userId);
- }
-
- });
-
- Ext.Ajax.request({
- url : 'deleteUsers.action',
- params : {
- deleteIds : ids.join(',')
- },
- method : 'POST',
- // timeout : 2000,//默認(rèn)30秒
- success : function(response, opts) {
-
- // store.loadPage(1);
-
- var success = Ext.decode(response.responseText).success;
- // 當(dāng)后臺(tái)數(shù)據(jù)同步成功時(shí)
- if (success) {
- Ext.Array.each(data, function(record) {
- store.remove(record);// 頁(yè)面效果
- });
- } else {
- Ext.MessageBox.show({
- title : "提示",
- msg : "數(shù)據(jù)刪除失敗!"
- // icon: Ext.MessageBox.INFO
- });
- }
-
- }
- });
- }
- });
-
- }
-
- }
- function add(){
- store.insert(0,new User());
- }
- });
struts2配置
- <struts>
- <package name="system" extends="json-default" namespace="/">
- <action name="baseUsers" class="wk.len.actions.system.UsersAction" method="usersInfo">
- <result name="success" type="json">
-
- </result>
- </action>
- <action name="deleteUsers" class="wk.len.actions.system.UsersAction" method="deleteUsers">
- <result name="success" type="json">
- <param name="includeProperties">success</param>
- </result>
- </action>
- <action name="alterUsers" class="wk.len.actions.system.UsersAction" method="alterUsers">
- <result name="success" type="json">
- <param name="includeProperties">success</param>
- </result>
- </action>
- </package>
- </struts>
|