Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flash Developer Center /

Customizing and sorting the DataGrid component

by Peter deHaan

Peter deHaan

Content

  • Creating data grid columns using the DataGridColumn object
  • Sorting data grid columns numerically
  • Determining the current sort column and sort order
  • Formatting data grid columns
  • Formatting data grid rows and headers
  • Where to go from here

Created

15 October 2008

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript components Flash Professional CS4

Requirements

Prerequisite knowledge

General experience with programming in ActionScript 3 is suggested.

User level

Intermediate

Required products

  • Flash Professional CS4 (Download trial)

Sample files

  • section5.example1.zip (455 KB)
  • section6.example1.zip (455 KB)
  • section7.example1.zip (455 KB)
  • section8.example1.zip (455 KB)
  • section8.example2.zip (455 KB)
  • section9.example1.zip (455 KB)

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.

Creating data grid columns using the DataGridColumn object

There are two main ways to add columns to a DataGrid instance:

  • Pass a string to the data grid's addColumn() or addColumnAt() method.
  • Pass a DataGridColumn object (fl.controls.dataGridClasses.DataGridColumn) to the data grid's addColumn() or addColumnAt() method.

By using the DataGridColumn object, you can control additional attributes of the data grid column, such as the following:

  • Specify whether the column is editable (DataGridColumn.editable)
  • Specify whether the column is resizable (DataGridColumn.resizable)
  • Specify the column's header text (DataGridColumn.headerText)
  • Specify a custom function which generates a cell's text (DataGridColumn.labelFunction)
  • Specify whether the column is sortable (DataGridColumn.sortable)
  • Specify sorting options (DataGridColumn.sortOptions)
  • Specify a custom sorting function (DataGridColumn.sortCompareFunction)

Example

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);

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download section5.example1.zip at the top of this page.

Sorting data grid columns numerically

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.

Example

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.

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download section6.example1.zip from the top of this page.

Determining the current sort column and sort order

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.

Example

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)); }

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download section7.example1.zip from the top of this page.

Formatting data grid columns

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.

Example

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);

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

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.

Example

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); }

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download section8.example2.zip at the top of this page.

Formatting data grid rows and headers

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.

Example

 

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);

Result

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download section9.example1.zip from the top of this page.

Where to go from here

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:

  • "Using the DataGrid" in Using ActionScript 3.0 Components
  • The DataGrid class in the ActionScript 3.0 Components and Language Reference
  • Creating, populating, and resizing the DataGrid component (Part 1 of 3)

Related Flash Quick Starts

  • Using the Button component
  • Using the Label component
  • Getting started with Flash CS4 user interface components
  • Creating, populating, and resizing the DataGrid component
  • Filtering and formatting data in the DataGrid component
  • Displaying images with the TileList component

More Like This

  • Embedding fonts
  • Event handling in ActionScript 3
  • Working with symbols and the Document class
  • Display list programming in ActionScript 3
  • Using ActionScript 3 drawing commands
  • Displaying images with the TileList component
  • Creating a simple ActionScript 3 class
  • Programming with the Vector class
  • Creating, populating, and resizing the DataGrid component
  • Loading images and Library assets with ActionScript 3

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement