Custom header in DataGrid
November 15th, 2007 at 05:39pm Administrator
If you're new here, you may want to subscribe to my RSS feed. If you like my site, consider linking back. I prefer text "Free Flash Tutorials" to http://blog.franto.com
Thanks for visiting my site! If you need anything just contacting through Contact page or Gtalk widget
Table of contents for DataGrid
- Custom header in DataGrid
- Custom header in DataGrid - part 2
- “Filter results in DataGrid” Flex Tutorial
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:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx :VBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="left" paddingLeft="3" verticalGap="5">
-
</mx><mx :Script>
-
<![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];
-
}
-
}
-
]]>
-
</mx>
-
<mx :Label id="topTextLbl" text="{topText}" fontWeight="bold"/>
-
<mx :Label id="txtLbl" text="{text}" fontWeight="bold"/>
Now we can create our custom DataGrid by extending DataGrid class. Here is the code:
-
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];
-
}
-
}
-
}
Now just make application to show our custom DataGrid with custom header
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx :Application xmlns:local="*" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:controls="controls.*">
-
-
<controls :MyDataGrid width="300" rowCount="5" dataProvider="{dp}"/>
-
-
</mx><mx :Script>
-
<![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'}]);
-
]]>
-
</mx>
And here you can try this example (Source code is available on right click):

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
Flash, Flex, FlexTips, Tutorial, Useful linksEntry Filed under: Tutorial, FlexTips, Flex, Useful links, Flash
»Related posts:
- Custom header in DataGrid - part 2
- “Filter results in DataGrid” Flex Tutorial
- New design on Franto.com
- CellRenderer rowHeight
- Search on my site
- FlexTips: Tween in AS3.0 project
- FlexTips - mx:Text vs. mx:TextArea
- Best Project Management Software for IT company
- AsDoc with Flex Library Project
- Flash Maps Collection





November 16th, 2007 at 1:27 am
You could extend DataGridColumn and create a CustomDataGridColumn which would take any properties u define and pass them into the headerRenderer.
November 16th, 2007 at 12:44 pm
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
November 19th, 2007 at 10:24 am
[…] Custom header in DataGrid - part 2 […]