Custom header in DataGrid

Thursday, November 15th, 2007

This is simple tutorial with question at the end to help me and other understand it correctly.

Let’s say we need DataGrid with 2 rows in header for each column. It’s not hard. In DataGridColumn class there is property headerRenderer so we can just implement our custom headerRenderer.

Here is code as MXML Component:
[as]
< ?xml version="1.0" encoding="utf-8"?>


< ![CDATA[

import mx.controls.dataGridClasses.DataGridColumn;

[Bindable]
private var text:String;
[Bindable]
private var topText:String;
[Bindable]
private var src:String;

override public function set data(value:Object):void
{
var col:DataGridColumn = value as DataGridColumn;
if (col)
{
var arr:Array = col.headerText.split(':::');
text = arr[0];
topText = arr[1];
}
}
]]>


[/as]

Now we can create our custom DataGrid by extending DataGrid class. Here is the code:
[as]
package controls
{
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.ClassFactory;

public class MyDataGrid extends DataGrid
{
public function MyDataGrid()
{

}

override protected function commitProperties():void
{
super.commitProperties();

draggableColumns = false;
editable = false;
resizableColumns = true;
sortableColumns = true;

var colName:DataGridColumn = new DataGridColumn();
colName.headerRenderer = new ClassFactory(MyDataGridHeader);
colName.headerText = “Name:::Web”;
colName.dataField = “name”;

var colLink:DataGridColumn = new DataGridColumn();
colLink.headerRenderer = new ClassFactory(MyDataGridHeader);
colLink.headerText = “Link:::URL”;
colLink.dataField = “link”;

columns = [colName, colLink];
}
}
}
[/as]

Now just make application to show our custom DataGrid with custom header
[as]
< ?xml version="1.0" encoding="utf-8"?>


< ![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var dp:ArrayCollection = new ArrayCollection([{name:'Franto.com Blog',url:'http://blog.franto.com'},{name:'Adobe.com Labs',url:'http://labs.adobe.com'}]);
]]>

[/as]

And here you can try this example (Source code is available on right click):
Custom DataGrid with custom header

Question for the Flex Gurus:

Now I got custom header with 2 rows, so I need change 2 properties for that header. Since there is just properties headerText which is String and can not be Object, my only workaround was set headerText to serialized string e.g. ‘text1:::text2‘ and in data setter deserialize. But I wonder if there is a way how to set my bindable vars from headerRenderer text‘ and ‘topText‘ directly. If anyone can help I would appreciate that. Thanks

AddThis Feed Button

This is some text prior to the author information. You can change this text from the admin section of WP-Gravatar  I'm living in Bratislava, Slovakia. I'm Senior Flex, AIR Developer for my own company Flexets. Read more from this author


tagged under:

ABOUT THIS AUTHOR

I'm living in Bratislava, Slovakia. I'm Senior Flex, AIR Developer for my own company Flexets.

Get a Trackback link

1 Trackbacks/Pingbacks

  1. Pingback: Franto.com Flash blog » Custom header in DataGrid - part 2 on November 19, 2007

3 Comments


  1. Tink
    Visit Site
    November 16th, 2007

    You could extend DataGridColumn and create a CustomDataGridColumn which would take any properties u define and pass them into the headerRenderer.


  2. Tink
    Visit Site
    November 16th, 2007

    package ws.tink.flex.controls.dataGrid
    {
    import mx.controls.dataGridClasses.DataGridColumn;

    public class CustomDataGridColumn extends DataGridColumn
    {

    private var _customValue:Object;

    public function get customValue():Object
    {
    return _customValue;
    }
    public function set customValue( value:Object ):Void
    {
    _customValue = value;
    }

    public function newInstance():*
    {
    var renderer:* = __itemRenderer.newInstance();

    renderer.customValue = _customValue;

    return renderer;
    }
    }
    }

    somat like that should do the job


  3. paper machines
    Visit Site
    July 4th, 2008

    im adding value Thank you for sharing this great tutorial.

Live Preview

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

This site is using OpenAvatar based on
Images is enhanced with WordPress Lightbox JS by Zeo
Clicky Web Analytics