I want to gray out the background of some rows in my Spark DataGrid. Whether I gray out the row is dependent on the data.
I create a custom itemRenderer that creates a new gray Rect as the background. In each of my GridColumns, I'm using itemRenderFunction to decide if I use the custom gray itemRenderer or the default renderer.
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″
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="0×505050″ 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);
}
+