程序员

flex实现DataGrid高亮显示数据功能的解决方案

作者:admin 2021-04-11 我要评论

flex要高亮数据一般可以使用选中效果或者设置背景,选中效果只能是高亮一条,多条高亮只能设置背景来达到效果。但是原生的DataGrid根本无法达到所要的效果,目前...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
flex要高亮数据一般可以使用选中效果或者设置背景,选中效果只能是高亮一条,多条高亮只能设置背景来达到效果。但是原生的DataGrid根本无法达到所要的效果,目前一般就是来改写原生的DataGrid,只需重新写一个类来重写drawRowBackground方法就可以了,代码如下
复制代码 代码如下:

package org.lxh 
{    
    import flash.display.Sprite; 
    import mx.controls.DataGrid; 
    public class SpecialDataGrid extends DataGrid 
    { 
        private var _rowColorFunction:Function;   //用于在外部能通过指定一个方法 去实现改变列的背景色 
        public function SpecialDataGrid() 
        { 
            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); 
        } 
    } 
}

用的时候先引入名称空间 xmlns:control="org.lxh.*",把原来的DataGrid改成下面这样
复制代码 代码如下:

<control:SpecialDataGrid id="planDataGrid" width="100%"  height="100%"  alternatingItemColors="[0xe3eaf2,0xe8f1f8]" dataProvider="{strArray}" rowColorFunction="colorFunction" doubleClick="planDataGrid_doubleClickHandler(event)" doubleClickEnabled="true"> 
                        <control:columns> 
                            <mx:DataGridColumn dataField="选择" width="50" sortable="false" resizable="false"  showDataTips="true"> 
                                <mx:itemRenderer> 
                                    <fx:Component> 
                                        <mx:CheckBox change="outerDocument.checkChangeHandlerForPlan(event)"/> 
                                    </fx:Component> 
                                </mx:itemRenderer> 
                            </mx:DataGridColumn> 
                        <mx:DataGridColumn dataField="id" headerText="主键" visible="false"/> 

                        </control:columns> 
                    </control:SpecialDataGrid>

rowColorFunction属性用来设置高亮的效果,例如那一列需要高亮,对应的function如下
复制代码 代码如下:

    private function colorFunction(item:Object, color:uint):uint 
    { 
                    var col:uint=0xe3eaf2; 
                    if(commonMsg.length > 0){ 
                        for(var i:int=0;i<commonMsg.length;i++){ 
                            if(commonMsg.getItemAt(i).id==item.id){ 
                                col=0xF10026; 
                            } 
                        } 
                    } 
                    return col; 

    } 

到这里效果就做出来了

本文转载自网络,原文链接:https://m.jb51.net/article/42089.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库

腾讯云代理商
精彩导读
海外云服务器
热门资讯
腾讯云代理商