Displaying images from an XML file using the Repeater component

by Peter deHaan on October 3, 2007

in Repeater, XML, Zoom

The following example shows you how to use the Repeater component to display images loaded from an external XML file.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/03/displaying-images-from-an-xml-file-using-the-repeater-component/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:XML id="imagesXML" source="images.xml" />

    <mx:Zoom id="zoom" />

    <mx:HBox id="hBox">
         <mx:Repeater id="rep"
                 dataProvider="{imagesXML.image}">
            <mx:Image source="{rep.currentItem.@src}"
                    toolTip="{rep.currentIndex}:{rep.currentItem.@alt}"
                    completeEffect="{zoom}" />
        </mx:Repeater>
    </mx:HBox>

</mx:Application>

images.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/03/displaying-images-from-an-xml-file-using-the-repeater-component/ -->
<images>
    <image src="images/Button.png" alt="Button" />
    <image src="images/ButtonBar.png" alt="ButtonBar" />
    <image src="images/CheckBox.png" alt="CheckBox" />
    <image src="images/ColorPicker.png" alt="ColorPicker" />
    <image src="images/ComboBox.png" alt="ComboBox" />
    <image src="images/DataGrid.png" alt="DataGrid" />
    <image src="images/DateChooser.png" alt="DateChooser" />
    <image src="images/DateField.png" alt="DateField" />
</images>

View source is enabled in the following example.

{ 8 comments… read them below or add one }

1 Michael b October 4, 2007 at 7:29 am

Looks good :)

Any chance you know how to get icons from a resource manager and NOT embedded ?

Reply

2 Waqas August 6, 2008 at 1:20 am

Wow cool…!

It helped me a lot… Flexexamples.com ROCKS!

Reply

3 Waqas August 7, 2008 at 9:51 pm

How can i get the name of the image on click ? I mean the name of the selected value?

Reply

4 robbeck December 29, 2008 at 3:11 pm

Can I add a link to the image to take me to anothe web page or site and if so how?

Reply

5 Flexouille January 16, 2009 at 5:06 am

Can you give the way to put a progressbar for each image load plz ?
I try with id on

Reply

6 G January 21, 2009 at 6:40 am

Im having difficulties.

When I embed imagess using : “”
it works FINE.

But When I use “”
then the resulting swf embeded into a HTML page does not show…

Irrespective of pulling images from XML or even staticly as I’ve shown above.

I have a feeling it’s something to do with ‘use-network’… I’ve tried this approach to no avail (setting it to true and false.)

PLEASE SOMEONE Hlep Me!

Alternatively knowing that ” synatx works fine when using the swf on my webroot…… how can I edit the below XML repeater Image syntax

source=”{rep.currentItem.@src}” to a version which EMBEDS the image.

Hence the swf works (displays images) when I use EMBED (hence embeding images at COMPILE TIME) but when using the RUN TIME version [ source=”{rep.currentItem.@src}” ] then it wont work for me.

Please someone hit me back.

Regards
g’

Reply

7 Tim Wilson October 20, 2009 at 12:27 am

How would I pass the currentIndex to a function??

click=”myFunction(rr.currentIndex)” does not work…

Reply

8 Peter deHaan October 20, 2009 at 7:01 am

@Tim Wilson,

Not sure if it helps, but you could try holding the repeater index in a control’s data property, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/03/displaying-images-from-an-xml-file-using-the-repeater-component/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
 
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
 
            private function myFunction(idx:uint):void {
                Alert.show(idx.toString());
            }
        ]]>
    </mx:Script>
 
    <mx:ArrayCollection id="arrColl" source="[One,Two,Three,Four,Five]" />
 
    <mx:HBox id="hBox">
        <mx:Repeater id="rr" dataProvider="{arrColl}">
            <mx:Button label="{rr.currentItem}"
                    data="{rr.currentIndex}"
                    click="myFunction(event.currentTarget.data);" />
        </mx:Repeater>
    </mx:HBox>
 
</mx:Application>

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: