The following example shows how you can format the size column to display bytes or kilobytes in an Adobe AIR FileSystemDataGrid control by setting the sizeDisplayMode property to one of the static constants in the FileSystemSizeDisplayMode class.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://airexamples.com/2008/12/26/formatting-the-size-column-on-the-filesystemdatagrid-control-in-adobe-air/ -->
<mx:WindowedApplication name="FileSystemDataGrid_sizeDisplayMode_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        showStatusBar="false">
 
    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="sizeDisplayMode:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[bytes,kilobytes]"
                        selectedIndex="1" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:FileSystemDataGrid id="dataGrid"
            directory="{File.documentsDirectory}"
            sizeDisplayMode="{comboBox.selectedItem}"
            horizontalScrollPolicy="off"
            verticalScrollPolicy="on"
            width="100%"
            rowCount="12" />
 
</mx:WindowedApplication>

You can also set the sizeDisplayMode property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://airexamples.com/2008/12/26/formatting-the-size-column-on-the-filesystemdatagrid-control-in-adobe-air/ -->
<mx:WindowedApplication name="FileSystemDataGrid_sizeDisplayMode_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        showStatusBar="false">
 
    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
 
            private function comboBox_change(evt:ListEvent):void {
                dataGrid.sizeDisplayMode = comboBox.selectedItem.toString();
            }
        ]]>
    </mx:Script>
 
    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="sizeDisplayMode:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[bytes,kilobytes]"
                        selectedIndex="1"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:FileSystemDataGrid id="dataGrid"
            directory="{File.documentsDirectory}"
            sizeDisplayMode="{comboBox.selectedItem}"
            horizontalScrollPolicy="off"
            verticalScrollPolicy="on"
            width="100%"
            rowCount="12" />
 
</mx:WindowedApplication>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://airexamples.com/2008/12/26/formatting-the-size-column-on-the-filesystemdatagrid-control-in-adobe-air/ -->
<mx:WindowedApplication name="FileSystemDataGrid_sizeDisplayMode_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        showStatusBar="false"
        initialize="init();">
 
    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.FileSystemSizeDisplayMode;
            import mx.controls.ComboBox;
            import mx.controls.FileSystemDataGrid;
            import mx.core.ScrollPolicy;
            import mx.events.ListEvent;
 
            private var comboBox:ComboBox;
            private var dataGrid:FileSystemDataGrid;
 
            private function init():void {
                var arr:Array = [];
                arr.push(FileSystemSizeDisplayMode.BYTES);
                arr.push(FileSystemSizeDisplayMode.KILOBYTES);
 
                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.selectedIndex = 1;
                comboBox.addEventListener(ListEvent.CHANGE, comboBox_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "sizeDisplayMode:";
                formItem.addChild(comboBox);
 
                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);
 
                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);
 
                dataGrid = new FileSystemDataGrid();
                dataGrid.directory = File.documentsDirectory;
                dataGrid.horizontalScrollPolicy = ScrollPolicy.OFF;
                dataGrid.verticalScrollPolicy = ScrollPolicy.ON;
                dataGrid.percentWidth = 100;
                dataGrid.rowCount = 12;
                addChild(dataGrid);
            }
 
            private function comboBox_change(evt:ListEvent):void {
                dataGrid.sizeDisplayMode = comboBox.selectedItem.toString();
            }
        ]]>
    </mx:Script>
 
</mx:WindowedApplication>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam Protection by WP-SpamFree