Flex DatagridColumn LabelFunction 附加参数

发布于 2024-10-20 21:12:02 字数 911 浏览 2 评论 0原文

我有一个 datagridcolumn,其中定义了 labelFunction:

private function myLabelFunction(item:Object, column:DataGridColumn):String 
{
  var returnVal:String;
  var nm:NumericFormatter;
  nm.decimalSeparatorTo = ".";
  nm.precision = additionalParameter;

  returnVal = nmTwoDecimals.format(item[column.dataField]);

  if (returnVal == '0.00') 
  {
    returnVal = '';
  }

  return returnVal;
}

是否可以添加一个附加参数,以便我可以传递我打算使用的格式化程序的属性值?

例如:

private function myLabelFunction(item:Object, column:DataGridColumn, precisionParam:int):String 
    {
      var returnVal:String;
      var nm:NumericFormatter;
      nm.decimalSeparatorTo = ".";
      nm.precision = precisionParam;

      returnVal = nmTwoDecimals.format(item[column.dataField]);

      if (returnVal == '0.00') 
      {
        returnVal = '';
      }

      return returnVal;
    }

谢谢。

I have a datagridcolumn where a labelFunction is defined:

private function myLabelFunction(item:Object, column:DataGridColumn):String 
{
  var returnVal:String;
  var nm:NumericFormatter;
  nm.decimalSeparatorTo = ".";
  nm.precision = additionalParameter;

  returnVal = nmTwoDecimals.format(item[column.dataField]);

  if (returnVal == '0.00') 
  {
    returnVal = '';
  }

  return returnVal;
}

Would it be possible to add an additional parameter so that I could pass the property values for the formatter which I intend to use?

Like for example:

private function myLabelFunction(item:Object, column:DataGridColumn, precisionParam:int):String 
    {
      var returnVal:String;
      var nm:NumericFormatter;
      nm.decimalSeparatorTo = ".";
      nm.precision = precisionParam;

      returnVal = nmTwoDecimals.format(item[column.dataField]);

      if (returnVal == '0.00') 
      {
        returnVal = '';
      }

      return returnVal;
    }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-10-27 21:12:02

您必须扩展 DataGridColumn 类。创建新类后,只需覆盖现有的 itemToLabel 函数:

public function itemToLabel(data:Object):String
{
        if (!data)
            return " ";

        if (labelFunction != null)
            return labelFunction(data, this);

        if (owner.labelFunction != null)
            return owner.labelFunction(data, this);

        if (typeof(data) == "object" || typeof(data) == "xml")
        {
            try
            {
                if ( !hasComplexFieldName ) 
                data = data[dataField];
                else 
                    data = deriveComplexColumnData( data );
            }
            catch(e:Error)
            {
                data = null;
            }
        }

        if (data is String)
            return String(data);

        try
        {
            return data.toString();
        }
        catch(e:Error)
        {
        }

        return " ";
    }

“return labelFunction(data, this);”行是调用 labelFunction 的内容(还将检查所有者数据网格中的 labelfunction)。 “itemToLabel”中的“data”是您的对象。您可以在对象中包含所需的精度值,也可以在扩展类中对其进行硬编码(或注入,或单例,类变量,无论您喜欢什么)。

此时,您可以继续将其作为第三个参数传递给新的 labelFunction。

You would have to extend the DataGridColumn class. After creating your new class simply override the existing itemToLabel function:

public function itemToLabel(data:Object):String
{
        if (!data)
            return " ";

        if (labelFunction != null)
            return labelFunction(data, this);

        if (owner.labelFunction != null)
            return owner.labelFunction(data, this);

        if (typeof(data) == "object" || typeof(data) == "xml")
        {
            try
            {
                if ( !hasComplexFieldName ) 
                data = data[dataField];
                else 
                    data = deriveComplexColumnData( data );
            }
            catch(e:Error)
            {
                data = null;
            }
        }

        if (data is String)
            return String(data);

        try
        {
            return data.toString();
        }
        catch(e:Error)
        {
        }

        return " ";
    }

The line 'return labelFunction(data, this);' is what calls the labelFunction (will also check the owner datagrid for a labelfunction). 'data' in 'itemToLabel' is your object. You could either include the precision value you want in the object or hard code it in the extended class (or inject, or singleton, class var, whatever you like).

At this point you can go ahead and pass it as a third parameter to your new labelFunction.

酷炫老祖宗 2024-10-27 21:12:02

在 datagrid 列的标签函数中,您可以使用 dataField 属性访问分配的数据字段,请参阅下面的语法:

“假设您的标签函数称为 formatNumbers_LabelFunction”

private function formatNumbers_LabelFunction(item:Object, column:DataGridColumn):String{

    //Write any code logic you want to apply on your data field ;)
    //In this example, I'm using a number formatter to edit numbers

    return myCustomNumberFormatter.format(item[column.dataField]);
}

这样,您可以使用通用标签函数来处理一些对显示数据进行统一操作

除此之外,您还可以通过调用数据提供程序中的任何数据字段,如下所示:

item.YourFieldName

其中 item 是标签函数方法中的第一个参数 [of type Object]

In your label function for the datagrid column, you can access the assigned data field by using the dataField property, see the following syntax below:

"supposing your label function is called formatNumbers_LabelFunction"

private function formatNumbers_LabelFunction(item:Object, column:DataGridColumn):String{

    //Write any code logic you want to apply on your data field ;)
    //In this example, I'm using a number formatter to edit numbers

    return myCustomNumberFormatter.format(item[column.dataField]);
}

This way, you can use a generic label function to handle some unified operations on your displayed data

And besides that, you can also access to any data field that is in the data provider by just calling its name like this:

item.YourFieldName

where item is the firs parameter [of type Object] in your label function method

羞稚 2024-10-27 21:12:02

这会起作用:

<DataGridColumn labelFunction="{function(item:Object, column:DataGridColumn):String { return anotherLabelFunction(item,column,2) }}" />

// Elsewhere ... 
function anotherLabelFunction(item:Object,column:DataGridColumn,precision:int):String
{
    // Do your business
}

This would work:

<DataGridColumn labelFunction="{function(item:Object, column:DataGridColumn):String { return anotherLabelFunction(item,column,2) }}" />

// Elsewhere ... 
function anotherLabelFunction(item:Object,column:DataGridColumn,precision:int):String
{
    // Do your business
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文