flex spaghetti

Just another WordPress.com weblog

Archive for the ‘DataGrid’ Category

One label function serving multiple DataGridColumns

Posted by rvollmar on November 19, 2009

If you want to format several columns of your DataGrid the same way, it may initially look like you need to create several identical label functions, one per column. However, you can get away with defining just one. e.g.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    
    <mx:Script>
        <![CDATA[
            import mx.formatters.CurrencyFormatter;
            
            private function theLabelFunction(item:Object, 
                            column:DataGridColumn):String{
                var val:Number;
                var formatter:CurrencyFormatter = new CurrencyFormatter();
                
                formatter.precision = 0;
                formatter.rounding = "nearest";
                val = item[column.dataField];
                
                return formatter.format(val);
            }
        ]]>
    </mx:Script>
    
    <mx:DataGrid id="dg1" >
        <mx:dataProvider>
            <mx:ArrayCollection>
                <mx:Object checking="100.22" savings="2000" under_pillow="0" />
                <mx:Object checking="0" savings="0" under_pillow="5000" />
                <mx:Object checking="1000" savings="23.88" under_pillow=".5" />
            </mx:ArrayCollection>            
        </mx:dataProvider>
        
        <mx:columns>
            <mx:DataGridColumn dataField="checking" 
                               headerText="Checking" 
                               labelFunction="theLabelFunction" />
            <mx:DataGridColumn dataField="savings" 
                               headerText="Savings" 
                               labelFunction="theLabelFunction" />
            <mx:DataGridColumn dataField="under_pillow" 
                               headerText="Under Pillow" 
                               labelFunction="theLabelFunction" />
        </mx:columns>
    </mx:DataGrid>
    
</mx:Application>

Posted in DataGrid | Leave a Comment »