使用Ext.grid.column.Action操作表格数据

6.8.10 使用Ext.grid.column.Action操作表格数据

前面已经提到,通过使用Ext.grid.column.Action列类型,可以为表格增加"按钮"列,但用户单击不同按钮时将会激发不同的事件处理函数,比如用户单击"编辑"按钮时,系统弹出浮动窗口修改当前记录;当用户单击"删除"按钮时,系统删除指定记录。

如下示例示范了使用Ext.grid.column.Action按钮列来操作表格数据的方法。

程序清单:codes\06\6.8\Ext.grid\Ext.grid.Panel_Action.html

 
 
  1. <body> 
  2. <script type="text/javascript"> 
  3. Ext.onReady(function(){  
  4.     Ext.define('Book', {  
  5.         extend: 'Ext.data.Model',  
  6.         fields: [  
  7.             {name: 'id' , type: 'int'},  
  8.             {name: 'name', type: 'string'},  
  9.             {name: 'author', type: 'string'},  
  10.             {name: 'price', type: 'float'},  
  11.         ]  
  12.     });  
  13.     // 创建一个Ext.data.Store对象  
  14.     var bookStore = Ext.create('Ext.data.Store',   
  15.     {  
  16.         // 指定使用Book Model管理记录  
  17.         model: 'Book',  
  18.         // 使用proxy指定加载远程数据  
  19.         proxy:   
  20.         {  
  21.             type: 'ajax',  
  22.             url: 'getAllBooks',// 向该URL发送Ajax请求  
  23.             reader: { // 使用Ext.data.reader.Json读取服务器数据  
  24.                 type: 'json',  
  25.                 root: 'data' // 直接读取服务器响应的data数据  
  26.             },  
  27.         },  
  28.         autoLoad:true// 自动加载服务器数据  
  29.     });  
  30.     var editWin;  
  31.     // 定义单击“编辑”按钮的事件处理函数  
  32.     var editFn = function(grid, rowIndex, colIndex)  
  33.     {  
  34.         var rec = grid.getStore().getAt(rowIndex);  
  35.         if(editWin)  
  36.         {  
  37.             editWin.setTitle('编辑《' + rec.get('name') + '》');  
  38.             var formFields = editWin.items.get(0).items;  
  39.             formFields.get(0).setValue(rec.get('id'));  
  40.             formFields.get(1).setValue(rec.get('name'));  
  41.             formFields.get(2).setValue(rec.get('author'));  
  42.             formFields.get(3).setValue(rec.get('price'));  
  43.         }  
  44.         else  
  45.         {  
  46.             editWin = Ext.create("Ext.window.Window",  
  47.             {  
  48.                 title : '编辑《' + rec.get('name') + '》',  
  49.                 items: [  
  50.                     {  
  51.                         url: 'updateBook',  
  52.                         xtype: 'form',  
  53.                         width: '100%',  
  54.                         bodyPadding: 10,  
  55.                         items: [  
  56.                             {  
  57.                                 xtype: 'textfield',  
  58.                                 name: 'id',  
  59.                                 readOnly: true,  
  60.                                 value: rec.get('id'), // 指定该表单控件的值  
  61.                                 fieldLabel: 'ID', // 指定该表单控件的标签  
  62.                             },   
  63.                             {  
  64.                                 xtype: 'textfield',  
  65.                                 name: 'name',  
  66.                                 value: rec.get('name'), // 指定该表单控件的值  
  67.                                 fieldLabel: '书名', // 指定该表单控件的标签  
  68.                             },  
  69.                             {  
  70.                                 xtype: 'textfield',  
  71.                                 name: 'author',  
  72.                                 value: rec.get('author'), // 指定该表单控件的值  
  73.                                 fieldLabel: '作者', // 指定该表单控件的标签  
  74.                             },  
  75.                             {  
  76.                                 xtype: 'textfield',  
  77.                                 name: 'price',  
  78.                                 value: rec.get('price'), // 指定该表单控件的值  
  79.                                 fieldLabel: '价格', // 指定该表单控件的标签  
  80.                             }  
  81.                         ]  
  82.                     }  
  83.                 ],  
  84.                 listeners: {  
  85.                     beforedestroy : function(cmp)  
  86.                     {  
  87.                         this.hide();  
  88.                         return false;  
  89.                     }  
  90.                 },  
  91.                 bbar: [  
  92.                     {xtype: 'tbfill' ,},  
  93.                     {text:'确定' , handler: function()  
  94.                         {  
  95.                             // 获取表单,实际返回的是Ext.form.Basic对象  
  96.                             var form = editWin.items.get(0).getForm();  
  97.                             // 如果表单输入校验通过  
  98.                             if (form.isValid())   
  99.                             {  
  100.                                 // 以Ajax方式提交表单  
  101.                                 form.submit(  
  102.                                 {  
  103.                                     // 修改成功的回调函数  
  104.                                     success: function(form, action)   
  105.                                     {  
  106.                                         bookStore.reload();  
  107.                                         editWin.hide();  
  108.                                         alert(action.result.tip);  
  109.                                     }  
  110.                                 });  
  111.                             }  
  112.                         }  
  113.                     },   
  114.                     {text:'取消' , handler:function()  
  115.                     {  
  116.                         editWin.hide();  
  117.                     }},  
  118.                     {xtype: 'tbfill' ,}  
  119.                 ]  
  120.             });  
  121.         }  
  122.         editWin.setVisible(true);  
  123.     };  
  124.     // 定义单击“删除”按钮的事件处理函数  
  125.     var deleteFn = function(grid, rowIndex, colIndex)   
  126.     {  
  127.         if(confirm("确认删除" , "您是否真想删除该记录"))  
  128.         {  
  129.             var rec = grid.getStore().getAt(rowIndex);  
  130.             Ext.Ajax.request({  
  131.                 url: 'deleteBook', // 向此处发送Ajax请求  
  132.                 method: 'POST',  
  133.                 params: { // 指定请求参数  
  134.                     id: rec.get('id')  
  135.                 }, // 指定服务器响应完成的回调函数  
  136.                 success: function(response){  
  137.                     bookStore.reload();  
  138.                     alert(Ext.JSON.decode(response.responseText).tip);  
  139.                 }  
  140.             });  
  141.         }  
  142.     };  
  143.     var grid = Ext.create('Ext.grid.Panel', {  
  144.         title: '查看服务器端图书',  
  145.         width: 550, // 指定表单宽度  
  146.         renderTo: Ext.getBody(),  
  147.         // 定义该表格包含的所有数据列  
  148.         columns: [  
  149.             { text: '图书ID', dataIndex: 'id' , flex: 1 }, // 第1个数据列  
  150.              // 第2个数据列  
  151.             { text: '书名', dataIndex: 'name' , flex: 1,  
  152.                 editor: {xtype: 'textfield', allowPattern: false}},  
  153.              // 第3个数据列  
  154.             { text: '作者', dataIndex: 'author', flex: 1,  
  155.                 editor: {xtype: 'textfield', allowPattern: false}},  
  156.             // 第4个数据列  
  157.             { text: '价格', dataIndex: 'price' , flex: 1,  
  158.                 editor: {xtype: 'numberfield', allowPattern: false}},  
  159.             {  
  160.                 text: '操作',  
  161.                 xtype:'actioncolumn',  
  162.                 width: 50,  
  163.                 items: [  
  164.                     {  
  165.                         icon: 'edit.gif', // 指定图标  
  166.                         tooltip: '编辑',  
  167.                         handler: editFn // 指定单击“编辑”按钮的事件处理函数  
  168.                     },  
  169.                     {  
  170.                         icon: 'delete.gif', // 指定图标  
  171.                         tooltip: '删除',  
  172.                         handler: deleteFn // 指定单击“删除”按钮的事件处理函数  
  173.                     }  
  174.                 ]  
  175.             }  
  176.         ],  
  177.         store: bookStore  
  178.     });  
  179. });  
  180. </script> 
  181. </body> 

上面页面代码中的最后一段粗体字代码指定了在该列增加两个按钮,当用户单击该列的 "编辑"按钮时,系统会激发editFn函数,该函数将会根据需要创建一个Ext.window.Window窗口来编辑当前记录;当用户单击该列的"删除"按钮时,系统将会激发"deleteFn"函数,该函数将会弹出一个确认框,让用户确认是否需要删除当前记录。

在浏览器中浏览该页面,单击"编辑"按钮,将可以看到如图6.82所示界面。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值