DataGrid的行渲染

1 配置渲染器的方式

   <mx:DataGridColumn headerStyleName="DISPVALUE" dataField="DISPVALUE" editable="false" fontSize="12" textAlign="left" paddingLeft="5">
   <mx:itemRenderer>
    <fx:Component>
     <local:ItemStyle>
     </local:ItemStyle>
    </fx:Component>
   </mx:itemRenderer>
  </mx:DataGridColumn>



ItemStyle
public class ItemStyle extends Label
 {
  /**
   * 构造函数
   */
  public function ItemStyle() {
   super();
  }
 
  override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
   super.updateDisplayList(unscaledWidth, unscaledHeight);
   if(data["POINT_X"]==0||data["POINT_X"]==null){
    this.setStyle('color', 0xDD0000);
   }else
    this.setStyle("color",0x000000);
  }
 
 }// end of class
}



2 编码渲染器的方式添加

 //必须要实现IFactory接口
 public class CellColorIFactory implements IFactory
 {
  //回调方法
  private var _cellColorFunction:Function ;
 
  public function CellColorIFactory(f:Function){
   super();
   this._cellColorFunction = f;
  }
 
  public function newInstance():*{
   //实例化渲染器,实现具体功能
   return new CellColorItemRenderer(_cellColorFunction);
  }
 }




public class CellColorItemRenderer extends DataGridItemRenderer
 {
  //传递回调函数
  private var _cellColorFunction:Function;
 
  private var _value:Object;
  public function CellColorItemRenderer(f:Function)
  {
   super();
   this._cellColorFunction=f;
  }
  override public function set data(value:Object):void
  {
   if (value != null)
   {
    var data:XML=XML(value);
    _value=value;
    //调用父类的public function get listData():BaseListData
    var dataField:String=DataGridListData(listData).dataField;
    while (dataField.indexOf("@") != -1)
     dataField=dataField.replace("@", "");
    //改变颜色;两个参数,dataField表示数据列(名称),data表示一行数据
    setStyle("color", this._cellColorFunction.call(this, dataField, value));
   }
  }
  override public function get data():Object
  {
   return _value;
  }
 }




var itemRender:IFactory = new CellColorIFactory(cellColor);
dataColumn.itemRenderer=itemRender;

private function cellColor(dataField:String,data:Object):uint{
    //设置前两页显示颜色
    //设置默认显示颜色
    if(!isNaN(data.x) && !isNaN(data.y)){
     return 0x000000;
    }else{
     return 0xDD0000;
    }
 
   } 

你可能感兴趣的:(Flex)