15 October 2008
General experience with programming in ActionScript 3 is suggested.
Intermediate
This is the second of three quick start tutorials that explain best practices for working with the ActionScript 3.0 DataGrid component. This quick start demonstrates how to customize data grid columns, sort data grid columns numerically, format numbers in data grid columns using a custom label function, and how to specify a custom text format for both data grid headers and data grid rows.
This article shows you how to do the following tasks with data grids: create columns using the DataGridColumn object, sort columns numerically, determine the current sort column and sort order, and format columns, rows, and headers.
Note: The following examples require that a DataGrid component exists in your document's library.
There are two main ways to add columns to a DataGrid instance:
addColumn() or addColumnAt() method.addColumn() or addColumnAt() method.By using the DataGridColumn object, you can control additional attributes of the data grid column, such as the following:
DataGridColumn.editable)DataGridColumn.resizable)DataGridColumn.headerText)DataGridColumn.labelFunction)DataGridColumn.sortable)DataGridColumn.sortOptions)DataGridColumn.sortCompareFunction)The following example creates a new DataGridColumn instance and adds it to the DataGrid using the addColumn() method:
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:"1234.000"});
dp.addItem({columnA:"Row 2A", columnB:"56.300"});
dp.addItem({columnA:"Row 3A", columnB:"789.123"});
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);
To get the source files for this example, download section5.example1.zip at the top of this page.
By default, columns are sorted alphabetically. If the user tries to sort a column containing numeric data the columns may not be sorted as expected. In order to specify that a column contains numeric data you must set the column's sortOptions property and specify that the column is numeric using the Array.NUMERIC constant.
The following example uses the DataGridColumn class's sortOptions property to specify that the values in columnB should be sorted numerically when the column's header is clicked:
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:"1234.000"});
dp.addItem({columnA:"Row 2A", columnB:"56.300"});
dp.addItem({columnA:"Row 3A", columnB:"789.123"});
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);
Note: By default columns are sorted alphabetically using a case sensitive sort. If you want to specify that the column should use a case insensitive sort, set the column's sortOptions parameter to Array.CASEINSENSITIVE.
To get the source files for this example, download section6.example1.zip from the top of this page.
Often when building an application you need to determine the current sort column and sort order, since the user can re-sort columns at any time by clicking on a column's header. The following example shows how to listen for the headerRelease event (DataGridEvent.HEADER_RELEASE) to determine the current sort order after a user clicks on a column header.
The following example creates a DataGrid component instance and traces the current column and sort order when a data grid header is clicked:
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import fl.events.DataGridEvent;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:1234.00});
dp.addItem({columnA:"Row 2A", columnB:56.30});
dp.addItem({columnA:"Row 3A", columnB:789.12});
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
To get the source files for this example, download section7.example1.zip from the top of this page.
When working with data grids you'll often need to create custom formatting for certain columns. Some examples of custom formatting include: loading numeric data with two or three trailing decimal places, or concatenating two fields (such as first name and last name) and displaying it in one column.
The following example creates a DataGrid component instance and populates it with both text and numeric data:
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:1234.00});
dp.addItem({columnA:"Row 2A", columnB:56.30});
dp.addItem({columnA:"Row 3A", columnB:789.12});
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);
To get the source files for this example, download section8.example1.zip from the top of this page.
Note that since columnB is using numeric data instead of strings, the data grid doesn't show trailing zeroes after the decimal points. The next example shows how to format numeric column data the way you want.
You can use the labelFunction property of the DataGridColumn class and the toFixed() method of the Number class to display numeric column data with two decimal places, as shown here:
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:1234.00});
dp.addItem({columnA:"Row 2A", columnB:56.30});
dp.addItem({columnA:"Row 3A", columnB:789.12});
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
colB.labelFunction = numberLabelFunction;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);
function numberLabelFunction(item:Object):String {
return Number(item.columnB).toFixed(2);
}
To get the source files for this example, download section8.example2.zip at the top of this page.
When developing applications with the DataGrid component, you may want to use a custom font so the data grid fits with your overall design. You change fonts by setting a custom text format using a TextFormat object, and the setStyle() or setRendererStyle() methods. If you want to set the text format for a DataGrid component's header, use the setStyle() method along with the headerTextFormat style. If you want to set the text format for each row in the DataGrid component, use the setRendererStyle() method along with the textFormat style.
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var comicTextFormat:TextFormat = new TextFormat();
comicTextFormat.font = "Comic Sans MS";
var comicBoldTextFormat:TextFormat = new TextFormat(comicTextFormat.font);
comicBoldTextFormat.bold = true;
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:"Row 1B"});
dp.addItem({columnA:"Row 2A", columnB:"Row 2B"});
dp.addItem({columnA:"Row 3A", columnB:"Row 3B"});
var colA:DataGridColumn = new DataGridColumn("columnA");
colA.headerText = "Column A:";
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.headerText = "Column B:";
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.setStyle("headerTextFormat", comicBoldTextFormat);
myDataGrid.setRendererStyle("textFormat", comicTextFormat);
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);
To get the source files for this example, download section9.example1.zip from the top of this page.
For further information about this topic, see Filtering and formatting data in the DataGrid component (Part 3 of 3). For more information about the ActionScript 3.0 DataGrid component: