商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > Flex 自定义DataGrid实现根据条目某一属性值改变背景颜色

Flex 自定义DataGrid实现根据条目某一属性值改变背景颜色

  发布于2026-07-22 阅读(0)

扫一扫,手机访问

package czgh.components {
    import flash.display.Sprite;
    import mx.controls.DataGrid;
    import mx.core.UIComponent;

    public class OptionalDataGrid extends DataGrid {
        private var _rowColorFunction:Function;
        private var _customed:Boolean;
        private var _customerColor:uint=0;

        public function OptionalDataGrid() {
            super();
        }

        override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void {
            color=0XFFFFFF;
            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);
        }

        override protected function drawHeaderBackground(headerBG:UIComponent):void {
            headerBG.setStyle("borderVisible","false");
        }

        public function set rowColorFunction(rowColorFunction:Function):void {
            this._rowColorFunction=rowColorFunction;
        }

        public function get rowColorFunction():Function {
            return this._rowColorFunction;
        }
    }
}
那么,在MXML里用起来也很直观。这里用了一个简单的回调函数,通过比较每条记录里`act`和`stand`字段的值,来决定这一行的背景色。
// 如果act的值小于stand,就把这一行的底色设成浅蓝色,否则就保留默认颜色
private function setCustomColor(item:Object, color:uint):uint {
    if (Number(item["act"])

这样一来,你就有了一个可以根据数据自动变色的DataGrid,非常实用。        
本文转载于:https://www.jb51.net/article/52801.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注