网上关于flex4.5的DataGrid例子很少,自己做的时候也遇到了很多问题。
应用程序中的DataGrid:(要用自定义的itemEditor)
<s:DataGrid id="osinfoData" width="100%" height="100%" alternatingRowColors="[#eeffff,#ffffff]" editable="true" dataProvider="{infoproperties}"> <s:columns> <s:ArrayList> <s:GridColumn headerText="名称" dataField="name" editable="false"/> <s:GridColumn headerText="值" dataField="value" editable="true" itemEditor="com.control.itemeditor.DataGridEditItemEditor"/> </s:ArrayList> </s:columns> </s:DataGrid>
infoproperties列表格式:(edit控制该行是否可编辑)
var infoproperties:ArrayCollection=new ArrayCollection(); infoproperties.addItem({name:"id",value:"1",edit:false}); infoproperties.addItem({name:"名称",value:"1",edit:true}); infoproperties.addItem({name:"xml名称",value:"1",edit:true}); infoproperties.addItem({name:"注释",value:"1",edit:true}); infoproperties.addItem({name:"创建时间",value:"2012-10-12 08:00:00",edit:false}); infoproperties.addItem({name:"创建人",value:"me",edit:false}); infoproperties.addItem({name:"最后修改时间",value:"2012-10-12 08:00:00",edit:false}); infoproperties.addItem({name:"最后修改人",value:"me",edit:false});
自定义DataGridEditItemEditor.mxml
<?xml version="1.0" encoding="utf-8"?> <s:GridItemEditor xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true"> <fx:Script> <![CDATA[ public var newValue:String; override public function set data(value:Object):void{ super.data=value; txt.text=data.value; txt.editable=data.edit; } ]]> </fx:Script> <s:TextInput id="txt" alpha="1" width="100%" height="100%" change="super.value = txt.text" updateComplete="newValue=txt.text;"/> </s:GridItemEditor>
注意:txt.text,和txt.editable要在set data时设置,写在TextInput标签里,初始化的时候data是null,取不到其中的值