Thursday, February 7, 2008

Loading an image using ActionScript with a DataGrid / ItemRenderer

I didn't find any examples of this on Google on how to do this programmatically.
What I wanted to do was display a trash icon / image indicating whether an email message was deleted or not.

For your DataGridColumn:

<mx:DataGridColumn headerText="" dataField="icon" width="11"
itemRenderer="com.sandman.view.IconRenderer" />


Now your renderer:

package com.sandman.view
{
import mx.containers.HBox;
import mx.controls.Image;

public class IconRenderer extends HBox
{
private var imageReference:Image = null;
override public function set data(value:Object):void {

if (value.deleted && imageReference==null) {
var img:Image = new Image();
img.source = 'assets/icons/trash.gif';
img.height = 10;
img.width = 10;
imageReference = img;
addChild(img);
} else if (imageReference is Image) {
imageReference.visible = value.deleted;
}

super.invalidateDisplayList();

}

}
}


The above is a more flexible equivalent to the <mx:Component /> tag.

No comments: