ExtJS中的面向对象设计,组件化编程思想
 1 /*
/*
2 * @author: Lilf
 * @author: Lilf
3 * Description: ExtJS中的面向对象设计,组件化变成思想
 * Description: ExtJS中的面向对象设计,组件化变成思想
4 */
 */
5 /***************************扩展VTypes类,增加年龄的验证****************************/
/***************************扩展VTypes类,增加年龄的验证****************************/
6 Ext.apply(Ext.form.VTypes, {
Ext.apply(Ext.form.VTypes, {
7 "age": function(_v){
    "age": function(_v){
8 if (/^\d+$/.test(_v)) {
        if (/^\d+$/.test(_v)) {
9 var intExp = parseInt(_v);
            var intExp = parseInt(_v);
10 if (intExp < 200)
            if (intExp < 200) 
11 return true;
                return true;
12 }
        }
13 return false;
        return false;
14 },
    },
15 ageText: "请输入正确的年龄格式,如:23"
    ageText: "请输入正确的年龄格式,如:23"
16 });
});
17 /***************************继承自FormPanel的表单组件,用来构件Window***************************/
/***************************继承自FormPanel的表单组件,用来构件Window***************************/
18 PersonInfoFormPanel = Ext.extend(Ext.form.FormPanel, {
PersonInfoFormPanel = Ext.extend(Ext.form.FormPanel, {
19 constructor: function(){
    constructor: function(){
20 PersonInfoFormPanel.superclass.constructor.apply(this, [{
        PersonInfoFormPanel.superclass.constructor.apply(this, [{
21 baseCls: "x-plain",
            baseCls: "x-plain",
22 buttonAlign: "right",
            buttonAlign: "right",
23 labelWidth: 30,
            labelWidth: 30,
24 defaultType: "textfield",
            defaultType: "textfield",
25 defaults: {
            defaults: {
26 anchor: "95%",
                anchor: "95%",
27 labelStyle: "text-align:right"
                labelStyle: "text-align:right"
28 },
            },
29 items: [{
            items: [{
30 fieldLabel: "姓名",
                fieldLabel: "姓名",
31 name: "name"
                name: "name"
32 }, {
            }, {
33 fieldLabel: "年龄",
                fieldLabel: "年龄",
34 name: "age",
                name: "age",
35 vtype: "age"//验证年龄(通过vtype类型来验证)
                vtype: "age"//验证年龄(通过vtype类型来验证)
36 }, {
            }, {
37 xtype: "combo",
                xtype: "combo",
38 mode: "local",//本地数据
                mode: "local",//本地数据
39 readOnly: true,
                readOnly: true,
40 fieldLabel: "性别",
                fieldLabel: "性别",
41 displayField: "sex",//显示下拉框的内容
                displayField: "sex",//显示下拉框的内容
42 triggerAction: "all",//在选择时,显示所有的项
                triggerAction: "all",//在选择时,显示所有的项
43 value: "男",//默认值
                value: "男",//默认值
44 store: new Ext.data.SimpleStore({
                store: new Ext.data.SimpleStore({
45 fields: ["sex"],
                    fields: ["sex"],
46 data: [["男"], ["女"]]
                    data: [["男"], ["女"]]
47 }),
                }),
48 name: "sex"//绑定字段
                name: "sex"//绑定字段
49 }]
            }]
50 
        
51 
        
52 }])
        }])
53 },
    },
54 //---以下为PersonInfoFormPanel类对外提供的方法---
    //---以下为PersonInfoFormPanel类对外提供的方法---
55 getValues: function(){
    getValues: function(){
56 if (this.getForm().isValid())
        if (this.getForm().isValid()) 
57 return new Ext.data.Record(this.getForm().getValues());
            return new Ext.data.Record(this.getForm().getValues());
58 else
        else 
59 throw new Error("验证没有通过");//自定义异常
            throw new Error("验证没有通过");//自定义异常
60 },
    },
61 setValues: function(_r){
    setValues: function(_r){
62 this.getForm().loadRecord(_r);
        this.getForm().loadRecord(_r);
63 },
    },
64 reset: function(){
    reset: function(){
65 this.getForm().reset();
        this.getForm().reset();
66 }
    }
67 
    
68 
    
69 });
});
70 /*************继承自Window的基类,insertWindow与updateWindow都由此继承****************/
/*************继承自Window的基类,insertWindow与updateWindow都由此继承****************/
71 baseWindow = Ext.extend(Ext.Window, {
baseWindow = Ext.extend(Ext.Window, {
72 form: null,
    form: null,
73 constructor: function(){
    constructor: function(){
74 this.form = new PersonInfoFormPanel();//实例化PersonInfoFormPanel类
        this.form = new PersonInfoFormPanel();//实例化PersonInfoFormPanel类
75 baseWindow.superclass.constructor.apply(this, [{
        baseWindow.superclass.constructor.apply(this, [{
76 plain: true,
            plain: true,
77 width: 350,
            width: 350,
78 //title: "新增人员",
            //title: "新增人员",
79 modal: true,
            modal: true,
80 resizable: false,
            resizable: false,
81 closeAction: "hide",
            closeAction: "hide",
82 defaults: {
            defaults: {
83 style: "padding:5px"
                style: "padding:5px"
84 },
            },
85 items: this.form,
            items: this.form,
86 buttons: [{
            buttons: [{
87 text: "确 定",
                text: "确 定",
88 handler: this.onSubmitClick,//提交事件调用
                handler: this.onSubmitClick,//提交事件调用
89 scope: this
                scope: this
90 }, {
            }, {
91 text: "取 消",
                text: "取 消",
92 handler: this.onCancelClick,//取消事件调用
                handler: this.onCancelClick,//取消事件调用
93 scope: this
                scope: this
94 
            
95 }]
            }]
96 }]);
        }]);
97 //给insertWindow对象添加事件(事件冒泡)
        //给insertWindow对象添加事件(事件冒泡)
98 this.addEvents("submit");
        this.addEvents("submit");
99 },
    },
100 //提交事件处理函数
    //提交事件处理函数
101 onSubmitClick: function(){
    onSubmitClick: function(){
102 try {
        try {
103 //发布事件
            //发布事件
104 this.fireEvent("submit", this, this.form.getValues());//调用PersonInfoFormPanel类中自定义的方法getValues
            this.fireEvent("submit", this, this.form.getValues());//调用PersonInfoFormPanel类中自定义的方法getValues
105 this.close();
            this.close();
106 
            
107 }
        } 
108 catch (_err) {
        catch (_err) {
109 Ext.Msg.alert("系统提示", _err.description);//扑捉自定义错误或异常
            Ext.Msg.alert("系统提示", _err.description);//扑捉自定义错误或异常
110 }
        }
111 },
    },
112 //取消事件处理函数
    //取消事件处理函数
113 onCancelClick: function(){
    onCancelClick: function(){
114 this.close();
        this.close();
115 },
    },
116 //重置与隐藏事件处理函数
    //重置与隐藏事件处理函数
117 close: function(){
    close: function(){
118 this.form.reset();
        this.form.reset();
119 this.hide();
        this.hide();
120 }
    }
121 
    
122 });
});
123 /******************insertWindow类****************************/
/******************insertWindow类****************************/
124 insertWindow = Ext.extend(baseWindow, {
insertWindow = Ext.extend(baseWindow, {
125 title: "新增人员"
    title: "新增人员"
126 });
});
127 /****************updateWindow类******************************/
/****************updateWindow类******************************/
128 updateWindow = Ext.extend(baseWindow, {
updateWindow = Ext.extend(baseWindow, {
129 title: "修改人员",
    title: "修改人员",
130 load: function(_r){
    load: function(_r){
131 this.form.setValues(_r);
        this.form.setValues(_r);
132 }
    }
133 });
});
134 /*******根据上面组件创建新的GridPanel类,它就像我们根据不同的零件设计出来的汽车************
/*******根据上面组件创建新的GridPanel类,它就像我们根据不同的零件设计出来的汽车************
135 * ExtJs自定义PersonListGridPanel类
 * ExtJs自定义PersonListGridPanel类
136 * 该类继承自GridPanel[使用Ext.extend(superClass,override Object)方法实现继承],
 * 该类继承自GridPanel[使用Ext.extend(superClass,override Object)方法实现继承],
137 * 并override了该类的构造函�hu数
 * 并override了该类的构造函�hu数
138 * 构造函数内部继承自GridPanel的构造函数[apply(this,arguments)实现继承]
 * 构造函数内部继承自GridPanel的构造函数[apply(this,arguments)实现继承]
139 * 该类实现了如何对外部公布一个事�件
 * 该类实现了如何对外部公布一个事�件
140 * 在构造函数中添加一个事件[this.addEvents("事件名称")]
 * 在构造函数中添加一个事件[this.addEvents("事件名称")]
141 * 然后使用this.fireEvent("事件名称",参数)来发布此事�件
 * 然后使用this.fireEvent("事件名称",参数)来发布此事�件
142 * 最后在客户端调用的时候来订阅该事�jian件
 * 最后在客户端调用的时候来订阅该事�jian件
143 */
 */
