When working with repeaters, one good technique is to create a custom
object, and repeat that. Pass into that component the entire
dataProvider Item. It will make the repeater code much cleaner (no
currentItem stuff), plus, if you also implement a getter for the item,
you can access that in an event handler via the target property, no
worrying about getRepeaterItem. Below is some sample code.
Tracy
Goal: Display a list of items using a complex display for each item, and
have each of those items behave like a menu element and respond to a
click anywhere on the item by running a handler function.
One solution is to use a Repeater with a custom component
In the main app or component, declare the Repeater, and the click
handler function.
mx:Application ...
mx:Script![CDATA[
import MyRepeaterItem;
...
private function onRPItemClick(oEvent:Event):void
{
var xmlItem:XML = XML(oEvent.target);
}//onRPItemClick
]]/mx:Script
mx:VBox ...
mx:Repeater id=rp dataProvider={_xmlData} ...
!-- Note we pass in the entire currentItem, and define a click
handler --
MyRepeaterItem xmlItem={rp.currentItem}
itemClick=onRPItemClick(event) .../
/mx:Repeater
/mx:VBox
/mx:Application
And in the component, MyRepeaterItem.mxml:
?xml version=1.0 encoding=utf-8?
mx:HBox mouseChildren=false buttonMode=true click=onClick(event)
!-- The metadata tag below allows us to define an itemClick handler
in mxml, as in the code above --
mx:Metadata
[Event(name=itemClick, type=flash.events.Event)]
/mx:Metadata
mx:Script![CDATA[
[Bindable]private var _xmlItem:XML;
/** Setter function */
public function set xmlItem(xml:XML):void
{
_xmlItem = xml;
//do any special, non-bound ui stuff you want
}//set xmlItem
/** Getter function */
public function get xmlItem():XML
{
return _xmlItem;
}//get xmlItem
/** Outer VBox Click handler function */
private function onClick():void
{
dispatchEvent(new Event(itemClick,false); //we do not need/want
this event to bubble
}//onClick
]]/mx:Script
!-- Now declare the Item UI --
mx:Text id=lbDescription text=[EMAIL PROTECTED]
width=100% height=100% /
/mx:HBox
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of grimmwerks
Sent: Saturday, April 19, 2008 10:28 AM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] naming children of repeater problem with xml
Ah - think I might have a way around it with getRepeaterItem()
click=Alert.show(String(event.currentTarget.getRepeaterItem()[EMAIL
PROTECTED]) +
' pressed')
On Apr 19, 2008, at 10:12 AM, grimmwerks wrote:
I've read this article on giving the id of a child the currentItem.Name
of a repeater:
http://www.adobe.com/devnet/air/flex/articles/xml_viewer_on_air_04.html
http://www.adobe.com/devnet/air/flex/articles/xml_viewer_on_air_04.html
The trouble is my xml is using attributes such as this:
circle type=work x=100 y=300 percent=25 /
Now on the whole most of what I'm doing to get at attributes works
EXCEPT for the id:
mx:Repeater id=rep
dataProvider={CircleData.getInstance().currentCircle.circle}
mx:Button x={Number([EMAIL PROTECTED])}
y={Number([EMAIL PROTECTED])}
width={(Number([EMAIL PROTECTED])*defaultSize)}
height={(Number([EMAIL PROTECTED])*defaultSize)}
styleName={String([EMAIL PROTECTED])}
label={String([EMAIL PROTECTED]).toUpperCase()}
useHandCursor={CircleData.getInstance().isEdit}
buttonMode={CircleData.getInstance().isEdit}
mouseMove=mouseMoveCircle(event);circleOffsetClick(event)
toolTip={'At the moment '+
String([EMAIL PROTECTED])+' is '[EMAIL PROTECTED]'% of my
life at the moment'} addedEffect=blur /
/mx:Repeater
I can't get the currentItem type and making that the id by
id={String([EMAIL PROTECTED])}
I can't even 'fake it' with the currentIndex or anything:
id={String(rep.currentIndex)}.
Is there any way around this?