本示例将使用第 2 章定义的 T_Categories 表,结合 DataWrite 和 RowEditor 直接在 Grid 中实现添加、修改和删除操作。
- Ext.QuickTips.init();
- 这是 RowEditor 显示错误信息用的,必须加上。
- 接着定义一个 HttpProxy ,代码如下所示。
- var proxy = new Ext.data.HttpProxy({
- api: {
- read : 'grid.ashx?act=list',
- create : 'grid.ashx?act=create',
- update: 'grid.ashx?act=update',
- destroy: 'grid.ashx?act=del'
- }
- });
- var reader = new Ext.data.JsonReader({
- totalProperty: 'total',
- successProperty: 'success',
- idProperty:"id",
- root: 'rows'
- }, [
- {name: 'id'},
- {name: 'cid',allowBlank: false},
- {name: 'title', allowBlank: false},
- {name: 'desc'}
- ]);
- var writer = new Ext.data.JsonWriter({
- encode: true,
- writeAllFields: true
- });
- var store = new Ext.data.Store({
- proxy: proxy,
- reader: reader,
- writer: writer,
- autoSave: true,
- autoLoad:true,
- listeners: {
- write : function(store, action, result, res, rs) {
- if(action==Ext.data.Api.actions.destroy){
- Ext.Msg.alert(" 信息 ",res["msg"]);
- }
- },
- exception : function(proxy, type, action, options, res, arg) {
- if (type === 'remote') {
- Ext.Msg.show({
- title: ' 错误 ',
- msg: res.message,
- icon: Ext.MessageBox.ERROR,
- buttons: Ext.Msg.OK
- });
- }else{
- if(action==Ext.data.Api.actions.create){
- var row = Ext.decode(options.params["rows"]);
- if(row["id"]){
- rec=store.getById(row["id"]);
- if(rec) store.remove(rec);
- }
- }
- var o= Ext.decode(res.responseText);
- if(o["msg"]){
- Ext.Msg.alert(" 错误 ",o["msg"]);
- }
- }
- }
- }
- });
- var editor = new Ext.ux.grid.RowEditor({
- saveText: ' 保存 ',
- cancelText:' 取消 ',
- listeners:{
- beforeedit:function(rowedit,index){
- // 如果是 update 状态不允许修改 id
- var rec=store.getAt(index);
- if(rec.data.cid){
- ideditor.disable();
- }else{
- ideditor.enable();
- }
- }
- }
- });