Wednesday, May 21, 2008

Flex Formatter: Coder its own trainers ActionScript

If any of Formatter standard is not suitable to your needs, you can write your own Formatter. To write a personalized Formatter, you must create a class which extends ActionScript mx.formatters.Formatter. This class must override the method format (). The following example create a new Formatter which will add to 0 that the chain is the length of the property "count" of Formatter:
---------------------------------------------------------------------------------------------
package
{
import mx.formatters.Formatter;
public class ZeroFillFormatter extends Formatter {
private var _count:int;
public function set count(value:int):void{
_count = value;
}
public function get count():int{
return _count;
}
public function ZeroFillFormatter(){
super();
_count = -1;
}
// méthode format() à surcharger
override public function format(value:Object):String{
// si nécéssaire, on convertit le paramètre en une String, sinon on le cast en String
var stringValue:String;
if(!(value is String)){
stringValue = value.toString();
} else {
stringValue = String(value);
}
// si la longueur de la chaîne est inférieure à _count,
// on la précède par des zéros
while (_count > stringValue.length){
stringValue = "0" + stringValue;
}
return stringValue;
}
}
}

---------------------------------------------------------------------------------------------

No comments: