改变Flex DataGrid 行的背景色

时间:2022-01-11 13:22:54


现在在F4、F3中改变DataGrid行的背景色还是很麻烦,不能像改变列(column)那么直接setBackColor就可以了。不过可以通过复写DataGrid类的drawRowBackground方法来实现。 麻烦是麻烦点 但没办法 谁要俺们要这种功能呢~~~。

首先写自定义类  继承自DataGrid

package BaseUI
{
    import flash.display.Sprite;
    import mx.controls.DataGrid;
    public class CustomerDataGrid extends DataGrid
    {
        private var _rowColorFunction:Function;   //用于在外部能通过指定一个方法 去实现改变列的背景色

        public function CustomerDataGrid()
        {
            super();
        }
        public function set rowColorFunction(f:Function):void
        {
            this._rowColorFunction = f;
        }
        public function get rowColorFunction():Function
        {
            return this._rowColorFunction;
        }

         //复写该方法
        override protected function drawRowBackground(s:Sprite,rowIndex:int,y:Number, height:Number, color:uint, dataIndex:int):void
        {
            if( this.rowColorFunction != null ){
                if( dataIndex < this.dataProvider.length ){
                    var item:Object = this.dataProvider.getItemAt(dataIndex);
                    color = this.rowColorFunction.call(this, item, color);
                }
            }           
            super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
        }
    }
}

 

之后在外面使用该自定义控件,注意要为该控件指定rowColorFunction

<BaseUI:CustomerDataGrid
        id="dgRain"
        width="100%"
        height="100%"
        dataProvider="{ac}"
        borderVisible="false"
        click="cli(event)"
        rowColorFunction="colorFunction"  //指定了改变颜色的方法
        >
        <BaseUI:columns>
            <mx:DataGridColumn dataField="waid"
                               headerText="预警编号" visible="false"/>
            <mx:DataGridColumn dataField="wanm"
                               headerText="预警名称"/>
            <mx:DataGridColumn dataField="stcd"
                               headerText="测站编号" visible="false"/>
            <mx:DataGridColumn dataField="STNM"
                               headerText="测站名称"/>
            <mx:DataGridColumn dataField="stid"
                               headerText="状态" labelFunction="stateFunction"/>
            <mx:DataGridColumn dataField=""
                               headerText="操作" itemRenderer="view.ssjk.ui.SsjkRainItemRender"/>
        </BaseUI:columns>
    </BaseUI:CustomerDataGrid>

 

//改变颜色方法

private function colorFunction(item:Object, color:uint):uint
        {
            switch(item.stid)
            {
                case 0:
                    break;
                case 1:
                    color=0xF10026;
                    break;
                case 2:
                    color=0x26972d;
                    break;
                case 3:
                    color=0xFFDF00;
                    break;
                case 4:
                    color=0xFFDF00;
                    break;
                case 5:
                    color=0xFFDF00;
                    break;
                case 6:
                    color=0xFFDF00;
                    break;
                case 7:
                    color=0xFFDF00;
                    break;
            }
            return color;
        }

下面为效果图~~

改变Flex DataGrid 行的背景色