|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS Book Excerpt Adobe Flex 2: Advanced DataGrid
Part 1: Destination-awareness formatters & renderers
Dec. 18, 2006 04:00 PM
More on Customizing the DataGridColumn As mentioned above, the "dirty" job of locating and assigning the proper label function has been delegated to the helper class FormattingManager. This class, presented in Listing 4, should be put in our theriabook project. Tada! And the winner is...the developer. Once we add theriabook.swc (with DataGrid, DataGridColumn, and FormattingManager) to the library path, the application code gets reduced to Listing 5.
Improving FormattingManager While SwitchSymbolFormatter derives from Object, all the rest of the formatters descend from Formatter. By encapsulating this specific of SwitchSymbolFormatter in the custom class MaskFormatter we'll help ourselves in basing the next version of FormattingManager entirely on Formatters as in Listing 6. Look how this MaskFormatter simplifies our FormattingManager: we can replace all the private methods with an anonymous function, as shown in Listing 7. Please note that the reference to the appropriate formatter is preserved with the closure. The testing application FormatStringDemo is in Listing 8. If you run it, the DataGrid dg will be formatted as shown in Figure 4: Let's focus on the hard-coding that we allowed in case of the money value:
case "money": This hard-coding reflects, perhaps, the most "popular" case. But what if we want to have the full advantage of the properties of the corresponding formatter, such as precision, in case of CurrencyFormatter? To address these cases we're going to introduce one more fx:DataGridColumn property - formatData. Here's how it will be used in the application MXML:
<fx:DataGridColumn dataField="SALARY" > The elegance of MXML lets us implement this extension with just a few lines of extra code in com.theriabook.controls.dataGridClasses.DataGridColumn:
public function set formatData(fd :Object) : void{ Then, to accommodate the change on the FormattingManager side, we'll iterate through all the properties of the formatData object and attempt to assign them to the appropriate properties of the formatter with an emphasis on the word appropriate. The MXML compiler isn't going to help us check the properties of the unsealed <mx:Object> against the properties of the formatter. Accordingly, to protect ourselves from the no-such-property-exceptions, we surround property assignments with try/catch:
public static function setFormat( The complete listing of renewed FormattingManager is in Listing 9. While maintaining your own framework, you would transform this class to accommodate your requirements Finally, Listing 10 has the sample application to test our changes, FormatDataDemo. When you run the application it will produce the DataGrid shown in Figure 5. We'll continue beefing up our custom DataGridColumn after a short detour into CheckBox and RadioButton controls.
CheckBox as a Drop-In Renderer The state of a CheckBox control is managed by the Boolean property selected. At the same time, many business systems use either Y/N or, sometimes, 0/1 flags. As a result, translating business-specific values into selected and vice versa burdens the application code. Listing 11 presents the custom CheckBox, which supports application-specific on and off values along with the current value. So, using this CheckBox, we could have written: <fx:CheckBox value="Y" onValue="Y" offValue="N" /> to have selected CheckBox, or <fx:CheckBox value="N" onValue="Y" offValue="N" /> to set selected to false.
DataGridColumn as ItemRenderer's Knowledge Base
<fx:DataGridColumn dataField="BENE_DAY_CARE" Obviously, we need to modify the CheckBox more to take care of the value within the data setter:
override public function set data(item:Object):void But how will we communicate the offValue and onValue properties to our CheckBox-turned-itemRenderer? Ideally, we would need something like:
<fx:DataGridColumn dataField="BENE_DAY_CARE" CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||