Apache flex 通过Flex4中的actionscript选择DropDownList的值

Apache flex 通过Flex4中的actionscript选择DropDownList的值,apache-flex,drop-down-menu,default-value,arraycollection,Apache Flex,Drop Down Menu,Default Value,Arraycollection,我相信这很简单,但我已经搜索了一段时间如何使用actionscript选择DropDownList元素。在这个场景中,我希望能够基于ddlabel或ddlData指定selectedItem <?xml version="1.0" encoding="utf-8"?> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.events.F

我相信这很简单,但我已经搜索了一段时间如何使用actionscript选择DropDownList元素。在这个场景中,我希望能够基于ddlabel或ddlData指定selectedItem

<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        protected var timezonesArray:ArrayCollection = new ArrayCollection([
            {ddlLabel:"Eastern Time", ddlData:"EST"}, 
            {ddlLabel:"Central Time", ddlData:"CST"}, 
            {ddlLabel:"Mountain Time", ddlData:"MST"}, 
            {ddlLabel:"Pacific Time", ddlData:"PST"}
        ]);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            // I'm looking to select an element via actionscript here, based on ddlLabel or ddlData
        }

    ]]>
</fx:Script>

<mx:Form>
    <s:DropDownList id="ddlTimezones" dataProvider="{timezonesArray}" labelField="ddlLabel"/>
</mx:Form>

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        protected var timezonesArray:ArrayCollection = new ArrayCollection([
            {ddlLabel:"Eastern Time", ddlData:"EST"}, 
            {ddlLabel:"Central Time", ddlData:"CST"}, 
            {ddlLabel:"Mountain Time", ddlData:"MST"}, 
            {ddlLabel:"Pacific Time", ddlData:"PST"}
        ]);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            // I'm looking to select an element via actionscript here, based on ddlLabel or ddlData
        }

    ]]>
</fx:Script>

<mx:Form>
    <s:DropDownList id="ddlTimezones" dataProvider="{timezonesArray}" labelField="ddlLabel"/>
</mx:Form>


有几种方法可以做到这一点-如果需要使用标签或值,可以像下面这样循环arraycollection:

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        protected var timezonesArray:ArrayCollection = new ArrayCollection([
            {ddlLabel:"Eastern Time", ddlData:"EST"}, 
            {ddlLabel:"Central Time", ddlData:"CST"}, 
            {ddlLabel:"Mountain Time", ddlData:"MST"}, 
            {ddlLabel:"Pacific Time", ddlData:"PST"}
        ]);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            // I'm looking to select an element via actionscript here, based on ddlLabel or ddlData
        }

    ]]>
</fx:Script>

<mx:Form>
    <s:DropDownList id="ddlTimezones" dataProvider="{timezonesArray}" labelField="ddlLabel"/>
</mx:Form>
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
    var searchTerm:String = "EST";
    var result:* = null;
    for each(var zone:* in timeZonesArray)
    {
        if(searchTerm == zone.ddlData)
        {
            result = zone;
            break;
        }
    }
    ddlTimezones.selectedItem = result;
}
但是,如果您单独保留对时区的引用,或者您从应用程序的其他部分获取它们,您可以更干净地执行此操作:

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        protected var timezonesArray:ArrayCollection = new ArrayCollection([
            {ddlLabel:"Eastern Time", ddlData:"EST"}, 
            {ddlLabel:"Central Time", ddlData:"CST"}, 
            {ddlLabel:"Mountain Time", ddlData:"MST"}, 
            {ddlLabel:"Pacific Time", ddlData:"PST"}
        ]);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            // I'm looking to select an element via actionscript here, based on ddlLabel or ddlData
        }

    ]]>
</fx:Script>

<mx:Form>
    <s:DropDownList id="ddlTimezones" dataProvider="{timezonesArray}" labelField="ddlLabel"/>
</mx:Form>
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;


var EST:Object = {ddlLabel:"Eastern Time", ddlData:"EST"};
var CST:Object = {ddlLabel:"Central Time", ddlData:"CST"};
var MST:Object = {ddlLabel:"Mountain Time", ddlData:"MST"};
var PST:Object = {ddlLabel:"Pacific Time", ddlData:"PST"};

[Bindable]
protected var timezonesArray:ArrayCollection = new ArrayCollection([
    EST, 
    CST, 
    MST, 
    PST
]);

protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
    ddlTimezones.selectedItem = EST;
}

这样,您就不必担心询问列表中的每一个对象,因为您停留在整个对象的层次上,而不是触及它们。如果您想用类定义替换JSON样式的对象列表,如果您开始需要存储关于时区的更复杂的信息,那么它也会让您变得更容易。

我喜欢第二种解决方案+1. :)是的,非常感谢,将ArrayCollection的每个部分都变成单独的对象简直太棒了。它在许多方面扩展了可能性。再次感谢!:-)@杰森·汤恩,谢谢你的提醒。我是这个论坛的新手。真的很喜欢它的工作方式:-)可能重复的问题可能是类似的,但我认为它的接近程度不足以保证结束。丹提供的解决方案肯定不同于另一个问题中提供的解决方案。
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        protected var timezonesArray:ArrayCollection = new ArrayCollection([
            {ddlLabel:"Eastern Time", ddlData:"EST"}, 
            {ddlLabel:"Central Time", ddlData:"CST"}, 
            {ddlLabel:"Mountain Time", ddlData:"MST"}, 
            {ddlLabel:"Pacific Time", ddlData:"PST"}
        ]);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            // I'm looking to select an element via actionscript here, based on ddlLabel or ddlData
        }

    ]]>
</fx:Script>

<mx:Form>
    <s:DropDownList id="ddlTimezones" dataProvider="{timezonesArray}" labelField="ddlLabel"/>
</mx:Form>