Event handles in Repeat components
2469 ワード
<?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 appication looks like this:The code in the followwing example uses the
getRepeaterItem()
method to display a specific URL for each Button control that the user clicks.The Button controlls must share a command-driven click hander,becausexyou cannefine privethe getRepeaterItem()
method lets you change the functionlity 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 chice、the relevant product page loads in a new browser window.<スクリプトtype=「text/javascript」>