开发者

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>

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?

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();

I tried this: MyComboBox.selectedItem.label="Label 1" But it's not working. Any suggestions?

Thank you.

-Laxmidi开发者_如何学编程


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();

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