【问题标题】:Flex: Programatically Setting the Chosen Item in a ComboBoxFlex:以编程方式在组合框中设置所选项目
【发布时间】:2011-01-26 19:26:34
【问题描述】:

我需要一些帮助,以编程方式在组合框中设置所选项目。

我有一个这样的组合框:

<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)。如何以编程方式设置组合框,使其与用户查看的内容相对应?

在我的函数的 switch 语句中,我想将组合框设置为与视图对应的标签。如果“view=2”,则组合框应显示“标签 2”为选中状态。

    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" 但它不起作用。有什么建议么?

谢谢。

-拉克西米迪

【问题讨论】:

    标签: apache-flex combobox


    【解决方案1】:

    您不想更改 selectedItem 的对象;您想要更改 selectedItem 或 selectedIndex。试试这个:

    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 值查找实际项目。像这样的:

    for each(var tempObject : Object in myList.dataProvider){
      if(tempObject.label == urlValue){
        MyComboBox.selectedItem = tempObject;
        break;
     }
    }
    

    从长远来看,第二种方法更灵活。

    【讨论】:

    • 嗨,Flextras.com,比你的帮助。我选择了选项 2,效果很好。谢谢!
    • @Laxmidi 很高兴为您提供帮助。不要忘记接受这个作为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多