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 / Flex Developer Center /

Understanding Flex itemEditors – Part 1: Inline itemEditors

by Peter Ent

Peter Ent
  • Adobe

Created

26 January 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flex

Requirements

Prerequisite knowledge

Prior experience working with Flex Builder to create applications is needed.

User level

Intermediate

Required products

  • Flex Builder 3 (Download trial)

Sample files

  • itemEditors_pt1.mxml (5 KB)

I recently completed a series on itemRenderers ― customizations to list controls that format the display of the list contents. Displaying and rendering content is a very effective UI technique and with Flex you can do nearly anything you can imagine.

This is Part 1 of a new series covering itemEditors, which allow data to be changed directly inside of a list control. This first article covers inline itemEditors, which are very simple, though quite useful, components you write directly inside your MXML files. Later articles in the series will cover more complex editing, validation, events, and using itemRenderers as itemEditors.

The TextInput editor

Editing directly in list controls is convenient. Imagine a DataGrid of warehouse inventory where you can adjust the content right in the grid without needing a special pop-up (see Figure 1). The list controls have a built in editor, a TextInput control, that appears whenever the user clicks in an editable area, either a row (for a List), a branch (for a Tree), or a cell (for a DataGrid). All you need to do is set the list control's editable property to true. For a DataGrid you can exclude a column from being edited by setting the DataGridColumn's editable property to false.

itemEditors allow editing directly within a DataGrid
Figure 1. itemEditors allow editing directly within a DataGrid

itemEditors differ from itemRenderers in that only one instance of the itemEditor is seen, just on the cell being edited. The itemEditor is not seen until the cell to be edited receives input focus. Then the itemRenderer is hidden and the itemEditor is moved to that position, sized to fit the area, and given the data for the record. When editing is finished (by moving focus to another location), the list control copies the new value from the editor to the dataProvider record.

In the application shown in Figure 1, when the user clicks in a cell of the "Part #" column, the dataProvider[row][dataField] value is given to the text property of the itemEditor (TextInput) control. When editing is finished, the text property value from the itemEditor (TextInput) control is copied to the dataProvider[row][dataField]. The dataProvider, being a collection, dispatches an event in response to the update.

While the default TextInput control makes a fine editor, it really only works for the most simple of cases. It works fine for String values, for example, such as a book title, author name, or product number. If you need more control or want to validate the user's input, then you need to take matters into your own hands.

Flex Controls as itemEditors

Here is how you make an itemEditor which only accepts numeric values:

<mx:DataGrid x="46" y="270" editable="true" dataProvider="{employeeDB}"> <mx:columns> <mx:DataGridColumn headerText="Name" dataField="name"/> <mx:DataGridColumn headerText="Position" dataField="position"/> <mx:DataGridColumn headerText="Age" dataField="age"> <mx:itemEditor> <mx:Component> <mx:TextInput restrict="0-9" maxChars="3" /> </mx:Component> </mx:itemEditor> </mx:DataGridColumn> </mx:columns> </mx:DataGrid>

The restrict and maxChars properties ensure that age values are constrained to three-digit numbers.

The CheckBox is another common control to use for an itemEditor, because it is useful for editing Boolean values. Figure 2 shows an example of using the CheckBox to edit the values for an "In Stock" column of an inventory program.

Using a CheckBox as an itemEditor for a Boolean value
Figure 2. Using a CheckBox as an itemEditor for a Boolean value

Here is the code to make it work:

<mx:DataGrid x="531" y="273" editable="true" dataProvider="{inventoryDB}"> <mx:columns> <mx:DataGridColumn headerText="Product" dataField="product"/> <mx:DataGridColumn headerText="Part #" dataField="part"/> <mx:DataGridColumn headerText="In Stock?" dataField="inStock" labelFunction="inStockLabeler" itemEditor="mx.controls.CheckBox" editorDataField="selected" /> <mx:DataGridColumn headerText="Quantity" dataField="quantity"/> </mx:columns> </mx:DataGrid>

