extjs学习笔记(五)可编辑的grid
大多数时候,我们只是使用grid来显示信息,但有时也需要在grid中对信息进行编辑。在extjs中,构造一个可编辑的grid非常方便。
?我们来看下Ext.grid.EditorGridPanel,该类从GridPanel继承而来,通过对列提供editor进行编辑。在前边的例子中,我们用过renderer函数,知道可以把数据显示成我们希望的样子,而在编辑的时候,我们其实是针对的原始数据。另外,我们在编辑的时候,通常希望能够对用户的输入进行控制而不是任由用户随心所欲的输入,Ext.form命名空间有一些类能够对用户输入进行一些基本的控制,我们会在下边的代码中看到,这里我再次强调一下,在向服务器提交数据之前和数据在服务器进行处理之前,必须要进行数据的有效性验证。由于对grid已经比较熟悉了,下边就直接给出代码了:
///<reference?path="vswd-ext_2.0.2.js"?/>
/**//*
*作者:大笨
*日期:2009-10-20
*版本:1.0
*博客地址:http://yage.cnblogs.com
*QQ:14202190
*/
Ext.BLANK_IMAGE_URL?=?'../extjs/resources/images/default/s.gif';
Ext.onReady(function()?
{
????Ext.QuickTips.init();
????//格式化日期
????function?formatDate(value)?
{
????????return?value???value.dateFormat('Y年m月d日')?:?'';
????}
????//?别名
????var?fm?=?Ext.form;
????//构造一个只能包含checkbox的列
????var?checkColumn?=?new?Ext.grid.CheckColumn(
{
????????header:?'Indoor?',
????????dataIndex:?'indoor',
????????width:?55
????});
????//?构造ColumnModel
????var?cm?=?new?Ext.grid.ColumnModel(
{
????????columns:?[
{
????????????id:?'common',
????????????header:?'Common?Name',
????????????dataIndex:?'common',
????????????width:?220,
????????????//?使用上边定义好的别名
????????????editor:?new?fm.TextField(
{
????????????????allowBlank:?false
????????????})
????????},?
{
????????????header:?'Light',
????????????dataIndex:?'light',
????????????width:?130,
????????????editor:?new?fm.ComboBox(
{
????????????????typeAhead:?true,
????????????????triggerAction:?'all',
????????????????transform:?'light',
????????????????lazyRender:?true,
????????????????listClass:?'x-combo-list-small'
????????????})
????????},?
{
????????????header:?'Price',
????????????dataIndex:?'price',
????????????width:?70,
????????????align:?'right',
????????????renderer:?'usMoney',
????????????editor:?new?fm.NumberField(
{
????????????????allowBlank:?false,
????????????????allowNegative:?false,
????????????????maxValue:?100000
????????????})
????????},?
{
????????????header:?'Available',
????????????dataIndex:?'availDate',
????????????width:?95,
????????????renderer:?formatDate,
????????????editor:?new?fm.DateField(
{
????????????????format:?'Y年m月d日',
????????????????minValue:?'01/01/06',
????????????????disabledDays:?[0,?6],
????????????????disabledDaysText:?'Plants?are?not?available?on?the?weekends'
????????????})
????????},
????????checkColumn,
????],
????????defaults:?
{
????????sortable:true
????}
});

//?构造一个Store对象
var?store?=?new?Ext.data.Store(
{
????url:?'plants.xml',
????reader:?new?Ext.data.XmlReader(
????????????
{
????????????????record:?'plant'
????????????},
????????????[
????????????????
{?name:?'common',?type:?'string'?},
????????????????
{?name:?'botanical',?type:?'string'?},
????????????????
{?name:?'light'?},
????????????????
{?name:?'price',?type:?'float'?},
????????????????
{?name:?'availDate',?mapping:?'availability',?type:?'date',?dateFormat:?'m/d/Y'?},
????????????????
{?name:?'indoor',?type:?'bool'?}
????????????]
????????),
????sortInfo:?
{?field:?'common',?direction:?'ASC'?}
});
//?构造可编辑的grid
var?grid?=?new?Ext.grid.EditorGridPanel(
{
????store:?store,
????cm:?cm,
????renderTo:?'grid',
????width:?600,
????height:?300,
????autoExpandColumn:?'common',
????title:?'Edit?Plants?',
????frame:?true,
????plugins:?checkColumn,
????clicksToEdit:?1
});
//?触发数据的加载
store.load();
}); ???? 我们在前边的系列中使用过数据和json来作为数据保存或者传递的格式,考虑到xml也很常用,这次我们使用了xml文件,内容如下:
<?xml?version="1.0"?encoding="utf-8"?>
<catalog>
??<plant>
????<common>Bloodroot</common>
????<botanical>Sanguinaria?canadensis</botanical>
????<zone>4</zone>
????<light>Mostly?Shady</light>
????<price>2.44</price>
????<availability>03/15/2006</availability>
????<indoor>true</indoor>
??</plant>
??<plant>
????<common>Columbine</common>
????<botanical>Aquilegia?canadensis</botanical>
????<zone>3</zone>
????<light>Mostly?Shady</light>
????<price>9.37</price>
????<availability>03/06/2006</availability>
????<indoor>true</indoor>
??</plant>
??<plant>
????<common>Marsh?Marigold</common>
????<botanical>Caltha?palustris</botanical>
????<zone>4</zone>
????<light>Mostly?Sunny</light>
????<price>6.81</price>
????<availability>05/17/2006</availability>
????<indoor>false</indoor>
??</plant>
??<plant>
????<common>Cowslip</common>