144 PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
145 _window: null,
    _window: null,
146 _updateWin: null,
    _updateWin: null,
147 constructor: function(_url){
    constructor: function(_url){
148 this._window = new insertWindow();//insertWindow对象引用
        this._window = new insertWindow();//insertWindow对象引用
149 this._updateWin = new updateWindow();//updateWindow对象引用
        this._updateWin = new updateWindow();//updateWindow对象引用
150 PersonListGridPanel.superclass.constructor.apply(this, [{
        PersonListGridPanel.superclass.constructor.apply(this, [{
151 renderTo: Ext.getBody(),
            renderTo: Ext.getBody(),
152 width: 550,
            width: 550,
153 height: 200,
            height: 200,
154 frame: true,
            frame: true,
155 layout: "form",
            layout: "form",
156 //工具栏
            //工具栏
157 tbar: [{
            tbar: [{
158 text: "add",
                text: "add",
159 handler: function(){
                handler: function(){
160 this._window.show();
                    this._window.show();
161 },
                },
162 scope: this
                scope: this
163 }, "-", {
            }, "-", {
164 text: "update",
                text: "update",
165 handler: function(){
                handler: function(){
166 this._updateWin.show();
                    this._updateWin.show();
167 try {
                    try {
168 this._updateWin.load(this.getSelected());
                        this._updateWin.load(this.getSelected());
169 }
                    } 
170 
                    
171 
                    
172 catch (_err) {
                    catch (_err) {
173 Ext.Msg.alert("系统提示", _err.description);
                        Ext.Msg.alert("系统提示", _err.description);
174 this._updateWin.close();
                        this._updateWin.close();
175 }
                    }
176 },
                },
177 scope: this
                scope: this
178 }, "-", {
            }, "-", {
179 text: "delete",
                text: "delete",
180 handler: this.onRemovePerson,
                handler: this.onRemovePerson,
181 scope: this
                scope: this
182 }],
            }],
183 enableColumnMove: false,
            enableColumnMove: false,
184 //列模板
            //列模板
185 columns: [{
            columns: [{
186 header: "Name",
                header: "Name",
187 menuDisabled: true,
                menuDisabled: true,
188 dataIndex: "name"
                dataIndex: "name"
189 }, {
            }, {
190 header: "Age",
                header: "Age",
191 menuDisabled: true,
                menuDisabled: true,
192 dataIndex: "age"
                dataIndex: "age"
193 }, {
            }, {
194 header: "Sex",
                header: "Sex",
195 menuDisabled: true,
                menuDisabled: true,
196 dataIndex: "sex"
                dataIndex: "sex"
197 }],
            }],
198 //数据源
            //数据源
199 store: new Ext.data.JsonStore({
            store: new Ext.data.JsonStore({
200 autoLoad: true,
                autoLoad: true,
201 url: _url,
                url: _url,
202 fields: ["name", "age", "sex"]
                fields: ["name", "age", "sex"]
203 }),
            }),
204 //选中模板
            //选中模板
205 selModel: new Ext.grid.RowSelectionModel({
            selModel: new Ext.grid.RowSelectionModel({
206 singleSelect: true,
                singleSelect: true,
207 listeners: {
                listeners: {
208 "rowselect": {
                    "rowselect": {
209 fn: this.onRowSelected,
                        fn: this.onRowSelected,
210 scope: this
                        scope: this
211 }
                    }
212 }
                }
213 })
            })
214 
        
215 }]);
        }]);
216 //添加事件
        //添加事件
217 this.addEvents("rowselect");
        this.addEvents("rowselect");
218 //事件订阅
        //事件订阅
219 this._window.on("submit", this.onInsertWinSubmit, this);
        this._window.on("submit", this.onInsertWinSubmit, this);
220 this._updateWin.on("submit", this.onUpdateWinSubmit, this);
        this._updateWin.on("submit", this.onUpdateWinSubmit, this);
221 },
    },
