extjs学习笔记(六) grid中数据的保存,添加和删除
? 在上一个系列当中,我们学习了如何对grid中的内容进行编辑,但是编辑的结果我们并没有保存,这在实际的应用中是没有什么意义的。在有些情况下,除了编辑之外,还要通过grid进行数据的增加和删除,这两个操作也涉及到对于数据的保存。在这个系列里边,我们将学习如何保存数据以及通过grid对数据进行增加和删除。
??? 我们在前边的学习过程中已经知道,grid其实只是显示数据,它通过配置参数store来获得数据,这个参数需要的是Store类或者其子类的一个对象,里边封装了我们需要的数据。我们现在应该已经比较熟悉Store类了,这次我们需要使用它的一个属性modified,里边保存了被修改过的记录的集合。我们通过把上个系列中的例子改变一下来看看如何保存数据:
///<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,
????????listeners:?
{
????????????"afterEdit":?
{
????????????????fn:?afterEdit,
????????????????scope:?this
????????????}
????????},
????????tbar:?[
{
????????????text:?"保存",
????????????handler:?function()?
{
????????????????var?modified?=?store.modified;
????????????????updateData(modified);
????????????}
????????}]
????});
????//?触发数据的加载
????store.load();
????//发送数据到服务器端进行更新
????function?updateData(modified)?
{
????????var?json?=?[];
????????Ext.each(modified,?function(item)?
{
????????????json.push(item.data);
????????});
????????if?(json.length?>?0)?
{
????????????Ext.Ajax.request(
{
????????????????url:?"EditGrid.aspx",
????????????????params:?
{?data:?Ext.util.JSON.encode(json)?},
????????????????method:?"POST",
????????????????success:?function(response)?
{
????????????????????Ext.Msg.alert("信息",?"数据更新成功!",?function()?
{?store.reload();?});
????????????????},