In this tutorial I will try to explain in a easy way an example usage of the cellRenerer API of Flash MX 2004 component.
A tipical usage of the cellrenderer is for example into a DataGrid which we would like to customize in order to contain an image, or a check box..
This is the final example: ( Download the tutorial files from here )
Another example can be found here: http://www.sephiroth.it/test/complexGrid.html
1. Setup the PHP script.
Where are loaded the data of the datagrid from? I setup a php page which will send a serialized array of objects. Then Flash will receive and unserialize this data using the Serializer class for Flash MX 2004.
Note that you need to install this Class in order to work with this tutorial
<?php
// ========================================================
// http://www.sephiroth.it/tutorials/flashPHP/cellRenderer
// Alessandro Crugnola
// =======================================================
// custom object (for passing to Flash)
class oData{
var $id, $available, $item, $preview, $quantity, $price, $total;
function oData($id, $available, $item, $preview, $quantity, $price){
$this->id = $id;
$this->available = $available;
$this->item = $item;
$this->preview = $preview;
$this->quantity = $quantity;
$this->price = $price;
$this->total = $price*$quantity;
}
}
// main data array (array of objects)
$mainData = array();
$mainData[] = new oData(1,true,"This is the link to <font color='#000066'><u><a href='cnn.com'>Australia</a></u></font>.<br><b>Be right back</b>","AUS.jpg",1, 200);
$mainData[] = new oData(2,true,"Canada","CAN.jpg",2, 420);
$mainData[] = new oData(3,true,"Italy","ITA.jpg",3, 340);
$mainData[] = new oData(4,false,"Brazil","BRA.jpg",0, 100);
$mainData[] = new oData(5,true,"Austria","AUT.jpg",3, 200);
// print the serialized variables in
// the correct ouput for flash
echo "output=" . urlencode(utf8_encode(serialize($mainData)));
?>
As you can see I create and the push into the mainArray a class object "oData". Then i pack all the mainArray with the PHP serialize function.
Now, if you open this page from the browser you will see a string like this:
output=a%3A5%3A%7Bi%3A0%3BO%3A5%3A%22odata%22%3A7%3A%7Bs%3A2%3A%22id%2...
Don't worry about it, this is the serialized array. Next tasks will be the step for unserialize this string and come back to the original array
2. Usage of Serializer Class
Ok, how to deserialize this kind of data? (see also the serializer home-page)
Open your flash and in a blank document write this:
import it.sephiroth.Serializer
// now load the PHP variables
var Class_Serializer:Serializer = new Serializer()
var myLoadVars:LoadVars = new LoadVars();
var data:Object;
myLoadVars.load("http://www.sephiroth.it/tutorials/flashPHP/cellRenderer/files/server.php");
myLoadVars.onLoad = function(success){
// unserialize the data object
data = Class_Serializer.unserialize(this.output)
/*
result is:
Variable _level0.data = [object #7, class 'Array'] [
0:[object #8, class 'Object'] {
id:1,
available:true,
item:"Australia",
preview:"AUS.jpg",
quantity:1,
price:200,
total:200
},
1:[object #9, class 'Object'] {
id:2,
available:true,
item:"Canada",
preview:"CAN.jpg",
quantity:2,
price:420,
total:840
}, ...
*/
}
First important step is to import the Serializer Class (with the "import" command).
Then normally load the php page with a LoadVars object, and once the data is loaded use the "unserialize" method of the Serializer class in order to get the complex data (as you can see in the comment this is an array of objects, like we build in the PHP file previously)