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!

    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
  • Guest Poster
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
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 (36 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. Tree in Advanced DataGrid Example
  5. Style Flex Tree Label Example

Written by Arjan · Filed Under Examples 

Was this post useful to you?

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

Comments

One Response 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

Get Adobe Flash player

  • Sponsors

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • Flash Builder
    • Flash Builder 4
    • Flashvars
    • Flex 4
    • Framework
    • HSlider
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
    • VSlider
  • Flex Blog Readers

  • Flex Jobs (from Flex Jobs.org)

    Your Job here? Post your job @ Flex Jobs.org

    Advertisement

  • Recent Posts

    • SWIZ Framework in Flex 4 Example: Part I
    • Dynamic AdvancedDataGridColumn in Flex 4
    • How to make a magnetic button in Flex 4
    • Using ASDoc as an External Tool in Flash Builder 4
    • Flex Encryption (MD5, SHA1, SHA224, SHA256, HMAC)
    • The SWIZ framework for Flex 3 / Flex 4: Easy, Light and Very Powerfull
    • Call Javascript function from Flex / Flash Builder (AS3)
    • Flex FlashVars in AS3 Example
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction
    • WordPress Flash / Flex Comments Form
  • Categories

    • Examples
    • Guest Poster
  • Archives

    • 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

    • List Directory with AIR in Flex 4 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Control Webpage in HTML Control in AIR 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Progressbar in Datagrid 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)
    • WordPress Flash / Flex Comments Form 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)