extjs学习笔记(五)可编辑的grid

大多数时候,我们只是使用grid来显示信息,但有时也需要在grid中对信息进行编辑。在extjs中,构造一个可编辑的grid非常方便。

 我们来看下Ext.grid.EditorGridPanel,该类从GridPanel继承而来,通过对列提供editor进行编辑。在前边的例子中,我们用过renderer函数,知道可以把数据显示成我们希望的样子,而在编辑的时候,我们其实是针对的原始数据。另外,我们在编辑的时候,通常希望能够对用户的输入进行控制而不是任由用户随心所欲的输入,Ext.form命名空间有一些类能够对用户输入进行一些基本的控制,我们会在下边的代码中看到,这里我再次强调一下,在向服务器提交数据之前和数据在服务器进行处理之前,必须要进行数据的有效性验证。由于对grid已经比较熟悉了,下边就直接给出代码了:

  1 // /<reference path="vswd-ext_2.0.2.js" />
  2 /*
  3*作者:大笨
  4*日期:2009-10-20
  5*版本:1.0
  6*博客地址:http://yage.cnblogs.com
  7*QQ:14202190
  8*/

  9 Ext.BLANK_IMAGE_URL  =   ' ../extjs/resources/images/default/s.gif ' ;
 10
 11 Ext.onReady( function ()  {
 12    Ext.QuickTips.init();
 13
 14    //格式化日期
 15    function formatDate(value) {
 16        return value ? value.dateFormat('Y年m月d日') : '';
 17    }

 18
 19    // 别名
 20    var fm = Ext.form;
 21
 22    //构造一个只能包含checkbox的列
 23    var checkColumn = new Ext.grid.CheckColumn({
 24        header: 'Indoor?',
 25        dataIndex: 'indoor',
 26        width: 55
 27    }
);
 28
 29    // 构造ColumnModel
 30    var cm = new Ext.grid.ColumnModel({
 31        columns: [{
 32            id: 'common',
 33            header: 'Common Name',
 34            dataIndex: 'common',
 35            width: 220,
 36            // 使用上边定义好的别名
 37            editor: new fm.TextField({
 38                allowBlank: false
 39            }
)
 40        }
{
 41            header: 'Light',
 42            dataIndex: 'light',
 43            width: 130,
 44            editor: new fm.ComboBox({
 45                typeAhead: true,
 46                triggerAction: 'all',
 47                transform: 'light',
 48                lazyRender: true,
 49                listClass: 'x-combo-list-small'
 50            }
)
 51        }
{
 52            header: 'Price',
 53            dataIndex: 'price',
 54            width: 70,
 55            align: 'right',
 56            renderer: 'usMoney',
 57            editor: new fm.NumberField({
 58                allowBlank: false,
 59                allowNegative: false,
 60                maxValue: 100000
 61            }
)
 62        }
{
 63            header: 'Available',
 64            dataIndex: 'availDate',
 65            width: 95,
 66            renderer: formatDate,
 67            editor: new fm.DateField({
 68                format: 'Y年m月d日',
 69                minValue: '01/01/06',
 70                disabledDays: [06],
 71                disabledDaysText: 'Plants are not available on the weekends'
 72            }
)
 73        }
,
 74        checkColumn,
 75    ],
 76        defaults: {
 77        sortable:true
 78    }

 79}
);
 80
 81
 82// 构造一个Store对象
 83var store = new Ext.data.Store({
 84
 85    url: 'plants.xml',
 86
 87    reader: new Ext.data.XmlReader(
 88            {
 89                record: 'plant'
 90            }
,
 91
 92            [
 93                { name: 'common', type: 'string' },
 94                { name: 'botanical', type: 'string' },
 95                { name: 'light' },
 96                { name: 'price', type: 'float' },
 97                { name: 'availDate', mapping: 'availability', type: 'date', dateFormat: 'm/d/Y' },
 98                { name: 'indoor', type: 'bool' }
 99            ]
100        ),
101
102    sortInfo: { field: 'common', direction: 'ASC' }
103}
);
104
105// 构造可编辑的grid
106var grid = new Ext.grid.EditorGridPanel({
107    store: store,
108    cm: cm,
109    renderTo: 'grid',
110    width: 600,
111    height: 300,
112    autoExpandColumn: 'common',
113    title: 'Edit Plants?',
114    frame: true,
115    plugins: checkColumn,
116    clicksToEdit: 1
117}
);
118
119// 触发数据的加载
120store.load();
121}
);

     我们在前边的系列中使用过数据和json来作为数据保存或者传递的格式,考虑到xml也很常用,这次我们使用了xml文件,内容如下:

