Thursday, February 9, 2012

Creating Hierarchical ArrayCollection out of Flat Array Collection where each record has three fields ID, Name, PID (say)

Here my flat array collection was having three fields ZZCUST_VEND (ID),ZZNAME (name),ZZHCUST_VEND(PID) and sample data would be like

            var flatArrayCollection:ArrayCollection = new ArrayCollection([
            {ZZCUST_VEND:"1", ZZNAME:"A", ZZHCUST_VEND:"0"},
            {ZZCUST_VEND:"2", ZZNAME:"B", ZZHCUST_VEND:"0"},
            {ZZCUST_VEND:"3", ZZNAME:"C", ZZHCUST_VEND:"0"},
            {ZZCUST_VEND:"4", ZZNAME:"D", ZZHCUST_VEND:"0"},
            {ZZCUST_VEND:"5", ZZNAME:"A1", ZZHCUST_VEND:"1"},
            {ZZCUST_VEND:"6", ZZNAME:"B1", ZZHCUST_VEND:"2"},
            {ZZCUST_VEND:"7", ZZNAME:"C1", ZZHCUST_VEND:"3"},
            {ZZCUST_VEND:"8", ZZNAME:"A11", ZZHCUST_VEND:"5"},
            {ZZCUST_VEND:"9", ZZNAME:"B11", ZZHCUST_VEND:"6"},
            {ZZCUST_VEND:"10", ZZNAME:"C11", ZZHCUST_VEND:"7"},
            {ZZCUST_VEND:"11", ZZNAME:"D1", ZZHCUST_VEND:"4"},
            {ZZCUST_VEND:"12", ZZNAME:"D11", ZZHCUST_VEND:"11"},
            {ZZCUST_VEND:"13", ZZNAME:"E", ZZHCUST_VEND:"0"},
            {ZZCUST_VEND:"14", ZZNAME:"E1", ZZHCUST_VEND:"13"},
            {ZZCUST_VEND:"15", ZZNAME:"C12", ZZHCUST_VEND:"7"},
            ]); 
I want to show this in a hierarchical fashion. So I created a Tree Utility class to handle this.The post is available at here.


I hope this will be helpful.

2 comments:

Mohammad Samaha said...

Hello Akhil,
I was trying to use the code that you've posted but I got the following error:

Index '1' specified is out of bounds.

could you advice what might be the cause?

var rootNode:TreeNodeH = new TreeNodeH;
try {
rootNode = TreeCreator.createHierarchicalArrayCollection(flatArrayCollection);
hierarchicalArrayCollection.addItem(rootNode);
myTree.dataProvider = hierarchicalArrayCollection;
}
catch (e:Error)
{
Alert.show(e.message.toString(),"error");
}

Akhil Mittal said...

Hi Mohammad,

I could not see your comment. I hope the problem is resolved. In any emergency you can email me at: contacttoakhil@gmail.com