Flex DatagridColumn LabelFunction Additonal Parameters

14.3k Views Asked by At

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.

3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On

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
}
0
On

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