plants.xml <? xml version="1.0" encoding="utf-8" ?>
  2 < catalog >
  3    < plant >
  4      < common > Bloodroot </ common >
  5      < botanical > Sanguinaria canadensis </ botanical >
  6      < zone > 4 </ zone >
  7      < light > Mostly Shady </ light >
  8      < price > 2.44 </ price >
  9      < availability > 03/15/2006 </ availability >
 10      < indoor > true </ indoor >
 11    </ plant >
 12    < plant >
 13      < common > Columbine </ common >
 14      < botanical > Aquilegia canadensis </ botanical >
 15      < zone > 3 </ zone >
 16      < light > Mostly Shady </ light >
 17      < price > 9.37 </ price >
 18      < availability > 03/06/2006 </ availability >
 19      < indoor > true </ indoor >
 20    </ plant >
 21    < plant >
 22      < common > Marsh Marigold </ common >
 23      < botanical > Caltha palustris </ botanical >
 24      < zone > 4 </ zone >
 25      < light > Mostly Sunny </ light >
 26      < price > 6.81 </ price >
 27      < availability > 05/17/2006 </ availability >
 28      < indoor > false </ indoor >
 29    </ plant >
 30    < plant >
 31      < common > Cowslip </ common >
 32      < botanical > Caltha palustris </ botanical >
 33      < zone > 4 </ zone >
 34      < light > Mostly Shady </ light >
 35      < price > 9.90 </ price >
 36      < availability > 03/06/2006 </ availability >
 37      < indoor > true </ indoor >
 38    </ plant >
 39    < plant >
 40      < common > Dutchman's-Breeches </ common >
 41      < botanical > Dicentra cucullaria </ botanical >
 42      < zone > 3 </ zone >
 43      < light > Mostly Shady </ light >
 44      < price > 6.44 </ price >
 45      < availability > 01/20/2006 </ availability >
 46      < indoor > true </ indoor >
 47    </ plant >
 48    < plant >
 49      < common > Ginger, Wild </ common >
 50      < botanical > Asarum canadense </ botanical >
 51      < zone > 3 </ zone >
 52      < light > Mostly Shady </ light >
 53      < price > 9.03 </ price >
 54      < availability > 04/18/2006 </ availability >
 55      < indoor > true </ indoor >
 56    </ plant >
 57    < plant >
 58      < common > Hepatica </ common >
 59      < botanical > Hepatica americana </ botanical >
 60      < zone > 4 </ zone >
 61      < light > Mostly Shady </ light >
 62      < price > 4.45 </ price >
 63      < availability > 01/26/2006 </ availability >
 64      < indoor > true </ indoor >
 65    </ plant >
 66    < plant >
 67      < common > Liverleaf </ common >
 68      < botanical > Hepatica americana </ botanical >
 69      < zone > 4 </ zone >
 70      < light > Mostly Shady </ light >
 71      < price > 3.99 </ price >
 72      < availability > 01/02/2006 </ availability >
 73      < indoor > true </ indoor >
 74    </ plant >
 75    < plant >
 76      < common > Jack-In-The-Pulpit </ common >
 77      < botanical > Arisaema triphyllum </ botanical >
 78      < zone > 4 </ zone >
 79      < light > Mostly Shady </ light >
 80      < price > 3.23 </ price >
 81      < availability > 02/01/2006 </ availability >
 82      < indoor > true </ indoor >
 83    </ plant >
 84    < plant >
 85      < common > Mayapple </ common >
 86      < botanical > Podophyllum peltatum </ botanical >
 87      < zone > 3 </ zone >
 88      < light > Mostly Shady </ light >
 89      < price > 2.98 </ price >
 90      < availability > 06/05/2006 </ availability >
 91      < indoor > true </ indoor >
 92    </ plant >
 93    < plant >
 94      < common > Phlox, Woodland </ common >
 95      < botanical > Phlox divaricata </ botanical >
 96      < zone > 3 </ zone >
 97      < light > Sun or Shade </ light >
 98      < price > 2.80 </ price >
 99      < availability > 01/22/2006 </ availability >
