在 Flex 4 中通过动作脚本选择 DropDownList 的值

发布于 2024-10-21 00:51:22 字数 1058 浏览 1 评论 0原文

我确信这很简单,但我一直在寻找如何使用动作脚本选择 DropDownList 元素。在这种情况下,我希望能够基于 ddlLabel 或 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>

I'm sure this is a easy one but I've been searching for a while how to select a DropDownList element with actionscript. In this scenario, I'd like to be able to specify the selectedItem based either on ddlLabel or ddlData

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-10-28 00:51:22

有几种方法可以做到这一点 - 如果您需要使用标签或值来执行此操作,您可以像这样循环遍历数组集合:

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;
}

但是,如果您单独保留对时区的引用或者从以下位置获取它们在应用程序的其他部分,您可以做得更干净:

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 样式对象列表,如果您开始需要存储有关时区的更复杂的信息,它也会变得更容易。

There are a couple ways to do this - if you need to do it using the label or the value, you can loop though the arraycollection like this:

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;
}

However, if you keep a reference to the time zones individually or you're getting them from some other part of the app, you can do it more cleanly:

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;
}

This way, you don't have to worry about interrogating every object in the list, because you're staying at the level of whole objects rather than reaching into them. It also makes it easier if you want to replace your list of JSON style objects with a class definition, if you start needing to store more complicated information about time zones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文