Programming, Technology, Web Development, Whatever
Flex: Introduction to ItemRenderers
A critical thing to know for any Flex programmer performing data-visualization is the itemRenderer. The itemRenderer in Flex can be a tricky beast, however they’re the best way to interact with content within a datagrid, or the various list controls(tileList, horizontalList, etc).
An itemRenderer is basically an object that sits in the list in the place of the default component(For example, the default component of a datagrid is the Label class). Most itemRenderers have access to all data in their row. You can use them to improve the user experience when performing operations on data. You can use either an MXML component or an ActionScript component as an itemRenderer, however the MXML is more straightforward, so we’ll start with this. Here’s a simple example:
Here’s a datagrid with some users populated(creative, I know), now I’ve used an itemRenderer to add my custom button component into the datagrid to delete a user. Here’s the MXML:
<mx:DataGrid dataProvider="{nameArr}">
<mx:columns>
<mx:DataGridColumn
headerText="First Name"
dataField="firstName"/>
<mx:DataGridColumn
headerText="Last Name"
dataField="lastName"/>
<mx:DataGridColumn
itemRenderer="comp.DeleteBtn"/>
</mx:columns>
</mx:DataGrid>
Please note that a column that contains an itemRenderer does not need a dataField attribute, as it already has access to all dataFields for a particular row. The itemRenderer property contains the path to the class you wish to use in that column. The MXML component comp.DeleteBtn that is used as the itemRenderer looks like this:
<?xml version="1.0" encoding="utf-8"?> <mx:Button xmlns:mx="http://www.adobe.com/2006/mxml" label="Delete" dataChange="update();" click="deleteUser();" > <mx:Script>
<![CDATA[
//The unique Identifier for my user list
private var UID:int;
//this function is run whenever the data is updated
//you cannot rely on the creationComplete event to work if you're using a remoteObject
private function update():void
{
UID = data.uid;
}
//this is run whenever the button is clicked
private function deleteUser():void
{
//I would run a remoteObject that deletes this user from my database
}
]]>
</mx:Script>
</mx:Button>
So Flex creates a DeleteBtn object for every row in the datagrid, and each DeleteBtn component has access to that rows data. While the update() function is not required(as you could use data.uid instead of UID) it simply serves as a reminder that you need to be conscious of when the data an itemRenderer is receiving changes. You also cannot use the creationComplete function to perform actions when the data changes, as the itemRenderer is not recreated when a dataChange even occurs.
Here’s Another Example:
In this example I’m summing the columns to make a total, and formatting the result based on its value. In this particular instance the value will turn red if it is below 20, and green otherwise. I used an ActionScript itemRenderer for this one.
package comp
{
import mx.controls.Label;
public class Sum extends Label
{
//Constructor
public function Sum()
{
super();
}
//You'll need this function to update how the label looks in the datagrid.
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
//This runs the default action for refreshing a Label object, you need this.
super.updateDisplayList(unscaledWidth,unscaledHeight);
//Sum the sales numbers
var sum:int = data.popcorn + data.cotton + data.apples;
//Assign the labels text property to display the sum
text = sum.toString();
//Adjust the fontSize and fontWeight
setStyle("fontSize",14);
setStyle("fontWeight","bold");
//////////////////////////////////////////////////
// conditional formatting based on total sales //
//////////////////////////////////////////////////
//if the total sales is less than 20
if(sum > 20)
{
//set the color to green
setStyle("color",0x00AA00);
}
else
{
//set the color to red
setStyle("color",0xAA0000);
}
}
}
}
For the most part you’ll be overriding the updateDisplayList method to change the look of a component using an ActionScript class.
So there’s a tutorial for basic itemRenderers. A more advanced tutorial is to follow.
| Print article | This entry was posted by Nathan on December 13, 2009 at 11:17 PM, and is filed under Flex. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No trackbacks yet.
Disable DataTip targets in Flex charts
about 3 months ago - No comments
After working on my new data visualization project at Grooveshark, I’ve been looking for a way to remove the DataTip “targets”, which I have considered calling “points”, “dots”, and “circles” in my search to turn these little things off. In case you don’t know what they are, here’s a picture: After trudging through the Flex
Integer Division In Flex/Actionscript
about 7 months ago - No comments
If you’re used to C and other elemental high level programming languages, you know that 5÷9 = 0 and 10÷7 = 1. ActionScript being higher level than C automatically coerces the quotient of two numbers to the Number class, producing a decimal. To get the correct integer quotient, use the Math.floor function, which drops the
Intro to Try/Catch in Flex/Actionscript
about 7 months ago - No comments
Sometimes when you’re making a Flex application, you’ll expect a certain piece of code to fail at some point. This is the job of try and catch. For example, say I wanted to loop over a bunch of components I had within an HBox: <mx:HBox id="myHBox"> <mx:Panel title="1"/> <mx:Panel title="2"/> <mx:Panel title="3"/> <mx:Panel title="4"/> <mx:Panel
Access Specifiers in Flex/Actionscript
about 9 months ago - No comments
There are four types of basic access in Flex: Access Description public All objects and classes can access private Only objects with the same class can access protected Only objects with the same class or derived classes internal Only objects in the same package(essentially the same directory in flex) There are custom namespaces and other
Flex Auto-Resizing Datagrid
about 9 months ago - No comments
If you’ve ever wanted to make a datagrid that re-sizes to fit its content, heres how… Make a function that computes the size necessary to accommodate all the data: private function datagridChange():void { var newHeight:int = sampleDG.rowHeight * sampleDG.dataProvider.length //this is for the column headers, and the extra empty row + 2*sampleDG.rowHeight; sampleDG.height = newHeight;
Working with Events and Popups in Flex
about 9 months ago - No comments
Problem: If you’re like me in flex, you use a lot of popups. If you’re also like me, you constantly rely on event propagation. Unfortunately due to the nature of Flex, popups can be a tricky beast. When you create a popup using the PopUpManager class, it looks a little something like this: private function
about 8 months ago
I want to quote your post in my blog. It can?
And you et an account on Twitter?