100      < indoor > false </ indoor >
101    </ plant >
102    < plant >
103      < common > Phlox, Blue </ common >
104      < botanical > Phlox divaricata </ botanical >
105      < zone > 3 </ zone >
106      < light > Sun or Shade </ light >
107      < price > 5.59 </ price >
108      < availability > 02/16/2006 </ availability >
109      < indoor > false </ indoor >
110    </ plant >
111    < plant >
112      < common > Spring-Beauty </ common >
113      < botanical > Claytonia Virginica </ botanical >
114      < zone > 7 </ zone >
115      < light > Mostly Shady </ light >
116      < price > 6.59 </ price >
117      < availability > 02/01/2006 </ availability >
118      < indoor > true </ indoor >
119    </ plant >
120    < plant >
121      < common > Trillium </ common >
122      < botanical > Trillium grandiflorum </ botanical >
123      < zone > 5 </ zone >
124      < light > Sun or Shade </ light >
125      < price > 3.90 </ price >
126      < availability > 04/29/2006 </ availability >
127      < indoor > false </ indoor >
128    </ plant >
129    < plant >
130      < common > Wake Robin </ common >
131      < botanical > Trillium grandiflorum </ botanical >
132      < zone > 5 </ zone >
133      < light > Sun or Shade </ light >
134      < price > 3.20 </ price >
135      < availability > 02/21/2006 </ availability >
136      < indoor > false </ indoor >
137    </ plant >
138    < plant >
139      < common > Violet, Dog-Tooth </ common >
140      < botanical > Erythronium americanum </ botanical >
141      < zone > 4 </ zone >
142      < light > Shade </ light >
143      < price > 9.04 </ price >
144      < availability > 02/01/2006 </ availability >
145      < indoor > true </ indoor >
146    </ plant >
147    < plant >
148      < common > Trout Lily </ common >
149      < botanical > Erythronium americanum </ botanical >
150      < zone > 4 </ zone >
151      < light > Shade </ light >
152      < price > 6.94 </ price >
153      < availability > 03/24/2006 </ availability >
154      < indoor > true </ indoor >
155    </ plant >
156    < plant >
157      < common > Adder's-Tongue </ common >
158      < botanical > Erythronium americanum </ botanical >
159      < zone > 4 </ zone >
160      < light > Shade </ light >
161      < price > 9.58 </ price >
162      < availability > 04/13/2006 </ availability >
163      < indoor > true </ indoor >
164    </ plant >
165    < plant >
166      < common > Anemone </ common >
167      < botanical > Anemone blanda </ botanical >
168      < zone > 6 </ zone >
169      < light > Mostly Shady </ light >
170      < price > 8.86 </ price >
171      < availability > 12/26/2006 </ availability >
172      < indoor > true </ indoor >
173    </ plant >
174    < plant >
175      < common > Grecian Windflower </ common >
176      < botanical > Anemone blanda </ botanical >
177      < zone > 6 </ zone >
178      < light > Mostly Shady </ light >
179      < price > 9.16 </ price >
180      < availability > 07/10/2006 </ availability >
181      < indoor > true </ indoor >
182    </ plant >
183    < plant >
184      < common > Bee Balm </ common >
185      < botanical > Monarda didyma </ botanical >
186      < zone > 4 </ zone >
187      < light > Shade </ light >
188      < price > 4.59 </ price >
189      < availability > 05/03/2006 </ availability >
190      < indoor > true </ indoor >
191    </ plant >
192    < plant >
193      < common > Bergamot </ common >
194      < botanical > Monarda didyma </ botanical >
195      < zone > 4 </ zone >
196      < light > Shade </ light >
197      < price > 7.16 </ price >
198      < availability > 04/27/2006 </ availability >
199      < indoor > true </ indoor >
200    </ plant >
201    < plant >
202      < common > Black-Eyed Susan </ common >
203      < botanical > Rudbeckia hirta </ botanical >
204      < zone > Annual </ zone >
205      < light > Sunny </ light >
206      < price > 9.80 </ price >
207      < availability > 06/18/2006 </ availability >
208      < indoor > false </ indoor >
209    </ plant >
210    < plant >
211      < common > Buttercup </ common >
212      < botanical > Ranunculus </ botanical >
213      < zone > 4 </ zone >
214      < light > Shade </ light >
215      < price > 2.57 </ price >
216      < availability > 06/10/2006 </ availability >
217      < indoor > true </ indoor >
218    </ plant >
219    < plant >
220      < common > Crowfoot </ common >
221      < botanical > Ranunculus </ botanical >
222      < zone > 4 </ zone >
223      < light > Shade </ light >
224      < price > 9.34 </ price >
225      < availability > 04/03/2006 </ availability >
226      < indoor > true </ indoor >
227    </ plant >
228    < plant >
229      < common > Butterfly Weed </ common >
230      < botanical > Asclepias tuberosa </ botanical >
231      < zone > Annual </ zone >
232      < light > Sunny </ light >
233      < price > 2.78 </ price >
234      < availability > 06/30/2006 </ availability >
235      < indoor > false </ indoor >
236    </ plant >
237    < plant >
238      < common > Cinquefoil </ common >
239      < botanical > Potentilla </ botanical >
240      < zone > Annual </ zone >
241      < light > Shade </ light >
242      < price > 7.06 </ price >
243      < availability > 05/25/2006 </ availability >
244      < indoor > true </ indoor >
245    </ plant >
246    < plant >
247      < common > Primrose </ common >
248      < botanical > Oenothera </ botanical >
249      < zone > 3 - 5 </ zone >
250      < light > Sunny </ light >
251      < price > 6.56 </ price >
252      < availability > 01/30/2006 </ availability >
253      < indoor > false </ indoor >
254    </ plant >
255    < plant >
256      < common > Gentian </ common >
257      < botanical > Gentiana </ botanical >
258      < zone > 4 </ zone >
259      < light > Sun or Shade </ light >
260      < price > 7.81 </ price >
261      < availability > 05/18/2006 </ availability >
262      < indoor > false </ indoor >
263    </ plant >
264    < plant >
265      < common > Blue Gentian </ common >
266      < botanical > Gentiana </ botanical >
267      < zone > 4 </ zone >
268      < light > Sun or Shade </ light >
269      < price > 8.56 </ price >
270      < availability > 05/02/2006 </ availability >
271      < indoor > false </ indoor >
272    </ plant >
273    < plant >
274      < common > Jacob's Ladder </ common >
275      < botanical > Polemonium caeruleum </ botanical >
276      < zone > Annual </ zone >
277      < light > Shade </ light >
278      < price > 9.26 </ price >
279      < availability > 02/21/2006 </ availability >
280      < indoor > true </ indoor >
281    </ plant >
282    < plant >
283      < common > Greek Valerian </ common >
284      < botanical > Polemonium caeruleum </ botanical >
285      < zone > Annual </ zone >
286      < light > Shade </ light >
287      < price > 4.36 </ price >
288      < availability > 07/14/2006 </ availability >
289      < indoor > true </ indoor >
290    </ plant >
291    < plant >
292      < common > California Poppy </ common >
293      < botanical > Eschscholzia californica </ botanical >
294      < zone > Annual </ zone >
295      < light > Sunny </ light >
296      < price > 7.89 </ price >
297      < availability > 03/27/2006 </ availability >
298      < indoor > false </ indoor >
299    </ plant >
300    < plant >
301      < common > Shooting Star </ common >
302      < botanical > Dodecatheon </ botanical >
303      < zone > Annual </ zone >
304      < light > Mostly Shady </ light >
305      < price > 8.60 </ price >
306      < availability > 05/13/2006 </ availability >
307      < indoor > true </ indoor >
308    </ plant >
309    < plant >
310      < common > Snakeroot </ common >
311      < botanical > Cimicifuga </ botanical >
312      < zone > Annual </ zone >
313      < light > Shade </ light >
314      < price > 5.63 </ price >
315      < availability > 07/11/2006 </ availability >
316      < indoor > true </ indoor >
317    </ plant >
318    < plant >
319      < common > Cardinal Flower </ common >
320      < botanical > Lobelia cardinalis </ botanical >
321      < zone > 2 </ zone >
322      < light > Shade </ light >
323      < price > 3.02 </ price >
324      < availability > 02/22/2006 </ availability >
325      < indoor > true </ indoor >
326    </ plant >
327 </ catalog >

    我们先来体验下效果:

