【问题标题】:Flex: DataGrid column formatting of numbersFlex:数字的DataGrid列格式
【发布时间】:2012-05-04 19:23:57
【问题描述】:

我正在尝试格式化 DataGrid 列中的一些数字。我在下面的简化测试程序中运行它时遇到错误。到目前为止,我看到的所有示例都有字符串形式的列数据。有没有办法使用数字来做到这一点?如何修改下面的代码以格式化checking 值?

<?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>

【问题讨论】:

    标签: apache-flex actionscript


    【解决方案1】:
    1. 更改过滤器函数签名(item 应为 Object

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

    2. 从列中删除dataField="checking"

    【讨论】:

      【解决方案2】:

      虽然标签功能肯定会起作用——我通常更喜欢使用 ItemRenderer 来处理这样的事情。您覆盖渲染功能,然后您可以在网格视图“框”中显示您喜欢的任何内容。

      一个不错的例子是here。向下滚动大约 1/4 以查看 DataGrid 示例。

      【讨论】:

      • 很高兴知道,谢谢。如果由于其他原因我已经有一个 ItemRenderer(未显示),我是否可以以某种方式向它添加一个格式化程序,这样我就不需要编写另一个 ItemRenderer?我需要更改/添加什么?
      • 该示例使用内联项渲染,它们直接绑定到它们“内联”的列。您可以使用外部项目渲染器(通常在其自己的脚本文件中)并将其绑定到任意数量的列。您只需编写函数来正确操作数据。不过,您只能通过项目渲染器获取数据对象,因此确切地弄清楚要做什么可能会很棘手。
      • 我想我的问题是,我将在 ItemRenderer 中使用什么控件来进行格式化?例如,要更改文本颜色,我会使用 。我将使用什么控件将 NumberFormatter 放入其中?
      • 没关系,我想我在这里找到了一个例子:stackoverflow.com/questions/4471477/…
      【解决方案3】:

      如果是你必须使用的对象/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;
              }
      

      【讨论】:

        猜你喜欢
        • 2012-11-18
        • 2010-11-20
        • 2013-09-06
        • 1970-01-01
        • 2012-06-17
        • 2015-06-19
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        相关资源
        最近更新 更多