Flex:在ComboBox中编程设置选择的项目 [英] Flex: Programatically Setting the Chosen Item in a ComboBox
问题描述
我需要一些帮助,在组合框中以编程方式设置所选项。
I need some help programatically setting the selected item in a combobox.
我有一个这样的组合框:
I've got a combobox like this:
<mx:ComboBox id="MyComboBox" change="puzzleHandler(event);" prompt="Make a Selection">
<mx:ArrayCollection id="myDP">
<mx:Object id="first" label="Label 1" series="2" pageTitle="Title 1"/>
<mx:Object id="second" label="Label 2" series="7" pageTitle="Title 2"/>
<mx:Object id="third" label="Label 3" series="9" pageTitle="Title 3"/>
</mx:ArrayCollection>
</mx:ComboBox>
我有一个关于深层链接的函数。如果有人放入网址:www.mysite.com/#view=2,他们将被带到网站的适当部分(没有在组合框中选择标签2)。我如何以编程方式设置comboBox,以便它对应于它正在查看的用户?
I've got a function that regards deep linking. If someone puts in the url: www.mysite.com/#view=2 they'll be taken to the appropriate part of the site (without having selected Label 2 in the comboBox). How do I set the comboBox programatically, so that it corresponds with what the user it looking at?
在我的函数的switch语句中,我想设置comboBox为标签对应于视图。如果view = 2,那么组合框应该显示Label 2。
In my function's switch statement, I want to set the comboBox to the label that corresponds with the view. If "view=2" then the comboBox should show "Label 2" as selected.
case "view=1":
MyComboBox.selectedItem.label="Label 1";
parseUrl();
case "view=2":
MyComboBox.selectedItem.label="Label 2";
parseUrl();
case "view=3":
MyComboBox.selectedItem.label="Label 3";
parseUrl();
我试过这样:MyComboBox.selectedItem.label =Label 1但它不工作。有什么建议么?
I tried this: MyComboBox.selectedItem.label="Label 1" But it's not working. Any suggestions?
谢谢。
-Laxmidi
推荐答案
您不想更改selectedItem的对象;您要更改selectedItem或selectedIndex。尝试此操作:
You don't want to change the selectedItem's object; you want to change the selectedItem or the selectedIndex. Try this:
case "view=1":
MyComboBox.selectedIndex=0;
parseUrl();
case "view=2":
MyComboBox.selectedIndex=1;
parseUrl();
case "view=3":
MyComboBox.selectedIndex=2;
parseUrl();
如果你想设置selectedItem而不是selectedIndex,你必须循环遍历dataProvider才能找到实际项目基于case / URL值。类似这样:
IF you want to set the selectedItem instead of the selectedIndex you'll have to loop over dataProvider to find the actual item based on the case / URL value. Something like this:
for each(var tempObject : Object in myList.dataProvider){
if(tempObject.label == urlValue){
MyComboBox.selectedItem = tempObject;
break;
}
}
第二种方法是更灵活的长期。
The second approach is more flexible long term.
这篇关于Flex:在ComboBox中编程设置选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!