0

在任何人回答问题之前,我已经有了从字节转换为 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

任何帮助完成这项工作将不胜感激。

4

1 回答 1

1

我认为labelFunction会有帮助

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

并且功能是(通过这个

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];
}
于 2012-05-17T06:04:14.083 回答