Custom header in DataGrid

Štvrtok, 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


tagged under:

ABOUT THIS AUTHOR

1 Trackbacks/Pingbacks

  1. Spätné upozorenie: Franto.com Flash blog » Custom header in DataGrid - part 2 on November 19, 2007

4 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
    Júl 4th, 2008

    im adding value Thank you for sharing this great tutorial.


  4. chito
    Visit Site
    November 14th, 2009

    hi i cant seem to find the example files anywhere on this page. could you please email me. im kinda new to flex and im having trouble making sense of your tute. this is something that i really need for my application. thanks

Sorry, comments for this entry are closed at this time.

Images is enhanced with WordPress Lightbox JS by Zeo