Today I finally gave up on writing itemRenderers/Editors by hand in favor of data driven approach. Few of my postings went to different forums, I will start collecting them here. The goal is to show all sides of power offered by DataGrid and factories -based approach.
More will emerge, for now here is the first trivial case – automating formatting of SSN, phone, currency, shortdate and anything else.
Define DataGridColumnEx with one extra property:
public function set formatString( fs :String ) : void{
var fm:Formatter = new Formatter();
switch (fs.toLowerCase()) {
case “ssn”:
fm = new MaskFormatter(“###-##-####”);
break;
case “currency”:
fm = new CurrencyFormatter();
break;
case “phone”:
fm = new PhoneFormatter();
break;
case “shortdate”:
fm = new DateFormatter();
break;
//……………………..
}
this.labelFunction =
function (data:Object, dgc:DataGridColumnEx):String { return fm.format(data[dgc.dataField]);}
}
Now you can do just this in your code:
<lib:DataGridColumnEx dataField=”PHONE” formatString=”phone” />
<lib:DataGridColumnEx dataField=”SS_NUMBER” formatString=”ssn” />
<lib:DataGridColumnEx dataField=”SALARY” formatString=”currency”
etc.
Not much – but at least we started to move in the data-driven direction….
Technorati Tags: Flex, format, itemRenderer, DataGrid, DataGridColumn, ActionScript, labelFunction, data, driven

