根据Flex中的多个条件更改数据网格单元格的背景颜色

时间:2013-02-20 16:56:17

标签: actionscript-3 flex flex4 flex4.5

您好我是Adobe Flex的新手,如果我的问题听起来很愚蠢,请道歉。无论如何这里是。 我正在尝试简单的数据网格,它基本上检查了两个条件 1)如果艺术家为01且专辑为“专辑01”,则在“年份”列中将相应单元格设置为背景。

使用我的下面的代码'将Style'设置为Background Color,因为属性不起作用但改变字体颜色正在工作,其次我不知道如何编写代码以使上述嵌套条件有效?如果有人能在这方面帮助我,我将非常感激。 谢谢!提前。

以下是代码: Newdatagrid.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 

           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"          minHeight="600" >
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        [Bindable]
        public static var initDG:ArrayCollection = new ArrayCollection([
            {Artist:'01', Album:'Album 01', Year:'2008'},
            {Artist:'01', Album:'Album 02', Year:'2009'},
            {Artist:'03', Album:'Album 03', Year:'2007'},
            {Artist:'03', Album:'Album 04', Year:'2003'},

        ]); 

    ]]>

</fx:Script>


<s:VGroup>
    <s:DataGrid id="myGrid" width="360" dataProvider="{initDG}">   
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="Artist"    headerText="Artist" itemRenderer="CellRenderer"/>
                <s:GridColumn dataField="Album" headerText="Album" itemRenderer="CellRenderer"/>
                <s:GridColumn dataField="Year" headerText="Year" itemRenderer="CellRenderer"/>
            </s:ArrayList>
        </s:columns>       
    </s:DataGrid> 
</s:VGroup>
</s:Application>

渲染器:

<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                    implements="mx.controls.listClasses.IDropInListItemRenderer">
<fx:Script>
    <![CDATA[
        import mx.controls.dataGridClasses.DataGridListData;
        import mx.controls.listClasses.BaseListData;
        import mx.controls.Alert;

        private var _listData:BaseListData;

        [Bindable]private var isSelected:Boolean = false;

        override public function set data( value:Object ) : void 
        {
            super.data = value;
            lblData.text = data[column.dataField];

            if (data[column.dataField].valueOf() >= 2008){
                //styleName="myStyles.BgColor";
                setStyle('backgroundColor',0xFFFF00);
            }else{
                setStyle('backgroundColor',0x32CD32);
            }

        }

        [Bindable]public function get listData() : BaseListData
        {
            return _listData;
        }
        public function set listData( value:BaseListData ) : void
        {
            _listData = value;
        }

    ]]>
</fx:Script>
<s:Label id="lblData" top="9" left="7" width="100%" height="100%"    textAlign="center"/>

</s:GridItemRenderer>

我想要的输出:条件:如果Artist = 01且Year&gt; = 2008,那么单元格背景将变为红色

2 个答案:

答案 0 :(得分:7)

GridItemRenderer类没有这样的样式backgroundColor。 所以设置它没有任何效果。

您可以做的是将一个Rect添加到ItemRenderer并根据您的条件设置其颜色属性。

一个例子就是:

<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    xmlns:s="library://ns.adobe.com/flex/spark" 
                    xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">

    <fx:Script>
        <![CDATA[
            override public function prepare(hasBeenRecycled:Boolean):void {


                if(this.data) {

                    if(this.data.Year >= 2008 && this.data.Artist == '01' && column.dataField == 'Year') 
                        bgColor.color = 0xFF0000;
                    else
                        bgColor.color = 0xFFFFFF;
                }
            }
        ]]>
    </fx:Script>

    <s:Rect top="0" bottom="0" left="0" right="0">
        <s:fill>
            <s:SolidColor id="bgColor" color="0xFFFFFF"/>
        </s:fill>
    </s:Rect>
    <s:Label id="labelDisplay" top="9" left="7"/>

</s:GridItemRenderer>

答案 1 :(得分:1)

    <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 

               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"          minHeight="600" >
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            [Bindable]
            public static var initDG:ArrayCollection = new ArrayCollection([
                {Artist:'01', Album:'Album 01', Year:'2008'},
                {Artist:'01', Album:'Album 02', Year:'2009'},
                {Artist:'03', Album:'Album 03', Year:'2007'},
                {Artist:'03', Album:'Album 04', Year:'2003'},

            ]); 

        ]]>

    </fx:Script>


    <s:VGroup>
        <s:DataGrid id="myGrid" width="360" dataProvider="{initDG}">   
            <s:columns>
                <s:ArrayList>
                    <s:GridColumn dataField="Artist"    headerText="Artist"/>
                    <s:GridColumn dataField="Album" headerText="Album"/>
                    <s:GridColumn dataField="Year" headerText="Year" itemRenderer="CellRenderer"/>
                </s:ArrayList>
            </s:columns>       
        </s:DataGrid> 
    </s:VGroup>
</s:WindowedApplication>

----------------------------CellRenderer.mxml------------------------

    <?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    xmlns:s="library://ns.adobe.com/flex/spark" 
                    xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">

    <fx:Script>
        <![CDATA[
            override public function prepare(hasBeenRecycled:Boolean):void {
                lblData.text = data[column.dataField]
                if(this.data) {
                    lblData.text = data[column.dataField];

                    if(this.data.Year >= 2008&&this.data.Artist==01)
                        bgColor.color = 0xFF0000;
                    else
                        bgColor.color = 0xFFFFFF;
                }
            }
        ]]>
    </fx:Script>

    <s:Rect top="0" bottom="0" left="0" right="0">
        <s:fill>
            <s:SolidColor id="bgColor" color="0xFFFFFF"/>
        </s:fill>
    </s:Rect>
    <s:Label id="lblData" top="9" left="7" width="100%" height="100%" textAlign="center"/>

</s:GridItemRenderer>