| By Victor Rasputnis | Article Rating: |
|
| February 3, 2007 10:00 AM EST | Reads: |
22,002 |
What I love about Adobe Flex is that it is a framework in addition to a pretty impressive library of off-the-shelf controls, which can fit the bill for many of the Rich Internet Applications. Flex enables you to create new and extend existing components with a simplicity and elegance hardly ever offered by other GUI development systems. In this article I'll show you how to start extending a standard ComboBox component, which is a combination of edit field, button and a dropdown list. We will be customizing the API and adding some new functionality, making our ComboBox a bit handier than a standard one.
A typical task, while working with a standard ComboBox, is to programmatically select a specific value. Suppose our ComboBox is populated with array of states:
private var usStates:Array=[
{label:"New York", data:"NY"},
{label:"Colorado", data:"CO"},
{label:"Texas", data:"TX"}
];
. . . . . . . .
<mx:ComboBox id="cbx_states" dataProvider="{usStates}"/>
To programmatically select Texas (to the visible portion of the ComboBox), you can write the following index-fetching loop, comparing val against the label of each element of dataProvider:
var val:String;
val = 'Texas' ;
for (var i: int = 0; i < cbx.dataProdider.length; i++) {
if ( val == cbx_states.dataProvider[i].label) {
cbx_states.selectedIndex = i;
break;
}
}
Alternatively, you could look up the data of dataProvider's elements :
var val:String;
val = 'TX' ;
for (var i: int = 0; i < cbx.dataProdider.length; i++) {
if ( val == cbx.dataProvider[i].data) {
cbx_states.selectedIndex = i;
break;
}
}
Either way these index-fetching loops will clutter the application code instead of simple cbx_states.value='Texas'.
But wait, there is a value property: if a selected object has something in the data property, value refers to data, otherwise value refers to the label:
mx.control.Alert.show(cbx_states.value); // displays 'NY'
Alas, value is a read-only property. It is still helpful as it shields us from selectedItem/ selectedIndex. What we miss is another half and in the following sections we will turn value into a read-write property. That will forever absolve us from index-fetching loops to modify the ComboBox selection.
The simplest way to do this is by extending the original ComboBox so that derived class provides a special setter for the value property. The setter attempts to match the value with either data or label properties of the dataProvider. Once a match is found, it modifies the selectedIndex which should cause the ComboBox to select the matching object:
<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
<![CDATA[
public function set value(val:Object) : void {
if ( val != null ) {
for (var i : int = 0; i < dataProvider.length; i++) {
if ( val == dataProvider[i].data || val == dataProvider[i].label) {
selectedIndex = i;
return;
} } }
selectedIndex = -1;
}
]]>
</mx:Script>
</mx:ComboBox>
If the ComboBox.mxml is located under the com/theriabook/controls, its test application can look as in Listing 3 below.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
xmlns:lib="com.theriabook.controls.*">
<mx:ArrayCollection id="comboData" >
<mx:Array>
<mx:Object label="New York" data="NY"/>
<mx:Object label="Connecticut" data="CT"/>
<mx:Object label="Illinois" data="IL"/>
</mx:Array>
</mx:ArrayCollection>
<mx:Label text="Current bound value is '{cbx_1.value}' " />
<lib:ComboBox id="cbx_1" value="IL" width="150" dataProvider="{comboData}"/>
</mx:Application>
Published February 3, 2007 Reads 22,002
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Victor Rasputnis
Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Executives Feature on Cloud Computing Expo Power Panel
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Ph.D. in Twitter Anyone?
- Adobe Flex Developer Earns $100K in New York City
- Eolas Sues the Internet
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Special Report on the Emerging Cloud Computing Trend
- My Thoughts on Ulitzer
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Ulitzer Live! New Media Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Executives Feature on Cloud Computing Expo Power Panel
- Software Flexibility in the Cloud - Part 4 of 5
- Is Microsoft as Free as Open Source?
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Where Are RIA Technologies Headed in 2008?
- Cover Story: How to Increase the Frame Rates of Your Flash Movies
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Your First Adobe Flex Application with a ColdFusion Backend
- Adobe Flex 2: Advanced DataGrid
- i-Technology Blog: Death-Knell For "Rich Media? Hardly!
- Adobe/Macromedia - Microsoft, Look Out!
- How To Create a Photo Slide Show ...
- Adobe Flex Interface Customization - Themes, Styles, Skins
- Personal Branding Checklist
- Has the Technology Bounceback Begun?
- "Real-World Flex" by Adobe's Christophe Coenraets



































