<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" x="0" y="0" fontSize="12">
<!-- editable="true" 只有编辑状态下才显示绑定的ComboBox-->
<mx:DataGrid id="dgv" editable="true" dataProvider="{ScenicSpots}" x="25" y="10" width="578" height="210">
<mx:columns>
<mx:DataGridColumn headerText="景点名称" dataField="景点名称"/>
<mx:DataGridColumn headerText="所在行政区"
dataField="景点行政区名称"
editorDataField="value">
<mx:itemEditor>
<mx:Component>
<mx:HBox>
<mx:Script>
<![CDATA[
public function get value():Object
{
if(cmb.selectedItem == null)return null;
var item1 :XML = cmb.selectedItem as XML;
var xzqdm : String = item1.children()[0]; //行政区代码
var xzqmc : String = item1.children()[1]; //行政区名称
data.景点行政区代码 = xzqdm;// data DataGrid上面的当前行的数据
return xzqmc;
}
override public function set data(value:Object):void
{
//必须要给data复制,否则会导致程序崩溃
super.data=value;
//给combobox赋初始值
for each(var item1 :XML in cmb.dataProvider)
{
trace(item1.children()[0]);
var xzqdm : String = item1.children()[0];
if(xzqdm == value.景点行政区代码){ //value DataGrid上面的当前行的数据
cmb.selectedItem=item1;
}
}
}
//下面的 {outerDocument}标识引用外部的数据 在这里写注释比较方便:)
]]>
</mx:Script>
<mx:ComboBox id="cmb" labelField="行政区名称" dataProvider="{outerDocument.Xzqs}"
width="100%"/>
</mx:HBox>
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
private var ScenicSpotsXML : XML =
<list>
<ScenicSpot>
<景点名称>黄山</景点名称>
<景点行政区代码>230312</景点行政区代码>
<景点行政区名称>黄山市</景点行政区名称>
</ScenicSpot>
<ScenicSpot>
<景点名称>泰山</景点名称>
<景点行政区代码>240103</景点行政区代码>
<景点行政区名称>泰安市</景点行政区名称>
</ScenicSpot>
</list>;
[Bindable]
private var ScenicSpots:XMLListCollection=new XMLListCollection(ScenicSpotsXML.ScenicSpot);
//下面的格式不好,最好写成这样的格式 <XZQ 行政区代码='' 行政区名称=''>
public var XZQXML : XML =
<list>
<XZQ>
<行政区代码>230312</行政区代码>
<行政区名称>黄山市</行政区名称>
</XZQ>
<XZQ>
<行政区代码>240103</行政区代码>
<行政区名称>泰安市</行政区名称>
</XZQ>
<XZQ>
<行政区代码>110102</行政区代码>
<行政区名称>北京海淀</行政区名称>
</XZQ>
<XZQ>
<行政区代码>420803</行政区代码>
<行政区名称>湖北荆门</行政区名称>
</XZQ>
</list>;
[Bindable]
public var Xzqs:XMLListCollection=new XMLListCollection(XZQXML.XZQ);
private function btnClick(evt:MouseEvent):void{
trace(dgv.dataProvider);
}
]]>
</mx:Script>
<mx:Button x="522" y="245" label="查看结果" click="btnClick(event);"/>
</mx:Application>