Flex: DataGrid column formatting of numbers

老子叫甜甜 提交于 2019-12-24 05:26:09

问题


I'm trying to format some numbers in a column of a DataGrid. I'm getting an error in my simplified test program below when I run it. All the examples I've seen so far have column data that are strings. Is there a way to do it using numbers? How to modify the code below to format the checking values?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Script>
    <![CDATA[       
        [Bindable]
        public var checking:Array = new Array(1000000.2222, 0, 1000);

        private function myLabelFunction(item:Array, column:DataGridColumn):String {    
                var result:String;
                result = myFormatter.format(item);
                return result;
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <s:NumberFormatter id="myFormatter"
                       fractionalDigits="2" 
                       decimalSeparator="."
                       groupingSeparator=","
                       useGrouping="true"
                       negativeNumberFormat="0"
                       />
</fx:Declarations>

<mx:DataGrid id="dg1" dataProvider="{checking}" >

    <mx:columns>
        <mx:DataGridColumn dataField="checking" headerText="Checking" 
                           labelFunction="myLabelFunction" />
    </mx:columns>

</mx:DataGrid>

</mx:Application>

回答1:


  1. Change filter function signature (item should be Object)

    private function myLabelFunction(item:Object, column:DataGridColumn):String

  2. Remove dataField="checking" from column.



回答2:


While the label function will certainly work -- I usually prefer an ItemRenderer for things like this. You override the render function and then you can display whatever it is in the grid view "box" however you like.

A decent example is here. Scroll down about 1/4 the way down for a DataGrid example.




回答3:


In case of object you must use/No caso de objeto, deve-se usar:

        private function myLabelFunction(item:Object, column:GridColumn):String {    
            var result:String;
            result = myFormatter.format(item[column.dataField]);
            return result;
        }


来源:https://stackoverflow.com/questions/10454932/flex-datagrid-column-formatting-of-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!