Item renderers are the key to customizing just about any Flex controls. If you want numbers in a data grid column to be green or red (based on whether they are positive or negative), you use a cell renderer. If you want list items to be displayed on multiple lines with data from multiple fields combined in a single display, you use a renderer. If you are using TileList to display data, then again you use a renderer to actually define the display.
Renderers can be written in MXML or ActionScript, and here is one reason why you may want to opt for the latter.
The following is a simple DataGrid example. An array contains five objects, each with three values, and then the results are displayed in a DataGrid.
Now what if you wanted the numbers right aligned and formatted using thousands separators? That’s a job for a custom renderer, and here is an MXML example that would do the trick:
To use this renderer you’d just change the DataGridColumn, like this (assuming the renderer was named MyRenderer.mxml):
But what if you wanted to then reuse the renderer for both of the numeric columns? The renderer receives an object named data that contains the object to be displayed, and has to specify the column to be displayed, hard coded (in this case data.q1). As such, you would need another renderer, just like the first, but with a different column specified.
To get around this problem you could implement IDropInListItemRenderer so as to obtain column info, but there is a cleaner option. Here is the ActionScript renderer:
public class MyRenderer extends Label
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
var nf:NumberFormatter = new NumberFormatter();
This version accomplishes the same result, but is not column specific, and can thus be used in multiple DataGrid columns, as seen here (assuming the file is named MyRenderer.as):
Leave a Reply