Event handlers in Repeater components

<?xml version="1.0"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script> 
        <![CDATA[
             public function clicker(cName:String):void { 
                  foolabel.text=cName; 
             }    
        ]]>
    </mx:Script> 

    <mx:Label id="foolabel" text="foo"></mx:Label> 
 
    <mx:Model id="data"> 
        <color>
            <colorName>Red</colorName> 
            <colorName>Yellow</colorName> 
            <colorName>Blue</colorName>
        </color>
    </mx:Model> 
    <mx:ArrayCollection id="myAC" source="{data.colorName}"/>

    <mx:Repeater id="myrep" dataProvider="{myAC}"> 
        <mx:Button click="clicker(event.currentTarget.getRepeaterItem());"
            label="{myrep.currentItem}"/> 
    </mx:Repeater> 
</mx:Application>

After the user clicks the Yellow button, the application looks like this:


The code in the following example uses the getRepeaterItem() method to display a specific URL for each Button control that the user clicks. The Button controls must share a common data-driven click handler, because you cannot use binding expressions inside event handlers. However, the getRepeaterItem() method lets you change the functionality for each Button control.

<?xml version="1.0"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            [Bindable]
            public var dp:Array = [ { label: "Flex",
                url: "http://www.adobe.com/flex" },
                { label: "Flash", url: "http://www.adobe.com/flash" } ];
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="myAC" source="{dp}"/>
    <mx:Repeater id="r" dataProvider="{myAC}">
        <mx:Button label="{r.currentItem.label}" click="navigateToURL(new
            URLRequest(event.currentTarget.getRepeaterItem().url));"/>
    </mx:Repeater>
</mx:Application>

When executed, this example yields two buttons: one for Flex and one for Flash. When you click the button of your choice, the relevant product page loads in a new browser window.


<script type="text/javascript"></script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值