Frage

Before anyone answers the question I already have the code that converts from bytes to MB, GB, TB, etc., it is below:

private var _levels:Array = [ 'bytes','Kb','MB','GB','TB','PB','EB','ZB','YB'];

private function convertSize(bytes:Number):String
{
    var index:uint = Math.floor(Math.log(bytes) / Math.log(1024));
    return (bytes / Math.pow(1024, index)).toFixed(2)+" "+this._levels[index];
}

I have the following AdvancedDataGrid:

<mx:AdvancedDataGrid id="albumTree" width="100%" height="100%" sortExpertMode="true" useHandCursor="true" buttonMode="true">
    <mx:dataProvider>
        <mx:GroupingCollection id="gcQuote" source="{albums}">
            <mx:grouping>
                <mx:Grouping>
                    <mx:GroupingField name="foldername" compareFunction="positionCompareFunction">
                        <mx:summaries>
                            <mx:SummaryRow summaryPlacement="group">
                                <mx:fields>
                                    <mx:SummaryField dataField="totalunpublished" operation="SUM" />
                                </mx:fields>
                            </mx:SummaryRow>
                        </mx:summaries>
                    </mx:GroupingField>
                </mx:Grouping>
            </mx:grouping>
        </mx:GroupingCollection>
    </mx:dataProvider>
    <mx:columns>
        <mx:AdvancedDataGridColumn dataField="name" sortCompareFunction="positionCompareFunction" headerText="Folder/Album" />
        <mx:AdvancedDataGridColumn width="80" dataField="totalunpublished" headerText="Photo Usage" />
    </mx:columns>
</mx:AdvancedDataGrid>

I am trying to format the totalunpublished column to be formatted using the convertSize function. Right now I have something similar to these values:

2302869 24901163 1890295725 1120056357856

But would like them to look something like this: 17.57 MB 189.98 MB 1.76 GB 1.02 TB

Any help in getting this accomplished would be greatly appreciated.

War es hilfreich?

Lösung

I think labelFunction will be helpfull

<mx:AdvancedDataGridColumn width="80" dataField="totalunpublished" headerText="Photo Usage" labelFunction="convertSize"/>

and the function is(By this)

private var _levels:Array = [ 'bytes','Kb','MB','GB','TB','PB','EB','ZB','YB'];
private function convertSize(data:Object, column:AdvancedDataGridColumn):String 
{
     var bytes:Number=data[column.dataField];
    var index:uint = Math.floor(Math.log(bytes) / Math.log(1024));
    return (bytes / Math.pow(1024, index)).toFixed(2)+" "+this._levels[index];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top