4,328
社区成员
发帖
与我相关
我的任务
分享
<?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">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
protected function button1_clickHandler(event:MouseEvent):void
{
var user_code = "01";
//如何根据user_code的值去选中 ComboBox中的一项?
//下面的代码该何写 ,user_code的值是不确定的。。。
}
[Bindable]
private var typeList:ArrayCollection=new ArrayCollection([
{data:"01",name:"CHEN",},{data:"02",name:"WANG"},{data:"03",name:"ZHANG"}]
);
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:ComboBox x="165" y="115" dataProvider="{typeList}" labelField="name" id="cmb"/>
<s:Button x="215" y="56" label="按钮" click="button1_clickHandler(event)"/>
</s:Application>
var index:uint = 0;
for (var i:uint=0; i < typeList.length; i++){
if (user_code == typeList.getItemAt(i).data){
index = i;
break;
}
}
cmb.selectedIndex = index;
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12">
<mx:Script>
<![CDATA[
import mx.containers.Canvas;
import mx.collections.ArrayCollection;
[Bindable]
private var _testSouce:ArrayCollection = new ArrayCollection([
{index:"hello", label:"This is hello"},
{index:"ok", label:"Not so bad"},
{index:"bad", label:"Terrible"},
{index:"bye", label:"Leave"}
]);
private function onBtnTestClick(event:Event):void {
var tiText:String = tiInput.text;
if(tiText != "") {
var targetIndex:int = -1;
for(var i:int = 0; i < _testSouce.length; ++ i) {
if(_testSouce.getItemAt(i).index == tiText) {
targetIndex = i;
break;
}
}
if(targetIndex != -1) {
cb.selectedIndex = targetIndex;
}
}
}
]]>
</mx:Script>
<mx:Panel width="80%" height="80%" layout="vertical" horizontalAlign="center">
<mx:ComboBox id="cb" dataProvider="{_testSouce}" width="400"/>
</mx:Panel>
<mx:HBox horizontalAlign="center">
<mx:TextInput id="tiInput" width="100"/>
<mx:Button id="btnTest" label="clickMe" click="onBtnTestClick(event)"/>
</mx:HBox>
</mx:Application>