I created a custom formatter that takes a decimal number and returns a fraction (string). I can use it with some sample text areas, like so:
Code:
<comp:FractionFormatter id="makeFraction" myNumber="{myTI.text}"/>
<mx:TextInput id="myTI"/>
<mx:TextArea text="Formatted string is {makeFraction.format(myTI.text)}"/>
But if I try to use it in my DataGrid, like so:
Code:
<mx:DataGridColumn dataField="{makeFraction.format(@qty)}" width="35" wordWrap="true"/>
The compile fails with "Error: Access of undefined property qty."
Suggestions?
Here's the formatter:
Code:
package myComponents
{
//Import base Formatter class
import mx.formatters.Formatter
public class FractionFormatter extends Formatter {
// Declare the variable to hold the pattern string.
public var myNumber:String;
// Constructor
public function FractionFormatter() {
// Call base class constructor.
super();
}
// Override format().
override public function format(value:Object):String {
//Validate value - it must be a nonzero length string.
if(value.length == 0) {
// Return empty string and error message for zero-length string.
error="Zero Length String";
return ""
}
//If the value is valid, format the string.
switch (myNumber) {
case ".25" :
return "1/4";
break;
case ".5":
return "1/2";
break;
case ".75":
return "3/4";
break;
case "1":
return "1";
break;
case "1.25":
return "1 1/4";
break;
default :
// If formatString is not "upper" or "lower",
// return empty string and set the error message.
error="Invalid Format String";
return "";
}
}
}
}