flex 获取DataGrid列

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="450" fontFamily="宋体" fontSize="12" height="400">
<mx:Script>
<![CDATA[
import mx.controls.*;
import mx.events.ListEvent;
import mx.controls.dataGridClasses.*;
private var DataGrid1:DataGrid;

[Bindable]//原始数据
public var dataArr:Array = [{id:1,name:"苹果",count:100},
{id:2,name:"西瓜",count:200},
{id:3,name:"水蜜桃",count:50}]

private function addItem():void{
        dataArr.push({id:uiId.value,name:uiName.text,count:uiCount.value})
        DataGrid2.dataProvider = dataArr
}
private function delItem():void{
        dataArr.pop();
        DataGrid2.dataProvider = dataArr
}
private function itemClick(e:ListEvent):void {
Alert.show(DataGrid2.columns[0].headerText);
   var txt:String = "表头为: "+(e.target as DataGrid).columns[e.columnIndex].headerText+"\n"
   txt+="选中第 "+e.columnIndex+" 列\n"
   txt+="选中第 "+e.rowIndex+" 行\n"
   txt+="选中的行的数据为:\n"
   var dat:Object = (e.target as DataGrid).selectedItem
   for(var i:* in dat){
     txt+=" "+i+":"+dat[i]+"\n"
   }
   txt+="选中的单元可格的数据为 "+(e.target as DataGrid).selectedItem[(e.target as DataGrid).columns[e.columnIndex].dataField]+" \n"
   Alert.show(txt)
   }
]]>
</mx:Script>

<mx:DataGrid id="DataGrid2" itemClick="itemClick(event)" dataProvider="{dataArr}" width="430" y="10" x="10" height="208">
<mx:columns>
<mx:DataGridColumn headerText="序号" dataField="id"/>
<mx:DataGridColumn headerText="名称" dataField="name"/>
<mx:DataGridColumn headerText="数量" dataField="count" editorDataField="value"/>
</mx:columns>
</mx:DataGrid>
<mx:NumericStepper id="uiId" x="10" y="241" width="150"/>
<mx:TextInput id="uiName" x="10" y="288" width="150"/>
<mx:NumericStepper id="uiCount" x="10" y="335" width="150"/>
<mx:Label x="10" y="224" text="序号:"/>
<mx:Label x="10" y="271" text="名称:"/>
<mx:Label x="10" y="318" text="数量:"/>
<mx:Button click="addItem()" x="10" y="368" label="添加" width="150"/>
<mx:Button click="delItem()" x="290" y="368" label="删除最后一个" width="150"/>
</mx:Application>

你可能感兴趣的:(xml,Flex)