222 //----- 以下为自定义方法---------
    //----- 以下为自定义方法---------
223 //获得选中的记录
    //获得选中的记录
224 getSelected: function(){
    getSelected: function(){
225 var _sm = this.getSelectionModel();
        var _sm = this.getSelectionModel();
226 if (_sm.getCount() == 0)
        if (_sm.getCount() == 0) 
227 throw new Error("你没有选中任何记录,请选择一条记录后重试
            throw new Error("你没有选中任何记录,请选择一条记录后重试 ");
");
228 return _sm.getSelected();
        return _sm.getSelected();
229 },
    },
230 //插入一条记录
    //插入一条记录
231 insert: function(_r){
    insert: function(_r){
232 this.getStore().add(_r);
        this.getStore().add(_r);
233 },
    },
234 //更新选中的记录
    //更新选中的记录
235 update: function(_r){
    update: function(_r){
236 try {
        try {
237 var _rs = this.getSelected();
            var _rs = this.getSelected();
238 var _data = _rs.data;
            var _data = _rs.data;
239 for (var _i in _data) {
            for (var _i in _data) {
240 _rs.set(_i, _r.get(_i));
                _rs.set(_i, _r.get(_i));
241 };
            };
242 _rs.commit();
            _rs.commit();
243 }
        } 
244 catch (_err) {
        catch (_err) {
245 
        
246 }
        }
247 
        
248 },
    },
249 //删除选中的记录
    //删除选中的记录
250 remove: function(){
    remove: function(){
251 try {
        try {
252 var _rs = this.getSelected();
            var _rs = this.getSelected();
253 Ext.Msg.confirm("系统提示", "你确定删除吗?", function(_btn){
            Ext.Msg.confirm("系统提示", "你确定删除吗?", function(_btn){
254 if (_btn == "yes")
                if (_btn == "yes") 
255 this.getStore().remove(_rs);
                    this.getStore().remove(_rs);
256 }, this);
            }, this);
257 }
        } 
258 catch (_err) {
        catch (_err) {
259 Ext.Msg.alert("系统提示", _err.description);
            Ext.Msg.alert("系统提示", _err.description);
260 }
        }
261 },
    },
262 //-------以下为自定义事件处理函数------------
    //-------以下为自定义事件处理函数------------
263 //添加事件
    //添加事件
264 onInsertWinSubmit: function(_win, _r){
    onInsertWinSubmit: function(_win, _r){
265 this.insert(_r);
        this.insert(_r);
266 },
    },
267 //修改事件
    //修改事件
268 onUpdateWinSubmit: function(_win, _r){
    onUpdateWinSubmit: function(_win, _r){
269 this.update(_r);
        this.update(_r);
270 },
    },
271 //删除事件
    //删除事件
272 onRemovePerson: function(){
    onRemovePerson: function(){
273 this.remove();
        this.remove();
274 },
    },
275 //选中事件
    //选中事件
276 onRowSelected: function(_sel, _index, _r){
    onRowSelected: function(_sel, _index, _r){
277 this.fireEvent("rowselect", _r);//发布事件
        this.fireEvent("rowselect", _r);//发布事件
278 }
    }
279 
    
280 })
})
281
282
283
284
285 
 /*
/*2
 * @author: Lilf
 * @author: Lilf3
 * Description: ExtJS中的面向对象设计,组件化变成思想
 * Description: ExtJS中的面向对象设计,组件化变成思想4
 */
 */5
 /***************************扩展VTypes类,增加年龄的验证****************************/
