如何为 Flex Datagrid 制作可重复使用的 labelFunction?

发布于 2024-07-08 18:43:18 字数 488 浏览 9 评论 0原文

我有一个标签函数,例如:

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    if (column.dataField == "startDate") {
        return df.format(item.startDate);
    }

    return "ERR";
}

我通过使用 labelFunction 在数据列中使用它。

如果我的数据字段名为“startDate”,则效果很好。 我想让这个函数变得通用,这样我就可以在任何地方使用它。

我怎样才能做到这一点。 我想我需要使用某种“反思” - 或者也许完全是另一种方法?

I have a label function like :

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    if (column.dataField == "startDate") {
        return df.format(item.startDate);
    }

    return "ERR";
}

Which I use in a datacolumn by using labelFunction.

This works just fine if my data field is called 'startDate'. I want to make this function generic so I can use it everywhere.

How can I do this. i think i need to use some kind of 'reflection' - or perhaps another approach altogether?

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

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

发布评论

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

评论(3

平定天下 2024-07-15 18:43:18

您可以使用列的 dataField 属性作为项目的键来使该函数通用。

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    var value:object = item[column.dataField];

    return df.format(value);
}

-本

You can make the function generic using the dataField attribute of the column as the key into your item.

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    var value:object = item[column.dataField];

    return df.format(value);
}

-Ben

夏末的微笑 2024-07-15 18:43:18

您可以定义另一个函数,我们称之为 partial ,它将一些额外的参数绑定到您的函数:

function partial( func : Function, ...boundArgs ) : Function {
  return function( ...dynamicArgs ) : * {
    return func.apply(null, boundArgs.concat(dynamicArgs))
  }
}

然后您可以像这样更改您的函数:

private function formatDate( dataField : String, item : Object, column : DataGridColumn ) : String {
  var df : DateFormatter = new DateFormatter();

  df.formatString = "MM/DD/YY";

  if ( column.dataField == dataField ) {
    return df.format(item[dataField]);
  }

  return "ERR";
}

请注意,我添加了一个名为 dataField 的新参数> 在参数列表中第一个,并将所有对“startDate”的引用替换为该参数。

并像这样使用它:

var startDateLabelFunction : Function = partial(formatDate, "startDate");
var endDateLabelFunction   : Function = partial(formatDate, "endDate");

partial 函数返回一个新函数,该函数使用调用partial的参数调用原始函数,并将参数与新函数的参数连接起来......你和我一样吗? 另一种说法是它可以返回一个新函数,其中 N 个参数预先绑定到特定值。

让我们一步一步地看一下:

partial(formatDate, "startDate") 返回一个如下所示的函数:

function( ...dynamicArgs ) : * {
  return func.apply(null, boundArgs.concat(dynamicArgs));
}

但是 funcboundArgs是你作为参数传递给 partial 的内容,所以你可以说它看起来像这样:

function( ...dynamicArgs ) : * {
  return formatDate.apply(null, ["startDate"].concat(dynamicArgs));
}

当它被调用时,它或多或少与这个

function( item : Object, column : DataGridColumn ) : * {
  return formatDate("startDate", item, column);
}

Tada 相同!

You can define another function, let's call it partial that binds some extra arguments to your function:

function partial( func : Function, ...boundArgs ) : Function {
  return function( ...dynamicArgs ) : * {
    return func.apply(null, boundArgs.concat(dynamicArgs))
  }
}

Then you change your function like this:

private function formatDate( dataField : String, item : Object, column : DataGridColumn ) : String {
  var df : DateFormatter = new DateFormatter();

  df.formatString = "MM/DD/YY";

  if ( column.dataField == dataField ) {
    return df.format(item[dataField]);
  }

  return "ERR";
}

Notice that I have added a new argument called dataField first in the argument list, and replaced all references to "startDate" with that argument.

And use it like this:

var startDateLabelFunction : Function = partial(formatDate, "startDate");
var endDateLabelFunction   : Function = partial(formatDate, "endDate");

The partial function returns a new function that calls the original function with the parameters from the call to partial concatenated with the parameters to the new function... you with me? Another way of putting it is that it can return a new function where N of the arguments are pre-bound to specific values.

Let's go through it step by step:

partial(formatDate, "startDate") returns a function that looks like this:

function( ...dynamicArgs ) : * {
  return func.apply(null, boundArgs.concat(dynamicArgs));
}

but the func and boundArgs are what you passed as arguments to partial, so you could say that it looks like this:

function( ...dynamicArgs ) : * {
  return formatDate.apply(null, ["startDate"].concat(dynamicArgs));
}

which, when it is called, will be more or less the same as this

function( item : Object, column : DataGridColumn ) : * {
  return formatDate("startDate", item, column);
}

Tada!

ι不睡觉的鱼゛ 2024-07-15 18:43:18

这是更通用的方法:

public static function getDateLabelFunction(dateFormatString:String=null, mxFunction:Boolean = false) : Function {
        var retf:Function;

        // defaults
        if(dateFormatString == null) dateFormatString = "MM/DD/YY";
        if(mxFunction) {
            retf = function  (item:Object, column:DataGridColumn):String
            {
                var df:DateFormatter = new DateFormatter();
                df.formatString = dateFormatString;

                var value:Object = item[column.dataField];

                return df.format(value);
            }
        }else {
            retf = function  (item:Object, column:GridColumn):String
            {
                var df:DateFormatter = new DateFormatter();
                df.formatString = dateFormatString;

                var value:Object = item[column.dataField];

                return df.format(new Date(value));
            }
        }

        return retf;

    }

用法(Spark DataGrid)

var labelFunction = getDateLabelFunction();

或 MX Datagrid

var labelFunction = getDateLabelFunction(null,true);

传递自定义日期格式字符串:

var labelFunction = getDateLabelFunction("DD/MM/YYYY",true);

默认为“MM/DD/YYYY”;

here is more generic way:

public static function getDateLabelFunction(dateFormatString:String=null, mxFunction:Boolean = false) : Function {
        var retf:Function;

        // defaults
        if(dateFormatString == null) dateFormatString = "MM/DD/YY";
        if(mxFunction) {
            retf = function  (item:Object, column:DataGridColumn):String
            {
                var df:DateFormatter = new DateFormatter();
                df.formatString = dateFormatString;

                var value:Object = item[column.dataField];

                return df.format(value);
            }
        }else {
            retf = function  (item:Object, column:GridColumn):String
            {
                var df:DateFormatter = new DateFormatter();
                df.formatString = dateFormatString;

                var value:Object = item[column.dataField];

                return df.format(new Date(value));
            }
        }

        return retf;

    }

Usage (Spark DataGrid)

var labelFunction = getDateLabelFunction();

or for MX Datagrid

var labelFunction = getDateLabelFunction(null,true);

to pass custom Date Format String:

var labelFunction = getDateLabelFunction("DD/MM/YYYY",true);

Default is a "MM/DD/YYYY";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文