In Part 1 you saw there was a complex itemRenderer used for a DataGrid:
<mx:DataGridColumn headerText="Title" dataField="title"> <mx:itemRenderer> <mx:Component> <mx:HBox paddingLeft="2"> <mx:Script> <![CDATA[ override public function set data( value:Object ) : void { super.data = value; var today:Number = (new Date()).time; var pubDate:Number = Date.parse(data.date); if( pubDate > today ) setStyle("backgroundColor",0xff99ff); else setStyle("backgroundColor",0xffffff); } ]]> </mx:Script> <mx:Image source="{data.image}" width="50" height="50" scaleContent="true" /> <mx:Text width="100%" text="{data.title}" /> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn>
The itemRenderer
is based on an HBox, contains an Image and a Text, and the background color is
set according to the pubDate field of the item record. You can write
this same itemRenderer as an external file using these steps:
HBox.
Don't worry about the size. HBox. With the file open, copy
everything between <mx:HBox> and </mx:HBox>,
but do not copy those tags, since they are already in the file. The result
should look something like this:
<?xml version="1.0" encoding="utf-8"?> <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"> <mx:Script> <![CDATA[ override public function set data( value:Object ) : void { super.data = value; var today:Number = (new Date()).time; var pubDate:Number = Date.parse(data.date); if( pubDate > today ) setStyle("backgroundColor",0xff99ff); else setStyle("backgroundColor",0xffffff); } ]]> </mx:Script> <mx:Image source="{data.image}" width="50" height="50" scaleContent="true" /> <mx:Text width="100%" text="{data.title}" /> </mx:HBox>
Now modify the DataGridColumn definition by removing the inline itemRenderer and replacing it with this:
<mx:DataGridColumn headerText="Title" dataField="title" itemRenderer="GridColumnSimpleRenderer">
Now run the
application. You'll get a surprise. The surprise is how tall the rows are. That's
because of the presence of height="300" on the itemRenderer.
The List
control always sets the itemRenderer's width. In this example, the explicit width="400" is ignored. You should write your itemRenderer to assume the width will change
as the user changes the column or list's width.
The height is a
different matter. If the list has an explicit rowHeight set, it will
impose that height on each row, ignoring any height you've set on the
itemRenderer. However, if you set the list's variableRowHeight property to true, then the list will seriously consider the
itemRenderer's height. In this example, the height is explicitly set to 300, so
each row is 300 pixels high.
To fix this, remove the explict height from the itemRenderer file and the application will work correctly.
In this example
the set
data() function has been overridden to examine the data and set
the itemRenderer's backgroundColor. This is very common.
Overriding set
data() enables you to intercept the time when the data is being
changed for a new row and you can you make style changes.
Common mistakes are:
super.data = value;. This is VITAL—failure
to do this will really mess up your itemRenderer.pubDate is in the
future, but you have to remember that itemRenderers are recycled, and so the else statement is very necessary.