/***************************扩展VTypes类,增加年龄的验证****************************/6
 Ext.apply(Ext.form.VTypes, {
Ext.apply(Ext.form.VTypes, {7
 "age": function(_v){
    "age": function(_v){8
 if (/^\d+$/.test(_v)) {
        if (/^\d+$/.test(_v)) {9
 var intExp = parseInt(_v);
            var intExp = parseInt(_v);10
 if (intExp < 200)
            if (intExp < 200) 11
 return true;
                return true;12
 }
        }13
 return false;
        return false;14
 },
    },15
 ageText: "请输入正确的年龄格式,如:23"
    ageText: "请输入正确的年龄格式,如:23"16
 });
});17
 /***************************继承自FormPanel的表单组件,用来构件Window***************************/
/***************************继承自FormPanel的表单组件,用来构件Window***************************/18
 PersonInfoFormPanel = Ext.extend(Ext.form.FormPanel, {
PersonInfoFormPanel = Ext.extend(Ext.form.FormPanel, {19
 constructor: function(){
    constructor: function(){20
 PersonInfoFormPanel.superclass.constructor.apply(this, [{
        PersonInfoFormPanel.superclass.constructor.apply(this, [{21
 baseCls: "x-plain",
            baseCls: "x-plain",22
 buttonAlign: "right",
            buttonAlign: "right",23
 labelWidth: 30,
            labelWidth: 30,24
 defaultType: "textfield",
            defaultType: "textfield",25
 defaults: {
            defaults: {26
 anchor: "95%",
                anchor: "95%",27
 labelStyle: "text-align:right"
                labelStyle: "text-align:right"28
 },
            },29
 items: [{
            items: [{30
 fieldLabel: "姓名",
                fieldLabel: "姓名",31
 name: "name"
                name: "name"32
 }, {
            }, {33
 fieldLabel: "年龄",
                fieldLabel: "年龄",34
 name: "age",
                name: "age",35
 vtype: "age"//验证年龄(通过vtype类型来验证)
                vtype: "age"//验证年龄(通过vtype类型来验证)36
 }, {
            }, {37
 xtype: "combo",
                xtype: "combo",38
 mode: "local",//本地数据
                mode: "local",//本地数据39
 readOnly: true,
                readOnly: true,40
 fieldLabel: "性别",
                fieldLabel: "性别",41
 displayField: "sex",//显示下拉框的内容
                displayField: "sex",//显示下拉框的内容42
 triggerAction: "all",//在选择时,显示所有的项
                triggerAction: "all",//在选择时,显示所有的项43
 value: "男",//默认值
                value: "男",//默认值44
 store: new Ext.data.SimpleStore({
                store: new Ext.data.SimpleStore({45
 fields: ["sex"],
                    fields: ["sex"],46
 data: [["男"], ["女"]]
                    data: [["男"], ["女"]]47
 }),
                }),48
 name: "sex"//绑定字段
                name: "sex"//绑定字段49
 }]
            }]50
 
        51
 
        52
 }])
        }])53
 },
    },54
 //---以下为PersonInfoFormPanel类对外提供的方法---
    //---以下为PersonInfoFormPanel类对外提供的方法---55
 getValues: function(){
    getValues: function(){56
 if (this.getForm().isValid())
        if (this.getForm().isValid()) 57
 return new Ext.data.Record(this.getForm().getValues());
            return new Ext.data.Record(this.getForm().getValues());58
 else
        else 59
 throw new Error("验证没有通过");//自定义异常
            throw new Error("验证没有通过");//自定义异常60
 },
    },61
 setValues: function(_r){
    setValues: function(_r){62
 this.getForm().loadRecord(_r);
        this.getForm().loadRecord(_r);63
 },
    },64
 reset: function(){
    reset: function(){65
 this.getForm().reset();
        this.getForm().reset();66
 }
    }67
 
    68
 
    69
 });
});70
 /*************继承自Window的基类,insertWindow与updateWindow都由此继承****************/
/*************继承自Window的基类,insertWindow与updateWindow都由此继承****************/71
 baseWindow = Ext.extend(Ext.Window, {
baseWindow = Ext.extend(Ext.Window, {72
 form: null,
    form: null,73
 constructor: function(){
    constructor: function(){74
 this.form = new PersonInfoFormPanel();//实例化PersonInfoFormPanel类
        this.form = new PersonInfoFormPanel();//实例化PersonInfoFormPanel类75
 baseWindow.superclass.constructor.apply(this, [{
        baseWindow.superclass.constructor.apply(this, [{76
 plain: true,
            plain: true,77
 width: 350,
            width: 350,78
 //title: "新增人员",
            //title: "新增人员",79
 modal: true,
            modal: true,80
 resizable: false,
            resizable: false,81
 closeAction: "hide",
            closeAction: "hide",82
 defaults: {
            defaults: {83
 style: "padding:5px"
                style: "padding:5px"84
 },
            },85
 items: this.form,
            items: this.form,86
 buttons: [{
            buttons: [{87
 text: "确 定",
                text: "确 定",88
 handler: this.onSubmitClick,//提交事件调用
                handler: this.onSubmitClick,//提交事件调用89
 scope: this
                scope: this90
 }, {
            }, {91
 text: "取 消",
                text: "取 消",92
 handler: this.onCancelClick,//取消事件调用
                handler: this.onCancelClick,//取消事件调用93
 scope: this
                scope: this94
 
            95
 }]
            }]96
 }]);
        }]);97
 //给insertWindow对象添加事件(事件冒泡)
        //给insertWindow对象添加事件(事件冒泡)98
 this.addEvents("submit");
        this.addEvents("submit");99
 },
    },100
 //提交事件处理函数
    //提交事件处理函数101
 onSubmitClick: function(){
    onSubmitClick: function(){102
 try {
        try {103
 //发布事件
            //发布事件104
 this.fireEvent("submit", this, this.form.getValues());//调用PersonInfoFormPanel类中自定义的方法getValues
            this.fireEvent("submit", this, this.form.getValues());//调用PersonInfoFormPanel类中自定义的方法getValues105
 this.close();
            this.close();106
 
            107
 }
        } 108
 catch (_err) {
        catch (_err) {109
 Ext.Msg.alert("系统提示", _err.description);//扑捉自定义错误或异常
            Ext.Msg.alert("系统提示", _err.description);//扑捉自定义错误或异常110
 }
        }111
 },
    },112
 //取消事件处理函数
    //取消事件处理函数113
 onCancelClick: function(){
    onCancelClick: function(){114
 this.close();
        this.close();115
 },
    },116
 //重置与隐藏事件处理函数
    //重置与隐藏事件处理函数117
 close: function(){
    close: function(){118
 this.form.reset();
        this.form.reset();119
 this.hide();
        this.hide();120
 }
    }121
 
    122
 });
});123
 /******************insertWindow类****************************/
/******************insertWindow类****************************/124
 insertWindow = Ext.extend(baseWindow, {
insertWindow = Ext.extend(baseWindow, {125
 title: "新增人员"
    title: "新增人员"126
 });
});127
 /****************updateWindow类******************************/
/****************updateWindow类******************************/128
 updateWindow = Ext.extend(baseWindow, {
updateWindow = Ext.extend(baseWindow, {129
 title: "修改人员",
    title: "修改人员",130
 load: function(_r){
    load: function(_r){131
 this.form.setValues(_r);
        this.form.setValues(_r);132
 }
    }133
 });
});134
 /*******根据上面组件创建新的GridPanel类,它就像我们根据不同的零件设计出来的汽车************
/*******根据上面组件创建新的GridPanel类,它就像我们根据不同的零件设计出来的汽车************135
 * ExtJs自定义PersonListGridPanel类
 * ExtJs自定义PersonListGridPanel类136
 * 该类继承自GridPanel[使用Ext.extend(superClass,override Object)方法实现继承],
 * 该类继承自GridPanel[使用Ext.extend(superClass,override Object)方法实现继承],137
 * 并override了该类的构造函�hu数
 * 并override了该类的构造函�hu数138
 * 构造函数内部继承自GridPanel的构造函数[apply(this,arguments)实现继承]
 * 构造函数内部继承自GridPanel的构造函数[apply(this,arguments)实现继承]139
 * 该类实现了如何对外部公布一个事�件
 * 该类实现了如何对外部公布一个事�件140
 * 在构造函数中添加一个事件[this.addEvents("事件名称")]
 * 在构造函数中添加一个事件[this.addEvents("事件名称")]141
 * 然后使用this.fireEvent("事件名称",参数)来发布此事�件
 * 然后使用this.fireEvent("事件名称",参数)来发布此事�件142
 * 最后在客户端调用的时候来订阅该事�jian件
 * 最后在客户端调用的时候来订阅该事�jian件143
 */
 */144
 PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {145
 _window: null,
    _window: null,146
 _updateWin: null,
    _updateWin: null,147
 constructor: function(_url){
    constructor: function(_url){148
 this._window = new insertWindow();//insertWindow对象引用
        this._window = new insertWindow();//insertWindow对象引用149
 this._updateWin = new updateWindow();//updateWindow对象引用
        this._updateWin = new updateWindow();//updateWindow对象引用150
 PersonListGridPanel.superclass.constructor.apply(this, [{
        PersonListGridPanel.superclass.constructor.apply(this, [{151
 renderTo: Ext.getBody(),
            renderTo: Ext.getBody(),152
 width: 550,
            width: 550,153
 height: 200,
            height: 200,154
 frame: true,
            frame: true,155
 layout: "form",
            layout: "form",156
 //工具栏
            //工具栏157
 tbar: [{
            tbar: [{158
 text: "add",
                text: "add",159
 handler: function(){
                handler: function(){160
 this._window.show();
                    this._window.show();161
 },
                },162
 scope: this
                scope: this163
 }, "-", {
            }, "-", {164
 text: "update",
                text: "update",165
 handler: function(){
                handler: function(){166
 this._updateWin.show();
                    this._updateWin.show();167
 try {
                    try {168
 this._updateWin.load(this.getSelected());
                        this._updateWin.load(this.getSelected());169
 }
                    } 170
 
                    171
 
                    172
 catch (_err) {
                    catch (_err) {173
 Ext.Msg.alert("系统提示", _err.description);
                        Ext.Msg.alert("系统提示", _err.description);174
 this._updateWin.close();
                        this._updateWin.close();175
 }
                    }176
 },
                },177
 scope: this
                scope: this178
 }, "-", {
            }, "-", {179
 text: "delete",
                text: "delete",180
 handler: this.onRemovePerson,
                handler: this.onRemovePerson,181
 scope: this
                scope: this182
 }],
            }],183
 enableColumnMove: false,
            enableColumnMove: false,184
 //列模板
            //列模板185
 columns: [{
            columns: [{186
 header: "Name",
                header: "Name",187
 menuDisabled: true,
                menuDisabled: true,188
 dataIndex: "name"
                dataIndex: "name"189
 }, {
            }, {190
 header: "Age",
                header: "Age",191
 menuDisabled: true,
                menuDisabled: true,192
 dataIndex: "age"
                dataIndex: "age"193
 }, {
            }, {194
 header: "Sex",
                header: "Sex",195
 menuDisabled: true,
                menuDisabled: true,196
 dataIndex: "sex"
                dataIndex: "sex"197
 }],
            }],198
 //数据源
            //数据源199
 store: new Ext.data.JsonStore({
            store: new Ext.data.JsonStore({200
 autoLoad: true,
                autoLoad: true,201
 url: _url,
                url: _url,202
 fields: ["name", "age", "sex"]
                fields: ["name", "age", "sex"]203
 }),
            }),204
 //选中模板
            //选中模板205
 selModel: new Ext.grid.RowSelectionModel({
            selModel: new Ext.grid.RowSelectionModel({206
 singleSelect: true,
                singleSelect: true,207
 listeners: {
                listeners: {208
 "rowselect": {
                    "rowselect": {209
 fn: this.onRowSelected,
                        fn: this.onRowSelected,210
 scope: this
                        scope: this211
 }
                    }212
 }
                }213
 })
            })214
 
        215
 }]);
        }]);216
 //添加事件
        //添加事件217
 this.addEvents("rowselect");
        this.addEvents("rowselect");218
 //事件订阅
        //事件订阅219
 this._window.on("submit", this.onInsertWinSubmit, this);
        this._window.on("submit", this.onInsertWinSubmit, this);220
 this._updateWin.on("submit", this.onUpdateWinSubmit, this);
        this._updateWin.on("submit", this.onUpdateWinSubmit, this);221
 },
    },222
 //----- 以下为自定义方法---------
    //----- 以下为自定义方法---------223
 //获得选中的记录
    //获得选中的记录224
 getSelected: function(){
    getSelected: function(){225
 var _sm = this.getSelectionModel();
        var _sm = this.getSelectionModel();226
 if (_sm.getCount() == 0)
        if (_sm.getCount() == 0) 227
 throw new Error("你没有选中任何记录,请选择一条记录后重试
            throw new Error("你没有选中任何记录,请选择一条记录后重试 ");
");228
 return _sm.getSelected();
        return _sm.getSelected();229
 },
    },230
 //插入一条记录
    //插入一条记录231
 insert: function(_r){
    insert: function(_r){232
 this.getStore().add(_r);
        this.getStore().add(_r);233
 },
    },234
 //更新选中的记录
    //更新选中的记录235
 update: function(_r){
    update: function(_r){236
 try {
        try {237
 var _rs = this.getSelected();
            var _rs = this.getSelected();238
 var _data = _rs.data;
            var _data = _rs.data;239
 for (var _i in _data) {
            for (var _i in _data) {240
 _rs.set(_i, _r.get(_i));
                _rs.set(_i, _r.get(_i));241
 };
            };242
 _rs.commit();
            _rs.commit();243
 }
        } 244
 catch (_err) {
        catch (_err) {245
 
        246
 }
        }247
 
        248
 },
    },249
 //删除选中的记录
    //删除选中的记录250
 remove: function(){
    remove: function(){251
 try {
        try {252
 var _rs = this.getSelected();
            var _rs = this.getSelected();253
 Ext.Msg.confirm("系统提示", "你确定删除吗?", function(_btn){
            Ext.Msg.confirm("系统提示", "你确定删除吗?", function(_btn){254
 if (_btn == "yes")
                if (_btn == "yes") 255
 this.getStore().remove(_rs);
                    this.getStore().remove(_rs);256
 }, this);
            }, this);257
 }
        } 258
 catch (_err) {
        catch (_err) {259
 Ext.Msg.alert("系统提示", _err.description);
            Ext.Msg.alert("系统提示", _err.description);260
 }
        }261
 },
    },262
 //-------以下为自定义事件处理函数------------
    //-------以下为自定义事件处理函数------------263
 //添加事件
    //添加事件264
 onInsertWinSubmit: function(_win, _r){
    onInsertWinSubmit: function(_win, _r){265
 this.insert(_r);
        this.insert(_r);266
 },
    },267
 //修改事件
    //修改事件268
 onUpdateWinSubmit: function(_win, _r){
    onUpdateWinSubmit: function(_win, _r){269
 this.update(_r);
        this.update(_r);270
 },
    },271
 //删除事件
    //删除事件272
 onRemovePerson: function(){
    onRemovePerson: function(){273
 this.remove();
        this.remove();274
 },
    },275
 //选中事件
    //选中事件276
 onRowSelected: function(_sel, _index, _r){
    onRowSelected: function(_sel, _index, _r){277
 this.fireEvent("rowselect", _r);//发布事件
        this.fireEvent("rowselect", _r);//发布事件278
 }
    }279
 
    280
 })
})281

282

283

284

285
 
 
                    
                     
                    
                 
                    
                
 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号