alistairphillips.com

I’m : a web and mobile developer based in the Manning Valley, Australia.


Adobe Flex

Adobe Flex 3 One of the requirements for our web-based software at the office was to display reports with fixed headers. Since the holy grail of a cross-browser HTML solution does not yet exist we started looking into Adobe Flex 3 and it's advanced datagrid. Some useful links:

The old HTML report that we were display had row highlighting that was applied depending on the contents of the data. In all we were toggling between 3 different styles, something that the AdvancedDataGrid cannot do out of the box. After trying various workarounds what turned out to be a workable solution was to extend the AdvancedDataGrid and override the drawRowBackground method.

The hardest part was getting access to the data in the row, something that needed to be sent across in the call to rowColorFunction(). The solution is to cast your dataProvider (note I had to use this.dataProvider as dataProvider itself was always null) as a HierarchicalCollectionView and then use a cursor over it and seek to the dataIndex value. With that done the object you get back can be accessed like an array, i.e item['first_name'].

RowColorAdvancedDataGrid.as

package
{
    import flash.display.Sprite;
    import mx.collections.CursorBookmark;
    import mx.collections.HierarchicalCollectionView;
    import mx.collections.HierarchicalCollectionViewCursor;
    import mx.controls.AdvancedDataGrid;

    public class RowColorAdvancedDataGrid extends AdvancedDataGrid
    {
        public var rowColorFunction:Function;

        override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void
        {
            if ( rowColorFunction != null )
            {
                if ( this.dataProvider )
                {
                    if ( rowIndex < listItems.length )
                    {
                        var dp:HierarchicalCollectionView = this.dataProvider as HierarchicalCollectionView;
                        var cursor:HierarchicalCollectionViewCursor = dp.createCursor() as HierarchicalCollectionViewCursor
                        cursor.seek( CursorBookmark.FIRST, dataIndex );
                        var item:Object = cursor.current;
                        if ( item ) {
                            color = rowColorFunction( item, rowIndex, dataIndex, color );
                        }
                    }
                }
            }

            super.drawRowBackground( s, rowIndex, y, height, color, dataIndex );
        }
    }
}