Flex:以编程方式设置组合框中的所选项目 [英] 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).如何以编程方式设置组合框,使其与用户查看的内容相对应?
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 语句中,我想将组合框设置为与视图对应的标签.如果view=2",则组合框应显示标签 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 以根据大小写/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:以编程方式设置组合框中的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!