views:

496

answers:

3

I'm trying to reach the dataField of a DataGridColumn in the itemRenderer. Below is the dataGrid:

<mx:Script>
    <![CDATA[
        [Bindable] public var weeksOfMoth:ArrayCollection = new ArrayCollection([
                {monday:30, tuesday:31, wednesday:1, thursday:2, friday:3, saturday:4, sunday:5},
                {monday:6, tuesday:7, wednesday:8, thursday:9, friday:10, saturday:11, sunday:12},
                {monday:13, tuesday:14, wednesday:15, thursday:16, friday:17, saturday:18, sunday:19}, 
                {monday:20, tuesday:21, wednesday:22, thursday:23, friday:24, saturday:25, sunday:26}, 
                {monday:27, tuesday:28, wednesday:29, thursday:30, friday:1, saturday:2, sunday:3}
            ]);
    ]]>
</mx:Script>
<mx:DataGrid dataProvider="{weeksOfMoth}" >
    <mx:columns>
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="monday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="tuesday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="wednesday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="thursday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="friday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="saturday" />
        <mx:DataGridColumn itemRenderer="view.DateRenderer" dataField="sunday" />
    </mx:columns>
</mx:DataGrid>

And this is my ItemRenderer:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" >
    <mx:Box >

                    <!-- How do I get the dataField here?? -->
        <mx:Label text="{data[dataField]}" /> 
    </mx:Box>
</mx:Canvas>

In the set data function of the itemRenderer, I receive an entire week (which is ok), but the itemRenderer doesn't know which day to use because the dataField is unknown. Does anyone know how to reach this dataField in the itemRenderer?

A: 

Use the DataGridListData class, which is passed into the renderer. It contains a dataField property.

I'm pretty sure this should work:

<mx:Label text="{data[listData.dataField]}" /> 
www.Flextras.com
This doesn't compile: "1120: Access of undefined property listData". It's probably a flex2 solution, but I'm using Flex3
Maurits de Boer
As you discovered, this has nothing to do w/ Flex 2. Many classes already implement the IDropInListItemRenderer interface. Off topic; You should consider re-working your itemRenderer to use less containers. Why not just use a Label? Do you really need the addition 2 containers?
www.Flextras.com
It's actually only a part of the entire renderer, in my actual code it really does need the containers. But you're right, the example used here can be smaller.
Maurits de Boer
I'm glad you got it worked out. if you think my answer helped you solve the issue, feel free to show some love and give my answer a vote. ;)
www.Flextras.com
A: 

The comment by www.Flextras.com helped me to find the solution. I can indeed use listData.dataField, but first the IDropInListItemRenderer class needs to be implemented. The final ItemRenderer:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer" >
    <mx:Script>
        <![CDATA[
            import mx.controls.dataGridClasses.DataGridListData;
            import mx.controls.listClasses.BaseListData;


            // Internal variable for the property value.
            private var _listData:DataGridListData;

            // Make the listData property bindable.
            [Bindable("dataChange")]

            // Define the getter method.
            public function get listData():BaseListData
            {
              return _listData;
            }

            // Define the setter method,
            public function set listData(value:BaseListData):void
            {
              _listData = DataGridListData(value);
            }

        ]]>
    </mx:Script>
    <mx:Box width="80%" height="80%" verticalCenter="0" horizontalCenter="0" backgroundColor="#FFFFFF">
        <mx:Label text="{data[_listData.dataField]}" />
    </mx:Box>
</mx:Canvas>
Maurits de Boer
+1  A: 

You don't have to implement the IDropInListItemRenderer. This is enough:

data[ ( listData as DataGridListData ).dataField ]

The concrete listData is of type DataGridListData, but the property itself is typed to BaseListData, since a renderer can be used in various scenarios. However, in this scenario we know it's DataGridListData, therefore we can simply cast it to access the dataField property.

Creynders