<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - Adobe</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Wed, 11 Jun 2008 18:15:36 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - Adobe - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>Flex Data Driven Controls - Part 2</title>
    <link>http://eduzine.edujini-labs.com/archives/31-Flex-Data-Driven-Controls-Part-2.html</link>
<category>Web 2.0</category><category>RIA</category><category>Flex</category><category>ActionScript</category>    <comments>http://eduzine.edujini-labs.com/archives/31-Flex-Data-Driven-Controls-Part-2.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=31</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=31</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In Part 2 of the series of articles on &amp;quot;Flex Data Driven Controls&amp;quot;, we learn about how to provide custom rendering to a data bound control. And we choose &lt;code&gt;List&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
In the previous article, &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html&quot;&gt;Part 1&lt;/a&gt; of the series, we got ourselves started with data driven controls - &lt;code&gt;ComboBox&lt;/code&gt; and &lt;code&gt;DataGrid&lt;/code&gt;. In this article, we will lean to cutomize the how the items are finally presented to the end user.
&lt;/p&gt;

&lt;p&gt;
The source code for this article is available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
We reuse the previous definition of &lt;code&gt;UserProfile&lt;/code&gt;. For completeness, the definition below is re-represented below:
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.tutorials.flex.core
{
  /**
   &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
   &lt;/strong&gt;
   &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
   &lt;/strong&gt;
   &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
   &lt;/strong&gt; http://www.edujini-labs.com
   */
  public class UserProfile
  {
    private var _employeeID : int;
    private var _employeeName: String;
    private var _departmentName: String;

    public function UserProfile(eid: int = 0, name: String = null, deptName: String = null)
    {
      this._employeeID = eid;
      this._employeeName = name;
      this._departmentName = deptName;
    }

    public function get employeeID(): int
    {
      return _employeeID;
    }

    public function set employeeID(eid: int) : void
    {
      this._employeeID = eid;
    }

    public function get employeeName(): String
    {
      return _employeeName;
    }

    public function set employeeName(name: String) : void
    {
      this._employeeName = name;
    }

    public function get departmentName(): String
    {
      return _departmentName;
    }

    public function set departmentName(name: String) : void
    {
      this._departmentName = name;
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;
As far the user interface is concerned, we replace the &lt;code&gt;ComboBox&lt;/code&gt; with &lt;code&gt;List&lt;/code&gt; and remove &lt;code&gt;DataGrid&lt;/code&gt; from the previous MXML definition. There would be a need to customize the layout a bit. The final definition looks as follows (important changes have been marked in bold):
&lt;/p&gt;

&lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;mx:Application xmlns:mx=&amp;quot;http://www.adobe.com/2006/mxml&amp;quot; layout=&amp;quot;absolute&amp;quot;
  height=&amp;quot;426&amp;quot; width=&amp;quot;400&amp;quot; borderThickness=&amp;quot;2&amp;quot; borderStyle=&amp;quot;solid&amp;quot;
  backgroundColor=&amp;quot;#F0F0F0&amp;quot; cornerRadius=&amp;quot;8&amp;quot;&amp;gt;

  &amp;lt;mx:Label text=&amp;quot;Edujini Labs Pvt Ltd&amp;quot; fontSize=&amp;quot;14&amp;quot; fontFamily=&amp;quot;Georgia&amp;quot;
  	fontWeight=&amp;quot;bold&amp;quot; fontStyle=&amp;quot;normal&amp;quot; color=&amp;quot;#F5A83B&amp;quot; left=&amp;quot;20&amp;quot; top=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;20&amp;quot; y=&amp;quot;64&amp;quot; text=&amp;quot;Edujini Team&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot; fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

  &lt;b&gt;&amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
  	cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot;&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;20&amp;quot; text=&amp;quot;© 2008, Edujini Labs Pvt Ltd&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot;
  	fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot; bottom=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Script&amp;gt;
    &amp;lt;![CDATA[
      import com.edujinilabs.eduzine.tutorials.flex.core.UserProfile;
      import mx.collections.ArrayCollection;

      [Bindable]
      private var employees: ArrayCollection = new ArrayCollection(
      [
        new UserProfile(11, &amp;quot;Shake Sajan&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(24, &amp;quot;Meera&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(23, &amp;quot;Neelam&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(12, &amp;quot;Hari S Nair&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(13, &amp;quot;Abdul Prodhani&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(21, &amp;quot;Ashish Vaish&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(10, &amp;quot;Gaurav Vaish&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(32, &amp;quot;Jagadish&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(33, &amp;quot;Anshu&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(22, &amp;quot;Padma Raviee&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(14, &amp;quot;Rajesh&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(31, &amp;quot;Yashwanth C&amp;quot;, &amp;quot;Management&amp;quot;),
        new UserProfile(34, &amp;quot;Ramesh&amp;quot;, &amp;quot;Finance&amp;quot;)
      ]);
    ]]&amp;gt;
  &amp;lt;/mx:Script&amp;gt;
&amp;lt;/mx:Application&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
With this, the UI that we expect is as follows:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.01.Initial.UI.jpg&quot; alt=&quot;Eduzine - Flex List - Renderer - Initial UI&quot; title=&quot;Eduzine - Flex List - Renderer - Initial UI&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Flex Item Renderers&lt;/h2&gt;

&lt;p&gt;
This interface is decently fine. Let's get something more out of this. How about change the UI to not only display the employees' names but also their Employee ID and the Department Name. And showing them a little formatted - say, in two lines rather than in a single line.
&lt;/p&gt;

&lt;p&gt;
Adobe Flex data driven controls work internally with the so called item renderers to represent a &amp;quot;list of items&amp;quot;. The external presentation may be linear, grid, tree or a combination of these.
&lt;/p&gt;

&lt;p&gt;
To get started, let us define a simple renderer for our &lt;code&gt;List&lt;/code&gt;. Modify the &lt;code&gt;List&lt;/code&gt; definition to as follows:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
    cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot;&amp;gt;
    &lt;b&gt;&amp;lt;mx:itemRenderer&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Name: {data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Department: {data.departmentName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemRenderer&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
The &lt;code&gt;itemRenderer&lt;/code&gt; comprises of one &lt;code&gt;VBox&lt;/code&gt; with two entries - a &lt;code&gt;Label&lt;/code&gt; to display the Employee ID, and a &lt;code&gt;HBox&lt;/code&gt; comprising of details for Employee Name and the Department Name.
&lt;/p&gt;

&lt;p&gt;
Note that the &lt;code&gt;itemRenderer&lt;/code&gt; element can contain only a definition of the &lt;code&gt;Component&lt;/code&gt; which in turn can comprise of only one component. For example, it is not possible to have a &lt;code&gt;Component&lt;/code&gt; with two &lt;code&gt;Label&lt;/code&gt; elements directly within itself. These &lt;code&gt;Labels&lt;/code&gt; must be enclosed in a container layout, like &lt;code&gt;VBox&lt;/code&gt; in this case.
&lt;/p&gt;

&lt;p&gt;
Also note that the controls are bount to values of the type &lt;code&gt;{data.*}&lt;/code&gt;. &lt;code&gt;data&lt;/code&gt; is a special variable in this case - it refers to the &lt;code&gt;item&lt;/code&gt; being rendered. For each iteration corresponding to the entry in the &lt;code&gt;dataProvider&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt; takes up the corresponding value. The type of the value referred to by &lt;code&gt;data&lt;/code&gt; is same as that in the &lt;code&gt;dataProvider&lt;/code&gt;, which in our case is &lt;code&gt;com.edujinilabs.eduzine.tutorials.flex.core.UserProfile&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
The UI expected is as below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.02.Simple.Renderer.jpg&quot; alt=&quot;Eduzine - Flex List - Custom Rendered Definition&quot; title=&quot;Eduzine - Flex List - Custom Rendered Definition&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Flex Item Editors&lt;/h2&gt;

&lt;p&gt;
Let's further fancy the UI and give the HR department an ease to be able to edit the details inline. When the HR personnel clicks on any entry, it should get into the edit mode and display the items as below:
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Employee ID: Must be shown in a &lt;code&gt;Label&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Employee Name: Must be shown in a &lt;code&gt;TextInput&lt;/code&gt;, so that it may be updated.&lt;/li&gt;
&lt;li&gt;Employee ID: Must be shown as a &lt;code&gt;ComboBox&lt;/code&gt;, with a list of options available.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Now that we already know how to data-bind the controls, we can directly jump on to looking at the updated &lt;code&gt;List&lt;/code&gt; definition in the MXML:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
    cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot; editable=&amp;quot;true&amp;quot; itemEditEnd=&amp;quot;event.preventDefault()&amp;quot;&amp;gt;
    &amp;lt;mx:itemRenderer&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Name: {data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Department: {data.departmentName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemRenderer&amp;gt;
    &lt;b&gt;&amp;lt;mx:itemEditor&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:TextInput text=&amp;quot;{data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:ComboBox width=&amp;quot;50%&amp;quot; selectedItem=&amp;quot;{data.departmentName}&amp;quot;&amp;gt;
              &amp;lt;mx:dataProvider&amp;gt;
                &amp;lt;mx:String&amp;gt;Product Dev&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Training&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Technology&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Finance&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Management&amp;lt;/mx:String&amp;gt;
              &amp;lt;/mx:dataProvider&amp;gt;
            &amp;lt;/mx:ComboBox&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemEditor&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
The updates in the code, now, must be self explanatory. You would also notice alternate form of specifying the &lt;code&gt;dataProvider&lt;/code&gt; for the &lt;code&gt;ComboBox&lt;/code&gt;. If you select any entry in the list, it must get into the edit-mode. The expected UI is as given below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.03.Item.Editor.jpg&quot; alt=&quot;Eduzine - Flex List - Custom Item Editor Definition&quot; title=&quot;Eduzine - Flex List - Custom Item Editor Definition&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
So, we seem to have got everything... except that the names / department names, if updated, do not effect the original items (the model). We shall explore this in our next article.
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt to:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Use Flex Item Renderers in data driven controls to create a rich UI to render the business objects.&lt;/li&gt;
 &lt;li&gt;Use Flex Item Editors to edit the business objects. We have left the task of updating the business objects to the next article.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;    </content:encoded>
    <pubDate>Tue, 10 Jun 2008 10:19:00 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/31-guid.html</guid>
    </item>
<item>
    <title>Flex Data Driven Controls - Part 1</title>
    <link>http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html</link>
<category>Web 2.0</category><category>RIA</category><category>Flex</category><category>ActionScript</category>    <comments>http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=30</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=30</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
Data driven controls are key to any UI toolkit, and Adobe Flex is no different.
&lt;/p&gt;

&lt;p&gt;
In this article, we explore some data driven controls in Flex and look at their basic functionalities, especially the data binding and custom rendering.
&lt;/p&gt;

&lt;p&gt;
In case you need a professional support on RIA, Web 2.0 and/or Adobe Flex, feel free to mail
&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;
  (function()
  {
    var _ej = 'y' + 'a' + 's' + 'h' + 'w' + 'a' + 'n' + 't' + 'h' + '@' + 'edujini' + '-' + 'labs' + '.com';
    document.write('&lt;a href=&quot;mailto:' + _ej + '?Subject=Flex / Web 2.0 Support&quot;&gt;' + _ej + '&lt;/a&gt;');
  })();
&lt;/script&gt;
&lt;/p&gt;

&lt;p&gt;
Downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; section.
&lt;/p&gt;

 
&lt;h2&gt;The Scenario&lt;/h2&gt;

&lt;p&gt;
Let's take a fictitious but practical scenario. The HR department at Edujini Labs to be given a dashboard to:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;Display employee names&lt;/li&gt;
 &lt;li&gt;Dispaly employee details&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;

&lt;p&gt;
Other complexities like capabilities to edit the employee details, paginate the listing if the list is long et al can be added later on.
&lt;/p&gt;

&lt;h2&gt;Getting Started...&lt;/h2&gt;

&lt;p&gt;
To get started, let's freeze on the following UI:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;For the employee list, we use a simple &lt;a href=&quot;http://livedocs.adobe.com/flex/3/langref/mx/controls/ComboBox.html&quot;&gt;ComboBox&lt;/a&gt; to get started with. Later we'd explore other options&lt;/li&gt;
 &lt;li&gt;For the details, we use &lt;a href=&quot;http://livedocs.adobe.com/flex/3/langref/mx/controls/DataGrid.html&quot;&gt;DataGrid&lt;/a&gt;.
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.01.UI.Definition.jpg&quot; alt=&quot;flex - datagrid/combobox - Edujini Labs HR Department UI&quot; title=&quot;flex - datagrid/combobox - Edujini Labs HR Department UI&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
The above UI can be generated using the following application definition (You will need Adobe Flex Builder to design it WSISYG or you can download the Adobe Flex SDK and use the MXML compiler to compile it at command line):
&lt;/p&gt;

&lt;pre&gt;&amp;lt;mx:Application xmlns:mx=&amp;quot;http://www.adobe.com/2006/mxml&amp;quot; layout=&amp;quot;absolute&amp;quot;
  height=&amp;quot;280&amp;quot; width=&amp;quot;600&amp;quot; borderThickness=&amp;quot;2&amp;quot; borderStyle=&amp;quot;solid&amp;quot;
  backgroundColor=&amp;quot;#F0F0F0&amp;quot; cornerRadius=&amp;quot;8&amp;quot;&amp;gt;

  &amp;lt;mx:Label text=&amp;quot;Edujini Labs Pvt Ltd&amp;quot; fontSize=&amp;quot;14&amp;quot;
      fontFamily=&amp;quot;Georgia&amp;quot; fontWeight=&amp;quot;bold&amp;quot; fontStyle=&amp;quot;normal&amp;quot;
      color=&amp;quot;#F5A83B&amp;quot; left=&amp;quot;20&amp;quot; top=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;21&amp;quot; y=&amp;quot;64&amp;quot; text=&amp;quot;Edujini Team&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot;
      fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

  &amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot;&amp;gt;&amp;lt;/mx:ComboBox&amp;gt;

  &amp;lt;mx:DataGrid x=&amp;quot;218&amp;quot; y=&amp;quot;92&amp;quot; width=&amp;quot;368&amp;quot;&amp;gt;
    &amp;lt;mx:columns&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Employee ID&amp;quot; dataField=&amp;quot;employeeID&amp;quot;/&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Employee Name&amp;quot; dataField=&amp;quot;employeeName&amp;quot;/&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Department&amp;quot; dataField=&amp;quot;departmentName&amp;quot;/&amp;gt;
    &amp;lt;/mx:columns&amp;gt;
  &amp;lt;/mx:DataGrid&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;21&amp;quot; y=&amp;quot;248&amp;quot; text=&amp;quot;© 2008, Edujini Labs Pvt Ltd&amp;quot;
      ontFamily=&amp;quot;Verdana&amp;quot; fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

&amp;lt;/mx:Application&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Note the definition of the &lt;code&gt;DataGrid&lt;/code&gt;. It contains a bunch of &lt;code&gt;DataGridColumn&lt;/code&gt; definitions. Each &lt;code&gt;DataGridColumn&lt;/code&gt; has been specified a &lt;code&gt;headerText&lt;/code&gt; - header of the column, and &lt;code&gt;dataField&lt;/code&gt; - the property of the object whose value is to be displayed.
&lt;/p&gt;

&lt;p&gt;
To model our data, let's create a class &lt;code&gt;UserProfile&lt;/code&gt; in package, say, &lt;code&gt;com.edujinilabs.eduzine.tutorials.flex.core&lt;/code&gt;. It comprises of the Employee ID, Employee Name and the Department Name.
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.tutorials.flex.core
{
  /**
   &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
   &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
   &lt;strong&gt;
   &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
   &lt;strong&gt; http://www.edujini-labs.com
   &lt;/strong&gt;/
  public class UserProfile
  {
    private var _employeeID : int;
    private var _employeeName: String;
    private var _departmentName: String;

    public function UserProfile(eid: int = 0,
                           name: String = null, deptName: String = null)
    {
      this._employeeID = eid;
      this._employeeName = name;
      this._departmentName = deptName;
    }

    public function get employeeID(): int
    {
      return _employeeID;
    }

    public function set employeeID(eid: int) : void
    {
      this._employeeID = eid;
    }

    public function get employeeName(): String
    {
      return _employeeName;
    }

    public function set employeeName(name: String) : void
    {
      this._employeeName = name;
    }

    public function get departmentName(): String
    {
      return _departmentName;
    }

    public function set departmentName(name: String) : void
    {
      this._departmentName = name;
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;
Notice that the property names have been purposely chosen to match the values of &lt;code&gt;dataField&lt;/code&gt; of the &lt;code&gt;DataGridColumn&lt;/code&gt;'s defined earlier.
&lt;/p&gt;

&lt;h2&gt;Data Model&lt;/h2&gt;

&lt;p&gt;
Employee list would be represented using the variable &lt;code&gt;employees&lt;/code&gt; of type &lt;code&gt;mx.collections.ArrayCollection&lt;/code&gt;. Ensure that it is &lt;code&gt;Bindable&lt;/code&gt;. Finally, let's popupate it with some data, as given below. Add the code directly into the MXML Application created.
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:Script&amp;gt;
    &amp;lt;![CDATA[
      import com.edujinilabs.eduzine.tutorials.flex.core.UserProfile;
      import mx.collections.ArrayCollection;

      [Bindable]
      private var employees: ArrayCollection = new ArrayCollection(
      [
        new UserProfile(11, &amp;quot;Shake Sajan&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(24, &amp;quot;Meera&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(23, &amp;quot;Neelam&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(12, &amp;quot;Hari S Nair&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(13, &amp;quot;Abdul Prodhani&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(21, &amp;quot;Ashish Vaish&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(10, &amp;quot;Gaurav Vaish&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(32, &amp;quot;Jagadish&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(33, &amp;quot;Anshu&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(22, &amp;quot;Padma Raviee&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(14, &amp;quot;Rajesh&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(31, &amp;quot;Yashwanth C&amp;quot;, &amp;quot;Management&amp;quot;),
        new UserProfile(34, &amp;quot;Ramesh&amp;quot;, &amp;quot;Finance&amp;quot;)
      ]);
    ]]&amp;gt;
  &amp;lt;/mx:Script&amp;gt;
&lt;/pre&gt;

&lt;h2&gt;Data Binding&lt;/h2&gt;

&lt;p&gt;
The next step is to bind the employee list to &lt;code&gt;ComboBox&lt;/code&gt; and the &lt;code&gt;DataGrid&lt;/code&gt;. Update the definition to as following:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot; &lt;b&gt;dataProvider=&amp;quot;{employees}&amp;quot;&lt;/b&gt;&amp;gt;&amp;lt;/mx:ComboBox&amp;gt;
&amp;lt;mx:DataGrid x=&amp;quot;230&amp;quot; y=&amp;quot;92&amp;quot; width=&amp;quot;302&amp;quot; &lt;b&gt;dataProvider=&amp;quot;{employees}&amp;quot;&lt;/b&gt;&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Note the special syntax - the variable &lt;code&gt;employees&lt;/code&gt; has been enclosed in the curly-braces &lt;code&gt;{}&lt;/code&gt;. This the data-binding syntax. It is a two step process:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Declare a variable with the attribute &lt;code&gt;Bindable&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;In MXML, use it within the curly braces.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
After binding, the UI should appear as given below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.02.After.Binding.jpg&quot; alt=&quot;Eduzine - Edujini Labs Employees - Data Binding Flex - UI&quot; title=&quot;Eduzine - Edujini Labs Employees - Data Binding Flex - UI&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;ComboBox&lt;/code&gt; Configuration&lt;/h2&gt;

&lt;p&gt;
Everything looks fine except for that the &lt;code&gt;ComboBox&lt;/code&gt; does not exactly display what we require. It displays the &lt;code&gt;String&lt;/code&gt; representation of the &lt;code&gt;UserProfile&lt;/code&gt; using the &lt;code&gt;toString()&lt;/code&gt; method.
&lt;/p&gt;

&lt;p&gt;
All we need to configure is the &lt;code&gt;labelField&lt;/code&gt; of the &lt;code&gt;ComboBox&lt;/code&gt; to tell it that it must pickup the text to be displayed to the UI from the corresponding property / field of the individual object of the collection. The default value of &lt;code&gt;labelField&lt;/code&gt; is &lt;code&gt;label&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Update the definition of the &lt;code&gt;ComboBox&lt;/code&gt; used to as follows:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot;
       labelField=&amp;quot;employeeName&amp;quot;&amp;gt;
  &amp;lt;/mx:ComboBox&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Voila! The UI is exactly as what we want:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.03.Custom.LabelField.jpg&quot; alt=&quot;Flex ComboBox Data Binding with custom LabelField&quot; title=&quot;Flex ComboBox Data Binding with custom LabelField&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt to:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Use data binding in Adobe Flex.&lt;/li&gt;
 &lt;li&gt;Bind data to the Flex controls &lt;code&gt;ComboBox&lt;/code&gt; and &lt;code&gt;DataGrid&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;Display custom objects in the Flex control &lt;code&gt;ComboBox&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;Display custom objects in the Flex control &lt;code&gt;DataGrid&lt;/code&gt; in multiple columns.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;    </content:encoded>
    <pubDate>Sat, 07 Jun 2008 22:39:44 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/30-guid.html</guid>
    </item>
</channel>
</rss>
