The idea came from a flexcoders post here. After attempting the previous example of row background colors in ADG I thought it should be easy to achive this which resulted in this post.
I extended the DGColumn and added a "alternatingItemColors" style it so that it would be possible to set it per column.
I extended the DGItemRenderer and used the background and backgroundColor properties to set the items background color. I added logic in a overridden validateNow function to pick up the color from the alternatingItemColors color array.
The drawback of this approach is that the selection feedback gets almost hidden by the background coloring which would not be the case if the drawRowBackground function is overridden.
and feed into DG or ADG it won't create columns auto-magically as it does for Object data type. This may be because it is difficult to decide whether you want to display the child nodes as columns or attributes of nodes. And may be it was not appropriate to clutter the API with one more flag to do this.
I wrote the following code to get this working.
private function generateCols(input:XMLList,
useAttributes:Boolean = false):Array
{
var e1:XML = input[0];
var columns:Array = [];
var children:XMLList ;
if (useAttributes)
children = e1.attributes();
else
children = e1.children();
for each(var child:XML in children)
{
var col:DataGridColumn = new DataGridColumn();
col.dataField = useAttributes ? "@" + child.name() : child.name();
var fieldName:String = child.name();
col.headerText = fieldName.charAt(0).toUpperCase() + fieldName.substr(1);
columns.push(col);
}
return columns;
}
I have seen numerous questions regarding how to specify DataGridColumn.dataField when the data is nested (object or XML). Developers attempt to use "datafield1.datafield2.datafield3" to find out that it doesn't work !
The data might look like this :
<mx:XMLList id="testData" xmlns="">
<root>
<item>
<childItem>
<name>India Flex group</name>
<mail>indiafx@gmail.com</mail>
</childItem>
<location>India</location>
</item>
<item>
<childItem>
<name>Singapore Flex group</name>
<mail>singaporefx@gmail.com</mail>
</childItem>
<location>Singapore</location>
</item>
<item>
<childItem>
<name>Sreenivas</name>
<mail>sreenivas.ramaswamy@gmail.com</mail>
</childItem>
<location>Bangalore</location>
</item>
</root>
</mx:XMLList>
The answer has always (most of the time?) been to use DataGrid.labelFunction. I dislike the solution because it requires the developer to do this again and again for every column and every usage. I find it easier to extend the DataGridColumn and override the itemToLabel function to do the needful.
The usage is simple. The Nested and ordinary columns can be mixed or only nested columns can be used as the logic kicks in only when "." is present in dataField value.