Flex在使用Summaries的AdvancedDataGrid列中将字节格式化为MB,GB,TB等

时间:2012-05-16 20:54:05

标签: flex format advanceddatagrid

在有人回答这个问题之前,我已经有了从字节转换为MB,GB,TB等的代码,它位于:

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];
}

我有以下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>

我正在尝试使用convertSize函数格式化要发布的totalunpublished列。现在我有类似这些值的东西:

2302869 24901163 1890295725 1120056357856

但是希望他们看起来像这样: 17.57 MB 189.98 MB 1.76 GB 1.02 TB

任何帮助实现这一目标将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为labelFunction会有所帮助

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

,功能是(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];
}