Flex combox测试

本文详细探讨了Flex MXML中组合框与XML数据绑定的相关问题,包括初始化方法中XML数据转换的问题、组合框选择项更新的异常现象,以及如何在MXML组件中嵌套使用树形结构。通过实例分析,提供了解决方案和优化建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[code="java"][/code]<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600" creationComplete="init()">

<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>

<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.IList;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
[Bindable]
private var monitorItemData:XML=<root>
<node label="负载均衡器" type="0">
<item label="apache" value="1"/>
<item label="loadbalance" value="2"/>
</node>
<node label="应用服务器" type="0">
<item label="apusic" value="1"/>
<item label="tomcat" value="2"/>
<item label="weblogic" value="3"/>
</node>
<node label="数据库" type="0">
<item label="mysql" value="1"/>
<item label="db2" value="2"/>
<item label="oracle" value="3"/>
</node>

</root>;

var data:ArrayCollection = new ArrayCollection();
var provider:ArrayCollection;
var xmlListCollection:XMLListCollection = new XMLListCollection();
var selectedVm:String;
var selectedSpecifyItem:String;
var obj:Object;//用于测试获取comboBox选中值

private function init():void{//将XML转换为ArrayCollection类型的数据
xmlListCollection = new XMLListCollection(monitorItemData.children());
data.source = xmlListCollection.toArray();
//第二种方式(失效?)
// var xmlList:XMLList = new XMLList();
// xmlList = monitorItemData.elements("node");
// xmlListCollection.source = xmlList;
// var myData:ArrayCollection = new ArrayCollection(xmlListCollection.toArray());

}

private function vmChangeHandler(e:Event):void{
var xmlListCollection:XMLListCollection = new XMLListCollection();
xmlListCollection.source = e.target.selectedItem.item;//将XMLList转换为XMLListCollection

provider = new ArrayCollection(xmlListCollection.toArray());
specifyItem.dataProvider = provider;
//specifyItem.selectedIndex=0;第二个comboBox的首项不实时更新
selectedVm = e.target.selectedItem.@label;
specifyItem.selectedIndex = -1;
}

private function specifyItemChangeHandler(e2:Event):void{
selectedSpecifyItem = specifyItem.selectedItem.@label;
yourSelect.text=selectedVm+selectedSpecifyItem;

}
]]>
</fx:Script>

<s:Group width="30%" height="45%" x="153" y="33">
<s:Panel title="添加监控项" width="482" height="100%">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Group width="100%" height="20%">
<s:Label text="虚拟设备:" id="vm" x="33" y="6"/>
<s:ComboBox x="109" y="1" width="124" id="vmCombox" labelField="@label" dataProvider="{data}" change="vmChangeHandler(event)"/>
<s:Label x="250" y="6" text="监控指标:" id="monitorItem"/>
<s:ComboBox x="311" y="1" width="124" id="specifyItem" labelField="@label" change="specifyItemChangeHandler(event)" />

</s:Group>
<s:Line width="100%" height="2">
<s:stroke>
<s:SolidColorStroke color="0x000000" weight="2"/>
</s:stroke>
</s:Line>
<s:Group width="100%" height="66%" id="drop">
<s:Label text="选择的监控项:" id="selectedItems" x="10" y="0"/>
<s:List height="153" width="161" x="105" y="0">
</s:List>

<s:Button label="删除" x="278" y="34"/>

<s:Button x="277" y="83" label="删除所有" id="dropAll"/>
</s:Group>
<s:Line width="100%" height="2">
<s:stroke>
<s:SolidColorStroke color="0x000000" weight="2"/>
</s:stroke>
</s:Line>
<s:Group width="100%" height="14%">
<s:Button label="确定" x="308" y="-4" id="submit"/>
<s:Button x="400" y="-4" label="取消" id="cancel"/>
<s:Label x="38" y="-1" text="你选中的为:"/>
<s:Label x="128" y="0" id="yourSelect"/>
</s:Group>
</s:Panel>
</s:Group>
</s:Application>

——————————————————————补充————————————
疑问:
1、 为什么init()方法中的第二种方式失效?
2、 vmChangeHandler方法中的specifyItem.selectedIndex=0所导致的奇怪问题
3、 扩展之comboBox中嵌套tree

——————————————实验——————————————
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<fx:XML id="xmlPC_linked" source="PC_linked.xml"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;

var xmlListCollection:XMLListCollection = new XMLListCollection();
var data:ArrayCollection;// = new ArrayCollection();
var xmlList:XMLList = new XMLList();

public function init():void{
//xmlListCollection = new XMLListCollection(xmlPC_linked.children());
//data.source = xmlListCollection.toArray();
xmlList = xmlPC_linked.elements("node");
xmlListCollection.source = xmlList;
data = new ArrayCollection(xmlListCollection.toArray());
}

public function changeHandler(e:Event):void
{
//showSelectedItem.text = ComboBox(e.target).selectedItem.node.label;

city.dataProvider=e.target.selectedItem.item;
city.selectedIndex=0;
}
]]>
</fx:Script>
<s:Panel x="262" y="144" width="296" height="200">
<s:ComboBox id="pro" dataProvider="{data}" labelField="@label" change="changeHandler(event)"/>
<s:ComboBox id="city" labelField="@label"/>
</s:Panel>
</s:Application>
这个mxml所采用的xml是来自于外部, 在这种情况下方式一与方式二都失效。然而我将此mxml放在flex3下编译运行则正确无误... ...

搞什么东东呀...  ... 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值