apache-flexdatagridlabelfunction

Flex DatagridColumn LabelFunction Additonal Parameters


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.


Solution

  • 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.