1

When using a List component, instead of a Repeater, how do I transfer a value from the dataprovider to the itemRenderer?

For example, I have an array collection (projectsAC) that contains arrays with the following structure:

projectsAC(
    0:
        Array(
                0:number
                1:string
                2:string
                3:string
                4:XMLList
        )
    1:
        Array(
                0:number
                1:string
                2:string
                3:string
                4:XMLList
        )
    Ect.....
)

I use this array collection as the data provider and a custom module for the item renderer.

How do I access the array values from within the module? I've got the following so far:

<mx:List id="directorsPrepList" dataProvider="{projectsAC}" itemRenderer="modules.project" />

Here's what my projects module looks like right (just for testing purposes)

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:Scheduler="modules.*" layout="absolute" creationComplete="init();">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            [Bindable] public var allData:Array = data as Array;

            private function init():void
            {
                Alert.show(String(allData[0]));
            }
        ]]>
    </mx:Script>
</mx:Module>

The program stalls during it's initialization... see anything wrong?

Brds
  • 1,035
  • 3
  • 17
  • 37

1 Answers1

1

how do I transfer a value from the dataprovider to the itemRenderer?

The list class does this automatically. Each itemRenderer has a data property. And your dataProvider's object is set as part of that data property.

It looks like, given your dataProvider, you are passing each individual the renderer an array. If such is the case, you will have to create your own renderer. It looks like you've done that (modules.project), but you didn't show us the code.

To make the itemRenderer update itself whenever the data updates, you'll have to either override the set data method or listen to the dataChange event.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • So how would i reference them from the projects module? I guess that's really my question here. Thanks! – Brds Jul 28 '11 at 18:41
  • @brds I'm sorry that was unclear in my answer. Access the data property of your projects component to get object of your dataProvider that the renderer is currently displaying. – JeffryHouser Jul 28 '11 at 18:45
  • so from within the projects module, i can do something like "dbID = data[0]" to get the first item in the array that was passed in from the list's dataprovider? – Brds Jul 28 '11 at 18:49
  • Yes, you should be able to. You may need to / want to cast data as an array first, though. – JeffryHouser Jul 28 '11 at 18:51
  • Flextras, think you could take a look at the edit i made to the original post? – Brds Jul 28 '11 at 19:06
  • doing "[Bindable] public var allData:Array = data as Array;" yields "null" :( – Brds Jul 28 '11 at 19:23
  • 1
    @Brds In your code; The default allData property will be set before the data property is set; giving you the null value. Also as people scroll through the list the allData property will not be updated. Do the conversion inside a dataChange event handler, like this: allData = data as Array(); If that gives you a null value; then do this: allData = Array(data); . In theory both syntaxes should work [in reality sometimes one works better than the other] – JeffryHouser Jul 28 '11 at 20:44
  • Thank you! I had to wrap a "if (allData)" around the value in some functions b/c if i didn't, i was getting some "cannot access an null object" errors. Everything good for now, but i'm sure i'll have some more questions down the road. Thanks again! – Brds Jul 28 '11 at 20:55
  • @www.Flextras.com let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1950/discussion-between-brds-and-www-flextras-com) – Brds Jul 29 '11 at 13:12