Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction

Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction

30 April 2010

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


I’ve written a few posts about the AdvancedDataGrid, including one where you can show a Tree inside of it. I’ve never written a post about the iconFunction though, so I thought it would be a good idea to do that. This post described how you can use a Tree in an AdvancedDataGrid where the Tree Node icons are dependant of XML node name and other data in the XML element.

In fact it’s quite straightforward, you just have to know how to do it. First, I will create some dummy XML data in my initialize method, from that data, I create an hierarchical data variable that I can use in my AdvancedDataGrid so that it shows a Tree.

Some variables we need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import mx.collections.HierarchicalData;

[Bindable]
private var dataHierarchy:HierarchicalData;

[Embed(source="assets/dialog-error.png")]
private var iconError:Class;

[Embed(source="assets/dialog-warning.png")]
private var iconWarning:Class;

[Embed(source="assets/weather-clear.png")]
private var iconClear:Class;

[Embed(source="assets/office.png")]
private var iconOffice:Class;

The initialize method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private function init():void
{
    var i:int;
    // Root Node: Corporate Finance
    var xmlData:XML = <Finance></Finance>;
    xmlData.@itemName = "Corporate Finance";
    // Sales node
    var sales:XML = <Sales></Sales>;
    sales.@itemName = "Corporate Sales";
    // Expenses Node
    var expenses:XML = <Expenses></Expenses>;
    expenses.@itemName = "Corporate Expenses";
   
    // Append Sales and Expenses to Coporate finance
    xmlData.appendChild(sales);
    xmlData.appendChild(expenses);
   
    var salesItem:XML;
   
    // Add some random sales items
    for( i=0; i<5; i++)
    {
        salesItem = new XML(<item></item>);
        salesItem.@amount = Math.round(Math.random() * 100000);
        salesItem.@itemName = "Random Sale " + i;
        sales.appendChild(salesItem);
    }
   
    // Add some random expense items
    var expensesItem:XML;
    for( i=0; i<5; i++)
    {
        expensesItem = new XML(<item></item>);
        expensesItem.@amount = Math.round(Math.random() * 100000);
        expensesItem.@itemName = "Random Expense " + i;
        expenses.appendChild(expensesItem);
    }
    // Greate hierarchy based on created XML
    dataHierarchy = new HierarchicalData(xmlData); 
}

Now they very simple layout:

1
2
3
4
5
6
7
8
9
10
<s:Button label="Generate New Data" click="init()"/>

<mx:AdvancedDataGrid id="adg" width="100%" height="100%"
                     dataProvider="{dataHierarchy}" iconFunction="iconFunction">
    <mx:columns>
        <mx:AdvancedDataGridColumn headerText="Finance" width="350"
                                   labelFunction="labelFunction"/>
        <mx:AdvancedDataGridColumn headerText="Amount" dataField="@amount" width="150"/>
    </mx:columns>
</mx:AdvancedDataGrid>

The label function (declared on the first AdvancedDataGridColumn, to return the node name):

1
2
3
4
5
private function labelFunction(item:XML, col:AdvancedDataGridColumn):String
{
    // Return itemName as the label for the node
    return item.@itemName;
}

And last but not least, the icon function to return the icon depending on node name and other data (read comments in source for explanation):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private function iconFunction(item:XML):Class
{
    var parentName:String;
    var parentNode:XML;
    parentNode = item.parent();
   
    // Return office icon if node name not equal to item
    if( item.name() != "item" )
    {
        return iconOffice;
    }
   
    // Check if parent not null
    if (parentNode != null ){
        // Get parent name
        parentName = parentNode.name();
       
        // Sales below 30000: return error icon
        // Expenses below 30000: return clear icon
        if( item.@amount < 30000 )
        {
            return parentName == "Sales" ? iconError : iconClear;
        }
        // Warning icon: the same threshold for both cases
        else if ( item.@amount < 60000 )
        {
            return iconWarning;
        }
        // amount is greater than 60000, return clear if sales
        // and error if Expenses
        else
        {
            return parentName == "Sales" ? iconClear : iconError;
        }
    }
    // default if none of the above apply
    return iconOffice;
}

That’s it! Here’s the sample application followed by the complete source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               initialize="init()" width="500" height="400"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955" minHeight="600" viewSourceURL="srcview/index.html">
    <s:layout>
        <s:VerticalLayout paddingBottom="5" paddingLeft="5"
                          paddingRight="5" paddingTop="5"/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.collections.HierarchicalData;
           
            [Bindable]
            private var dataHierarchy:HierarchicalData;
           
            [Embed(source="assets/dialog-error.png")]
            private var iconError:Class;
           
            [Embed(source="assets/dialog-warning.png")]
            private var iconWarning:Class;
           
            [Embed(source="assets/weather-clear.png")]
            private var iconClear:Class;
           
            [Embed(source="assets/office.png")]
            private var iconOffice:Class;
           
           
            private function init():void
            {
                var i:int;
                // Root Node: Corporate Finance
                var xmlData:XML = <Finance></Finance>;
                xmlData.@itemName = "Corporate Finance";
                // Sales node
                var sales:XML = <Sales></Sales>;
                sales.@itemName = "Corporate Sales";
                // Expenses Node
                var expenses:XML = <Expenses></Expenses>;
                expenses.@itemName = "Corporate Expenses";
               
                // Append Sales and Expenses to Coporate finance
                xmlData.appendChild(sales);
                xmlData.appendChild(expenses);
               
                var salesItem:XML;
               
                // Add some random sales items
                for( i=0; i<5; i++)
                {
                    salesItem = new XML(<item></item>);
                    salesItem.@amount = Math.round(Math.random() * 100000);
                    salesItem.@itemName = "Random Sale " + i;
                    sales.appendChild(salesItem);
                }
               
                // Add some random expense items
                var expensesItem:XML;
                for( i=0; i<5; i++)
                {
                    expensesItem = new XML(<item></item>);
                    expensesItem.@amount = Math.round(Math.random() * 100000);
                    expensesItem.@itemName = "Random Expense " + i;
                    expenses.appendChild(expensesItem);
                }
                // Greate hierarchy based on created XML
                dataHierarchy = new HierarchicalData(xmlData); 
            }
           
            private function labelFunction(item:XML, col:AdvancedDataGridColumn):String
            {
                // Return itemName as the label for the node
                return item.@itemName;
            }
           
            private function iconFunction(item:XML):Class
            {
                var parentName:String;
                var parentNode:XML;
                parentNode = item.parent();
               
                // Return office icon if node name not equal to item
                if( item.name() != "item" )
                {
                    return iconOffice;
                }
               
                // Check if parent not null
                if (parentNode != null ){
                    // Get parent name
                    parentName = parentNode.name();
                   
                    // Sales below 30000: return error icon
                    // Expenses below 30000: return clear icon
                    if( item.@amount < 30000 )
                    {
                        return parentName == "Sales" ? iconError : iconClear;
                    }
                    // Warning icon: the same threshold for both cases
                    else if ( item.@amount < 60000 )
                    {
                        return iconWarning;
                    }
                    // amount is greater than 60000, return clear if sales
                    // and error if Expenses
                    else
                    {
                        return parentName == "Sales" ? iconClear : iconError;
                    }
                }
                // default if none of the above apply
                return iconOffice;
            }
           
        ]]>
    </fx:Script>
   
<s:Button label="Generate New Data" click="init()"/>

<mx:AdvancedDataGrid id="adg" width="100%" height="100%"
                     dataProvider="{dataHierarchy}" iconFunction="iconFunction">
    <mx:columns>
        <mx:AdvancedDataGridColumn headerText="Finance" width="350"
                                   labelFunction="labelFunction"/>
        <mx:AdvancedDataGridColumn headerText="Amount" dataField="@amount" width="150"/>
    </mx:columns>
</mx:AdvancedDataGrid>
   
</s:Application>

Related posts:

  1. Drag and Drop from DataGrid or AdvancedDataGrid to Tree
  2. Style AdvancedDataGrid depending on data example
  3. Change open and close icons on Flex Tree
  4. Data Dependent decoratorClass in MobileIconItemRenderer Example
  5. Tree in Advanced DataGrid Example

Written by Arjan · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 5.00 out of 5)
Loading ... Loading ...

d1ccf9347c00d35082100c0954d0c379delicious

Comments

4 Responses to “Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction”

  1. Said on May 19th, 2010 6:30 pm

    Hi

    This is really a nice example.

    All the best
    Said

  2. armolina on November 11th, 2010 9:16 am

    Good example!!

    thanks.

  3. Suka on May 23rd, 2011 10:16 am

    Thanx that muuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuch =)

  4. Mahesh on September 27th, 2011 7:37 am

    Hi Arjan, nice example, can u tell me how can i handle the hierarchical tree node clicking event so the i can adjust the height of the advanceddatagrid when tree collapses and expands

    Thanq in advance

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · AdobeĀ® and AdobeĀ® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)