In this example the content of the cells in this column are rendered using a labelFunction (inStockLabeler), which can display descriptive strings such as "Yes", "No", "In Stock", or "Out of Stock". The itemEditor property is set to the mx.controls.CheckBox class. And there is another, equally important, property set on the DataGridColumn: editorDataField. This field indicates the property of the itemEditor class to use to fetch the value when editing is finished. In this case it is the CheckBox's selected property. When editing is finished, the DataGrid will use the CheckBox's selected property to replace the inStock property in the data record.

At this point, you may wonder why the example with the TextInput did not supply the editorDataField property. That is because the default value for editorDataField is "text" which just happens to be the name of the property on the TextInput control holding the value.

You can use this same technique with a number of Flex controls. Here is one for an order quantity column using NumericStepper, as shown in Figure 3:

<mx:DataGrid x="531" y="82" editable="true" dataProvider="{inventoryDB}"> <mx:columns> <mx:DataGridColumn headerText="Product" dataField="product"/> <mx:DataGridColumn headerText="Part #" dataField="part"/> <mx:DataGridColumn headerText="In Stock?" dataField="inStock"/> <mx:DataGridColumn headerText="Quantity" dataField="quantity" itemEditor="mx.controls.NumericStepper" editorDataField="value"/> </mx:columns> </mx:DataGrid>
Using a NumericStepper to edit quantities
Figure 3. Using a NumericStepper to edit quantities

Notice the editorDataField is "value" - the property of the NumericStepper that holds the current value of the control. Make sure you use the fully-qualified class name for the itemEditor property or else the compiler will not be able to find the class and flag the line with an error.

A more complex editor

Now suppose you want to do something a little more complex that doesn't have a ready-made Flex control available. Here is one which allows the user to enter a credit card number using four separate four-digit fields (see Figure 4):

Editing a credit card number in four separate fields
Figure 4. Editing a credit card number in four separate fields

Here is the code to make it work:

<mx:DataGrid x="46" y="463" editable="true" dataProvider="{accountDB}" width="302"> <mx:columns> <mx:DataGridColumn headerText="Account" dataField="account" width="100"/> <mx:DataGridColumn headerText="Credit Card" dataField="ccard" editorDataField="value"> <mx:itemEditor> <mx:Component> <mx:HBox> <mx:Script> <![CDATA[ public function get value() : String { return part1.text+part2.text+part3.text+part4.text; } override public function set data(value:Object):void { super.data = value; part1.text = value.ccard.substr(0,4); part2.text = value.ccard.substr(4,4); part3.text = value.ccard.substr(8,4); part4.text = value.ccard.substr(12,4); } ]]> </mx:Script> <mx:TextInput id="part1" maxChars="4" restrict="0-9" width="40" /> <mx:TextInput id="part2" maxChars="4" restrict="0-9" width="40" /> <mx:TextInput id="part3" maxChars="4" restrict="0-9" width="40" /> <mx:TextInput id="part4" maxChars="4" restrict="0-9" width="40" /> </mx:HBox> </mx:Component> </mx:itemEditor> </mx:DataGridColumn> </mx:columns> </mx:DataGrid>

This inline itemEditor follows the same rules as other itemEditors and names the editorDataField as "value". The component chosen for the itemEditor is the HBox, which does not have a "value" property. To make this itemEditor work, a getter function named value is created to return the concatenation of the four input fields. Now when the user is finished editing the cell, the DataGrid can call upon the value property of the itemEditor and it will receive the combined fields.

Note the super.data = value in the data setter function. The data property - really the data getter function - is used extensively behind the scenes in the List controls and elsewhere in the framework (not to mention your own code). If you don't set the internal value of data using super.data, then the data getter function will return a null value and, most likely, cause your application to crash.

You can also see that I have overridden the data setter function. In that function I split up the credit card number among the four TextInput fields. This is the technique used to display the data to be edited. The editorDataField is the property used to retrieve the new value.

Where to go from here

In this article you've seen how to create an inline itemEditor by simply naming a class and by creating a complex class right within the MXML tags. Naming the property of the editor class that contains the final editor value allows the DataGrid to retrieve the value from the editor instance and replace the current value in the data.

Part 2 of this series covers more complex itemEditors and editing events.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

Tutorials & Samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex User Forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

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