5

我正在尝试访问 itemRenderer 中 DataGridColumn 的数据字段。下面是数据网格:

<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>

这是我的 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>

在itemRenderer的set data函数中,我收到了一整周(可以),但是itemRenderer不知道使用哪一天,因为dataField是未知的。有谁知道如何在 itemRenderer 中访问这个 dataField?

4

6 回答 6

7

www.Flextras.com 的评论帮助我找到了解决方案。我确实可以使用listData.dataField,但首先IDropInListItemRenderer需要实现该类。

决赛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>
于 2010-08-20T12:14:17.260 回答
3

使用传递给渲染器的DataGridListData类。它包含一个 dataField 属性。

我很确定这应该有效:

<mx:Label text="{data[listData.dataField]}" /> 
于 2010-08-20T11:45:21.373 回答
2

您不必实现 IDropInListItemRenderer。这就够了:

data[ ( listData as DataGridListData ).dataField ]

具体的 listData 是 DataGridListData 类型,但属性本身的类型是 BaseListData,因为可以在各种场景中使用渲染器。但是,在这种情况下,我们知道它是 DataGridListData,因此我们可以简单地将其转换为访问 dataField 属性。

于 2010-10-05T09:46:27.733 回答
0

以下适用于 MX AdvancedDataGrid 中基于 Spark 的项目渲染器:

<?xml version="1.0" encoding="utf-8"?>
<s:MXAdvancedDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx"
                                  width="100%"
                                  >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[               
            import mx.controls.dataGridClasses.DataGridListData;

            override public function set data( value:Object):void {             
                var latency:Number = value[ ( this.listData as DataGridListData).dataField] as Number;

                lbl.text = Utils.formatLatency( latency);
                if( latency > 1000000) { // 1s => error
                    lbl.setStyle( "backgroundColor", 0xFF8080);
                } else if( latency > 100000) { // 100ms => warning
                    lbl.setStyle( "backgroundColor", 0xFFFF80);
                } else if( latency > 0){ // low latency
                    lbl.setStyle( "backgroundColor", 0x80FF80);
                }
            }
        ]]>
    </fx:Script>
    <s:Label id="lbl" width="100%" height="100%" verticalAlign="middle" textAlign="right" paddingRight="5"/>
</s:MXAdvancedDataGridItemRenderer>
于 2011-12-29T17:39:24.020 回答
0

更简单:只需使用属性

  • advancedDataGridListData在 MXAdvancedDataGridItemRenderers

  • dataGridListData在 MXDataGridItemRenderer

    访问该信息。

于 2012-02-03T09:36:29.160 回答
0

对于一个普通的 GridItemRenderer 它是一个

column.dataField

因此,在您的情况下,这应该有效:

<s:Label text="{data[column.dataField]}" />
于 2015-04-13T14:23:20.407 回答