Monthly Archives: February, 2011

Adding an icon to the header for a Spark DataGrid

Adding a static icon to the header of Spark DataGrid is very easy. You will need to create a custom  headerRenderer and specify it for your GridColumn. Here are the steps:

1. Copy the contents of the skin file: DefaultGridHeaderRenderer.mxml and paste it into a new mxml f ile that will be used as your custom headerRenderer.

2. In the custom header renderer, find the HGroup at the end of the skin file. Add a BitmapImage as the first element in the HGroup (before the ‘labelDisplay’ Group).

<s:BitmapImage source="beer.JPG" />

3. You might want to make some padding or gap changes to the HGroup to position your icon. This example changed the gap to 8.

4. Specify this custom headerRenderer for your GridColumn:

<s:GridColumn id="c1_2" dataField="name" headerText="Pub Name"
    headerRenderer="IconHeaderRenderer2"/>

Voila! It’s simple. Note, if you wanted to modify the sortIndicator, you can also do it by creating a custom header renderer based off of the default one.

Sample Code: IconHeaderExample.mxml, IconHeaderRenderer.mxml IconHeaderRenderer2.mxml

Outlining the selected row in a Spark DataGrid

In this example, I am setting a red outline/stroke around the selected row in a DataGrid. At first, I thought I would need a custom itemRenderer. However, I discovered that the UI for how the DataGrid looks in its hovered, selected, disabled and normal states are determined by the DataGrid skin. Therefore, to get this selected row outlined, I created a custom DataGrid skin using the Flash Builder templates. In the  DataGrid skin, I just added the following to the selectionIndicator component:

<s:stroke>
<s:SolidColorStroke color=”0xC00000″ weight=”3″ />
</s:stroke>

Run the example: DataGridStrokedSelectionExample.swf
Sample Code: DataGridStrokedSelectionExample.mxml, StrokedSelectedItemSkin.mxml

Changing the background color of a row using itemRendererFunction in Spark

Changing the background color of a cell in a DataGrid is much easier, in my opinion, using the Spark architecture. The itemRenderer I am using is all mxml and uses the Rect graphic as the background. Here is the simple itemRenderer with a gray background and grayed out text.

<?xml version=”1.0″ encoding=”utf-8″?>
<s:GridItemRenderer xmlns:fx=”http://ns.adobe.com/mxml/2009&#8243;
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx” clipAndEnableScrolling=”true”>

<fx:Script>
<![CDATA[
override public function prepare(hasBeenRecycled:Boolean):void {
lblData.text = data[column.dataField]
}
]]>
</fx:Script>
<s:Rect top=”0″ bottom=”0″ right=”0″ left=”0″>
<s:fill>
<s:SolidColor color=”#E0E0E0″ alpha=”0.5″/>
</s:fill>
</s:Rect>
<s:Label id=”lblData” top=”9″ left=”7″ color=”0x505050″ alpha=”0.5″/>

</s:GridItemRenderer>

Using itemRendererFunction, I am determining whether to use this custom itemRenderer with the gray background, or the DefaultGridItemRenderer. If the item is in stock, use the DefaultGridItemRenderer, if it is not, use the custom itemRenderer. My itemRenderer function looks like this:

private function product_itemRendererFunction(item:Object, column:GridColumn):ClassFactory {

if(item.inStock)
return new ClassFactory(DefaultGridItemRenderer);
else
return new ClassFactory(OutOfStockItemRenderer);
}

Here are the files to download.

Sample Code: DataGrid_itemRendererFunctionExample.mxml, OutOfStockItemRenderer.mxml

Sample SWF: DataGrid_itemRendererFunctionExample.swf

Using a CheckBox in a Spark DataGrid

Today, we just posted a new stable build of the Flex 4.5 SDK on our opensource site. This is build 19786 and it includes a lot of improvements in our Spark DataGrid from the last stable build from November. Since the APIs for the DataGrid will likely not change between now and our release, I thought I’d start posting some simple DataGrid tutorials.

First up, this example shows how to use a CheckBox as an itemRenderer in a Spark DataGrid.  Coming from Flex 3, you will notice that the syntax for the Spark DataGrid is slightly different.

In this example, I have a three column DataGrid. The first two columns use the default itemRenderer. The last column uses an inline itemRenderer of a centered CheckBox.

Sample Code: DataGrid_CheckBoxExample.mxml

Run Sample: DataGrid_CheckBoxExample.swf