1

当用户从第一个下拉框中选择一个类别时,我希望根据第一个下拉框的选择来更新第二个下拉列表。

我创建了多个 ArrayCollections,其名称设置为第一个下拉列表的“数据”值,例如:

[Bindable]
public var countries:ArrayCollection = new ArrayCollection([
                {label:"USA",data:"USA"}, 
                {label:"Canada",data:"Canada"},  ]);

[Bindable]
public var USA:ArrayCollection = new ArrayCollection([
                {label:"state1",data:"state1"}, 
                {label:"state2",data:"state2"}, 
                {label:"state3",data:"state3"}, ]);

[Bindable]
public var Canada:ArrayCollection = new ArrayCollection([
                {label:"statea",data:"statea"}, 
                {label:"state2b",data:"stateb"}, 
                {label:"statec",data:"statec"}, ]);

[Bindable]
public var Italy:ArrayCollection = new ArrayCollection([
                {label:"statex",data:"statex"}, 
                {label:"statey",data:"statey"}, 
                {label:"statez",data:"statez"}, ]);

列表实现为:

<mx:FormItem label="State:" color="black" required="true">
<s:DropDownList id="state" prompt="Select State" dataProvider="{country.selectedItem.data}">
</s:DropDownList>
</mx:FormItem>

任何想法如何实现这一目标?基本上我需要知道如何正确更新列表的数据提供程序以使用正确的数组集合。

4

2 回答 2

2

您可以改为将数据更改为嵌套,更像这样:

    [Bindable]
    public var countries:ArrayCollection = new ArrayCollection([
            {label:"USA", data:             // country
                new ArrayCollection([       // its states, nested
                    {label:"state1",data:"state1"}, 
                    {label:"state2",data:"state2"}, 
                    {label:"state3",data:"state3"} 
                ])
            },
            {label:"Canada",data:           // country
                new ArrayCollection([       // its states, nested
                    {label:"state1",data:"state1"}, 
                    {label:"state2",data:"state2"}, 
                    {label:"state3",data:"state3"} 
                ])            
            }
            ]);

然后像你一样绑定选定的项目:

<mx:FormItem label="State:" color="black" required="true">
    <s:DropDownList id="state" prompt="Select State" 
        dataProvider="{country.selectedItem.data}">
    </s:DropDownList>
</mx:FormItem>
于 2011-07-07T00:25:26.287 回答
1

监听第一个下拉列表的更改事件并执行以下操作:

state.dataProvider = this[country.selectedItem.data]

'this' 关键字引用当前组件,使用括号语法将使用状态 dataProvider 中的字符串值来访问组件上的变量。

于 2011-07-06T21:37:52.900 回答