Wednesday, November 19, 2008    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jun 2006 >>
S M T W T F S
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

Search

Categories
 • Acrobat (2) [RSS]
 • Adobe (71) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (44) [RSS]
 • AIR (143) [RSS]
 • Appearances (132) [RSS]
 • Books (69) [RSS]
 • CFEclipse (14) [RSS]
 • ColdFusion (1172) [RSS]
 • Data Services (15) [RSS]
 • Fish Tank (2) [RSS]
 • Flash (108) [RSS]
 • Flex (381) [RSS]
 • Home Automation (3) [RSS]
 • Jobs (100) [RSS]
 • JRun (13) [RSS]
 • Labs (29) [RSS]
 • LiveCycle (23) [RSS]
 • MAX (179) [RSS]
 • Regular Expressions (15) [RSS]
 • RIA (12) [RSS]
 • SQL (38) [RSS]
 • Stuff (505) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (140) [RSS]
 • Wireless (100) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Day : June 7, 2006 / Main
June 7, 2006

Using ActionScript To Create Reusable Flex Renderers

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.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="400" width="600">

   <mx:Script>
   <![CDATA[
      [Bindable]
      public var myDP:Array =
      [
         {region:"East", q1:100000, q2:130000},
         {region:"West", q1:175000, q2:210000},
         {region:"Northwest", q1:95000, q2:110000},
         {region:"South", q1:125000, q2:75000},
         {region:"Southwest", q1:145000, q2:160000}
      ]
   ]]>
   </mx:Script>

   <mx:DataGrid id="myDG" dataProvider="{myDP}">
      <mx:columns>
         <mx:DataGridColumn dataField="region" headerText="Region" />
         <mx:DataGridColumn dataField="q1" headerText="Q1" />
         <mx:DataGridColumn dataField="q2" headerText="Q2" />
      </mx:columns>
   </mx:DataGrid>
</mx:Application>

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:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
   <mx:NumberFormatter id="nf" useThousandsSeparator="true" />
   <mx:Label textAlign="right" text="{nf.format(data.q1)}" width="100%" />
</mx:VBox>

To use this renderer you'd just change the DataGridColumn, like this (assuming the renderer was named MyRenderer.mxml):

<mx:DataGridColumn dataField="q1" headerText="Q1" itemRenderer="MyRenderer"/>

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:

package {
   import mx.controls.Label;
   import mx.formatters.NumberFormatter;

   public class MyRenderer extends Label
   {
      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
      {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         setStyle("textAlign", "right");
         var nf:NumberFormatter = new NumberFormatter();
         nf.useThousandsSeparator=true;
         this.text=nf.format(this.text);
      }
   }
}

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):

<mx:DataGridColumn dataField="q1" headerText="Q1" itemRenderer="MyRenderer" />
<mx:DataGridColumn dataField="q2" headerText="Q2" itemRenderer="MyRenderer" />


U.S. Senate SAA Looking For Senior CF Developer/Manager

The U.S. Senate Office of the Sergeant at Arms (in Washington, D.C.) contacted me to share that their ColdFusion/XML/Flex/.NET team has an opening for a very senior principal position. They're looking for someone with extensive experience in ColdFusion, XML, OOP, frameworks and lots of Project Management. Email jason_blum (@) saa.senate.gov for details.

I'll add my own note here to say that I've known these guys for a long time. They are a fun bunch, the apps are cool, and they love the cutting-edge (including Flex).

  © Copyright 1997-2008 Ben Forta, All Rights Reserved