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

所属分类: 网络编程 / Flex 阅读数: 1626
收藏 0 赞 0 分享
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; 

    } 

到这里效果就做出来了
更多精彩内容其他人还在看

Flex父子窗口相互调用实现思路及源码

这篇文章主要介绍了Flex父子窗口相互调用实现思路及源码,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex中如何判断是否在组件之外单击

这篇文章主要介绍了在Flex中判断是否在组件之外单击的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex中TitleWindow传值思路及实现

这篇文章主要介绍了Flex中TitleWindow传值思路及实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex DataGrid自动编号示例

这篇文章主要介绍了Flex DataGrid如何自动编号,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

Flex DataGrid 伪合并单元格思路及代码

这篇文章主要介绍了Flex DataGrid 伪合并单元格思路及代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex读取txt文件中的内容报错原因分析及解决

这篇文章主要介绍了Flex读取txt文件中的内容报错原因分析及解决,需要的朋友可以参考下
收藏 0 赞 0 分享

FLEX ArrayCollection删除过滤的数据问题解决

ArrayCollection添加过滤器后,调用removeItemAt()是无法删除的,下面有个不错的解决方法,大家可以参考下
收藏 0 赞 0 分享

Flex 错误(mx.messaging.messages::RemotingMessage)分析

有时我们在做项目的时候会遇到Flex 错误提示mx.messaging.messages::RemotingMessage,那么产生这个错误的原因是什么呢,今天我们来分析下
收藏 0 赞 0 分享

手把手教你使用flex eclipse整合spring

Flex 是一个高效、免费的开源框架,可用于构建具有表现力的 Web应用程序,这些应用程序利用Adobe Flash Player和Adobe AIR, 可以实现跨浏览器、桌面和操作系统。我们今天就来好好研究下他
收藏 0 赞 0 分享

flex小技巧之加载GIF图片

这篇文章主要是flex使用GIFPlayer加载GIF图片的代码示例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多