改变Flex DataGrid 行的背景色



现在在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;
        }

下面为效果图~~

image



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值