1.datagrid编辑功能简介
当我们点击datagrid中的一个单元格进行编辑时,可结合使用一些组件,RadioButtonGroup、checkbox、ComboBox等
datagrid的编辑功能必须使用以下组件:editorDataField、itemEditor和itemRenderer。
editorDataField 属性确定包含新数据的项目编辑器的属性,并使用这些新数据更新数据提供程序项目。因为默认项目编辑器是 TextInput 控件,所以 editorDataField 属性的默认值为 "text",以指定 TextInput 的 text 属性包含新项目数据。
itemEditor为DataGrid的单元格编辑器,而itemRenderer则为渲染器,就是说.itemEditor只会在单元格处理编辑状态下才会出现.
itemRenderer则是一直显示(就是网友关心的,自定义DataGrid的列)
datagrid各项数据编辑后的保存
不要一个一个地从datagrid中读取各项的值,最好通过Arraycollection数据源与datagrid进行绑定,如果需要读取编辑后的数值,可以直接读取ArrayCollection。具体看本文《datagrid控件的各项数据可编辑》。
2.editorDataField使用方法
属性描述:
public var editorDataField:String = "text"
项目编辑器的属性名称,其中包含列表项目的新数据。例如,默认的 itemEditor 为 TextInput,因此,editorDataField 属性的默认值为 "text",此值指定 TextInput 控件的 text 属性。
例1:
以上代码说明表示"名称"这一列可通过TextInput控件编辑,并将TextInput控件的text属性值传给DataGridColumn。editorDataField="text"是默认值,可不写。
例2:
dataField="price" editorDataField="value" editable="false">
editorDataField="value"表示将控件NumericStepper 的属性value传给DataGridColumn
3.itemEditor使用方法
属性描述:
public var itemEditor:IFactory
当编辑该列时,调用列的项目编辑器实例的类工厂创建编辑类。
默认值为 new ClassFactory(mx.controls.TextInput).见本文《editorDataField使用方法》
例1:
editable="true"
itemEditor="mx.controls.NumericStepper"
editorDataField="value" />
这里表示,当进入编辑状态时,itemEditor将创建NumericStepper对象,并将NumericStepper的属性与该单元格进行绑定。
如果想进一步指定NumericStepper的取值范围等,可采用如下代码
例2:
如果绑定的对象属性与itemEditor指定控件不能对应,比如数据源中的日期是一个String类型,而编辑时必须用Date类型。可采用如下代码:
dataField="dateString" width="120"
itemEditor="DateEditor"
editorDataField="text" />
下面对DateEditor控件继承mx.controls.DateField控件,并重载set data()方法,并进入编辑状态时,创建一个DateEditor对象,这个对象先将字符串转换成Date对象,再用DateField控件进行编辑。
详见本文的《Flex中的DateField作为DataGrid的itemEditor接收字符串日期》
4.itemRenderer使用方法
itemRenderer有两种用法:自定义控件和直接嵌入代码。
例1:
本例为直接嵌入代码,表示直接嵌入一个控件,可以理解为一个内部类。
editorDataField="value" editable="false">
例2:
本例先自定义一个控件,再引用该控件。
{cb_Render}"/>
不合格
合格
错别字
5.as代码使用方法
大概跟上面类似.,需要注意的是.用代码设置itemRenderer时.接受的类形是ClassFactory.,如果需要给选择的组件
(这里是NumericStepper),需要设置ClassFactory的properties属性.为一个object,代码模拟《控件的各项数据可编辑》
col = new DataGridColumn()
col.headerText = "价格"
col.dataField = "price"
col.editable = false
var itemRenderer:ClassFactory = new ClassFactory(NumericStepper);
itemRenderer.properties = {maximum:1000,minimum:10}
col.itemRenderer = itemRenderer
col.editorDataField = "value"
6.datagrid控件的各项数据可编辑
功能说明:
本例中的datagrid各项数据都可编辑,“序号”不可编辑,“名称”通过文本框编辑,“价格”通过数字下拉框编辑,“描述”通过combobox编辑,“select”通过checkbox编辑。
用户点击“保存”按钮,读取表中各行属性显示。
代码:
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
creationComplete="init()"
minWidth="1024" minHeight="768">
import mx.collections.ArrayCollection;
import mx.controls.*;
import mx.controls.dataGridClasses.*;
private var DataGrid1:DataGrid
public var languageFlag:String = "cn";
[Bindable]
public var dataArr2:ArrayCollection =new ArrayCollection(
[{id:1,name:"苹果",price:100,count:"不合格",selected:false},
{id:2,name:"西瓜",price:50,count:"不合格",selected:true},
{id:3,name:"水蜜桃",price:333,count:"错别字",selected:false}]
);
[Bindable]
public var dataArr:ArrayCollection = new ArrayCollection(
[{label:"不合格", data:"2"},{label:"不合格", data:"3"},
{label:"进口", data:"4"}, {label:"一般", data:"5"}]
);
private function init():void{
}
public function saved():void{
for each(var a:Object in dataArr2){
Alert.show("当前数据为:
" + "序号: " + a.id + ", 名称: " + a.name + ", 数量: " + a.count + ", selected: " + a.selected);
}
}
]]>
不合格
合格
错别字
editable="true" dataProvider="{dataArr2}" y="76" x="10" height="266" width="516">
dataField="price" editorDataField="value" editable="false">
itemEditor="{cb_Render}"/>
7.Flex中的DateField作为DataGrid的itemEditor接收字符串日期
功能说明:
扩展DateField组件处理字符型日期数据
代码:
DateEditor.mxml
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
implements="mx.controls.listClasses.IListItemRenderer"
focusIn="open()">
override public function set data(value:Object):void{
if(listData){
var newDate:Date;
if (value is String){
newDate = new Date(Date.parse(data as String));
super.data=newDate;
}
else if (value is Date){
super.data=value as Date;
}
}
}
]]>
datagrid4.mxml
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768">
import mx.controls.dataGridClasses.DataGridListData;
import mx.events.DataGridEvent;
import mx.collections.ArrayCollection;
import mx.controls.DateField;
import mx.core.ClassFactory;
import mx.controls.dataGridClasses.DataGridListData;
[Bindable]
public var ac:ArrayCollection = new ArrayCollection(
[{dateString:"11/12/2006", contact:"ABD DEF", dt: new Date(2003,10,23)},
{dateString:"11/12/2007", contact:"GHI", dt:new Date(2004,11,2)},
{dateString:"10/10/2007", contact:"JKL MNOP", dt:new Date(2007,4,14)},
{dateString:"09/12/2007", contact:"QRSTUV W XY Z", dt:new Date(2006,1,1)}]);
]]>
itemEditor="DateEditor"
editorDataField="text" />
itemRenderer="mx.controls.DateField"
rendererIsEditor="true"
editorDataField="selectedDate"/>
8.Flex的Datagrid控件双击弹出子窗体编辑
功能说明:
当双击DataGrid的一项时,弹出窗口进行内容编辑.
通过PopUpManager和itemRenderer实现
代码:
主程序
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:local="XautComponent.*"
minWidth="1024" minHeight="768">
editable="false" wordWrap="true"
headerText="Article Name"
itemRenderer="XautComponent.MyRenderer" dataField="articleName"/>
MyRender.mxml
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
implements="mx.controls.listClasses.IDropInListItemRenderer"
toolTip="Double Click to Edit…" doubleClick="callLater(openPopup)"
doubleClickEnabled="true"
text="{txt}">
import mx.controls.DataGrid;
import mx.controls.listClasses.ListData;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.managers.PopUpManager;
import mx.events.FlexEvent;
import XautComponent.Popup;
private var _listData:DataGridListData;
[Bindable]
public var txt:String;
private var pop:Popup;
override public function set data(value:Object):void {
super.data = value;
txt=data[_listData.dataField];
}
override public function get data():Object {
return super.data;
}
override public function get listData():BaseListData
{
return _listData;
}
override public function set listData(value:BaseListData):void
{
_listData = DataGridListData(value);
}
private function openPopup():void{
pop= Popup(PopUpManager.createPopUp(this.owner,Popup,true));
pop.txt=this.txt;
pop.opener=this;
}
public function updateDP(str:String):void{
var myDG:DataGrid=this.owner as DataGrid;
var row:int=_listData.rowIndex+myDG.verticalScrollPosition;
this.data[_listData.dataField]=str;
myDG.dataProvider.itemUpdated(data);
}
]]>
Popup.mxml
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
layout="absolute"
width="350" height="250" showCloseButton="false" creationComplete="centerMe()"
defaultButton="{btnSave}">
import mx.managers.FocusManager;
import mx.managers.PopUpManager;
[Bindable]
public var txt:String;
public var opener:Object;
private function save():void{
(opener as MyRenderer).updateDP(ta.text);
cancel();
}
private function cancel():void{
PopUpManager.removePopUp(this);
returnFocus();
}
private function returnFocus():void{
opener.setFocus();
}
private function centerMe():void{
PopUpManager.centerPopUp(this);
ta.setFocus();
}
]]>
9.Flex的Datagrid控件双击后进行编辑
本例为用户必须双击单元格才能进行编辑,由于datagrid默认是单击进行编辑,必须先继承Datagrid类,添加doubleClick事件处理。
代码:
主程序
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"
xmlns:local="*" >
private var arr:Array = [
{ firstName: "Alex", lastName: "Harui" },
{ firstName: "Gordon", lastName: "Smith" },
{ firstName: "Deepa", lastName: "Subramanian" },
{ firstName: "Matt", lastName: "Chotin" },
{ firstName: "Ely", lastName: "Greenfield" },
{ firstName: "Kevin", lastName: "Lynch" },
{ firstName: "Shantanu", lastName: "Narayan" },
{ firstName: "Joan", lastName: "Lafferty" },
{ firstName: "Ryan", lastName: "Frishberg" },
{ firstName: "Jason", lastName: "Szeto" },
{ firstName: "Mark", lastName: "Anders" },
{ firstName: "Peter", lastName: "Flynn" },
{ firstName: "Heidi", lastName: "Williams" }
];
]]>
DoubleClickDataGrid.as
package
{
import flash.events.MouseEvent;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.EventPriority;
import mx.events.DataGridEvent;
public class DoubleClickDataGrid extends DataGrid
{
public function DoubleClickDataGrid()
{
super();
doubleClickEnabled = true;
}
override protected function mouseDoubleClickHandler(event:MouseEvent):void
{
var dataGridEvent:DataGridEvent;
var r:IListItemRenderer;
var dgColumn:DataGridColumn;
r = mouseEventToItemRenderer(event);
if (r && r != itemEditorInstance)
{
var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
if (columns[dilr.listData.columnIndex].editable)
{
dgColumn = columns[dilr.listData.columnIndex];
dataGridEvent = new DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, false, true);
// ITEM_EDIT events are cancelable
dataGridEvent.columnIndex = dilr.listData.columnIndex;
dataGridEvent.dataField = dgColumn.dataField;
dataGridEvent.rowIndex = dilr.listData.rowIndex + verticalScrollPosition;
dataGridEvent.itemRenderer = r;
dispatchEvent(dataGridEvent);
}
}
super.mouseDoubleClickHandler(event);
}
override protected function mouseUpHandler(event:MouseEvent):void
{
var r:IListItemRenderer;
var dgColumn:DataGridColumn;
r = mouseEventToItemRenderer(event);
if (r)
{
var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
if (columns[dilr.listData.columnIndex].editable)
{
dgColumn = columns[dilr.listData.columnIndex];
dgColumn.editable = false;
}
}
super.mouseUpHandler(event);
if (dgColumn)
dgColumn.editable = true;
}
}
}
参考文献:
1.在dataGrid中使用checkBox和ComboBox的用法示例.
2.DataGrid Example with SelectAll Checkbox. http://www.cnblogs.com/sban/archive/2009/02/28/select-all-checkbox-in-datagrid-example.html
3.DataGrid里嵌入checkBox,增加,删除等控件等操作. http://blog.csdn.net/jinxinxin1314/archive/2009/11/03/4761443.aspx
4.Flex教程/组件详解之一:DataGrid. http://blog.l4cd.net/post-old-42.html
5.Flex中的DateField作为DataGrid的itemEditor接收字符串日期.
6.Flex的Datagrid控件双击编辑问题.
阅读(7137) | 评论(1) | 转发(1) |