user320691
user320691

Reputation: 185

Accessing DataGridColumn item renderer variable

Within a DataGrid, I have a DataGridColumn that uses a custom component as the item renderer. Within the component, I have an ArrayCollection that stores a set of value objects. My problem is that I cannot access the ArrayCollection values from outside of the item renderer component. Does anyone know how it would be possible to do this? I have posted a code snippet below.

<mx:Script>
    <![CDATA[
        // Cannot access arrFiles from here.
    ]]>
</mx:Script>
<mx:DataGrid editable="true">
    <mx:columns>
        <mx:DataGridColumn id="dgcUpload" width="130" headerText="Uploaded Files"
        editable="false">
        <mx:itemRenderer>
        <mx:Component>
                    <mx:VBox>
                        <mx:Script>
                            <![CDATA[
                                [Bindable]public var arrFiles:ArrayCollection = new ArrayCollection();
                            ]]>
                        </mx:Script>
                    </mx:VBox>
        </mx:Component>
        </mx:itemRenderer>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>

Is this possible?

Thank you in advance for any assistance,

Orville

Upvotes: 2

Views: 4133

Answers (2)

Jeff Pinkston
Jeff Pinkston

Reputation: 176

I would create a custom MXML Box component as the rendered with a label (myLabel) as a child. Set the data provider for the DataGrid to the array. In the custom MXML component override the set data method which is called each time the data is rendered for each row and set the label to the current value passed in:

override public function set data(value:Object):void{
         myLabel.text = value.myTextForLabel;
}

If the field in the ArrayCollection (myArrayCollection) is always the same for the label, then just set the DataGrid data provider to the ArrayCollection and the dataField property of the column to the appropriate value (myText):

<mx:DataGrid editable="true" dataProvider="myArrayCollection">
   <mx:columns>
    <mx:DataGridColumn id="dgcUpload" width="130" dataField="myText" headerText="Uploaded Files"
    editable="false">
   </mx:columns>
</mx:DataGrid>

Upvotes: 1

ryanstewart
ryanstewart

Reputation: 1014

It is possible depending on how you want to access it. You can access the property of a specific item being rendered by the itemRenderer by calling the itemToItemRenderer function on the datagrid. That gives you an instance of that specific itemRenderer and you can call the arrFiles variable for that item.

Here's an example

        protected function datagrid1_clickHandler(event:MouseEvent):void
        {
            var obj:Object = dgcUpload.itemToItemRenderer(dgcUpload.selectedItem);
            var newArray:ArrayCollection = obj.arrFiles;
        }

I call that when something is clicked on the DataGrid and I want to access the arrFiles variable for the selected item.

Is that what you're looking for?

=Ryan

Upvotes: 1

Related Questions