extjs学习笔记(五)可编辑的grid

     我们注意到被编辑过的单元格左上角有个红色的小箭头,并且第一列是不允许为空的,第二列可以从下拉列表来选择,第三列在编辑状态显示的是原始数据,第四列可以从一个日期控件中来选择日期。现在我们来看一下主要的代码:

    第12行,和本次的主题无关,只是初始化一个全局QuickTips对象,该对象为元素提供漂亮和可定制的提示工具(tooltips)。

    第15-17行,是列的renderer函数, 在下边构造ColumnModel的时候会用到。

    第20行,取了个别名,下文中需要使用Ext.form的地方,就可以简单的使用fm来代替了。

    第23-27行,使用CheckColumn类构造了一个显示checkbox的列, 该类在一个单独的js文中定义。我们也可以使用renderer函数很方便的做到这一点,但是有现成的没有理由不用。

    第30-79行,构造了ColumnModel类的对象,我们在其中使用了editor来提供编辑的功能。editor其实是Ext.form.Field类或者其子类的一个对象,我们会在学习表单的时候再详细介绍Ext.form命名空间中的类。这里我们只需要注意在构造Ext.form.ComboBox对象的时候,需要在页面上有一个HTML的SELECT元素,配置参数transform需要这个SELECT元素。

    第83-103行构造grid需要的Store对象,现在我们只要能够对照plant.xml文件大概看明白就可以了 。

    第106-117就是我们想要的可编辑的grid了,大部分参数我们都用过,这里简单介绍下最后三个参数:

        frame:Boolean类型的值,默认为false,呈现出普通的1个象素的方形边框;为true的时候可以是定制的圆弧形转角(custom rounded corners )。有兴趣的朋友可以用firebug观察一下不同值的时候生成的html代码。

        plugins:为grid组件提供自定义功能的插件,只有在插件的init方法需要grid组件的引用作为参数的时候才需要。

        clicksToEdit:变为可编辑状态需要点击鼠标的次数,默认值是2。

    下边给出我们的页面文件:

edit-grid.htm

     需要注意的是,当我们对grid中的数据编辑之后并未保存,所以页面一旦刷新,我们的改动便会丢失。另外,我们也并未对数据进行有效性验证,只是由列的editor提供了最基本的验证,比如数据不能为空。我们会在下一个系列对grid进行增加和删除操作中学习到如何解决这些问题。

 

 

 

 

 

 

你可能感兴趣的:(ExtJs)