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">
            import mx.formatters.CurrencyFormatter;
            private function theLabelFunction(item:Object, 
                var val:Number;
                var formatter:CurrencyFormatter = new CurrencyFormatter();
                formatter.precision = 0;
                formatter.rounding = "nearest";
                val = item[column.dataField];
                return formatter.format(val);
    <mx:DataGrid id="dg1" >
                <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:DataGridColumn dataField="checking" 
                               labelFunction="theLabelFunction" />
            <mx:DataGridColumn dataField="savings" 
                               labelFunction="theLabelFunction" />
            <mx:DataGridColumn dataField="under_pillow" 
                               headerText="Under Pillow" 
                               labelFunction="theLabelFunction" />


Posted in DataGrid | Leave a Comment »