Thanks. i made i little changes to your sample, because having a
children arraycollection in class gets the tree control to always
display a folder icon. so this is the changed code. Thanks for the
kickstart.
------------------------------
Class Node.as
------------------------------
package
{
import mx.collections.ArrayCollection;
[Bindable]
public class Node
{
public var id:int=0;
public var label:String = "";
public var children:ArrayCollection;
public function aArr(obj:Object = null):void
{
if (obj != null)
{
this.id = obj.id;
this.label = obj.label;
this.children = null; // to get a page icon in tree
}
}
}
}
----------------------------
Project Main File text.mxml
----------------------------
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
public var dp:ArrayCollection = new ArrayCollection;
public function loadTree():void {
var Node1:Node = new Node();
Node1.id = 0;
Node1.label = "Root Node 1";
var Node2:Node = new Node();
Node2.id = 0;
Node2.label = "child Node 1";
var Node3:Node = new Node();
Node3.id = 0;
Node3.label = "child Node 2";
var Node4:Node = new Node();
Node4.id = 0;
Node4.label = "Root Node 2";
Node1.children = new ArrayCollection;
Node1.children.addItem(Node2);
Node2.children = new ArrayCollection;
Node2.children.addItem(Node3);
dp.addItem(Node1);
dp.addItem(Node4);
}
]]>
</mx:Script>
<mx:Tree height="223" width="244" dataProvider="{dp}"></mx:Tree>
<mx:Button label="Button" click="loadTree();"/>
</mx:Application>
-------------------------------
Thats all. and thanks to all.
--- In [email protected], "Nate Pearson" <[EMAIL PROTECTED]>
wrote:
>
> I would stick with what you know and used nested array
collections. I
> was about to implement this structure when I checked out flexcoders
> and saw your post. This should help you out a bit:
>
> 1) make your array into a class.
>
> package dto
> {
> import mx.collections.ArrayCollection;
>
> [Bindable]
> public class aArr
> {
> public var id:int=0;
> public var description:String = "";
> public var children:ArrayCollection = new
ArrayCollection
>
> public function aArr(obj:Object = null)
> {
> if (obj != null)
> {
> this.id = obj.id;
> this.description =
description.SD;
> this.children = new
ArrayCollection;
> }
> }
> }
> }
>
>
> As you see you have to add an arraycollection to your class. This
is
> where your children will go.
>
> 2) Build your data from what ever your source is and put it in
these
> nested array collections. If i were to do these from a .net
> webservice i would have it send over the data in a multidimensional
> array. You can add the children to the correct parents with slick
for
> and if statements.
>
> anyways, here is the code I would use to make a few layers(make sure
> you put class above in a file named "aArr.as" in a folder named
dto):
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application creationComplete="load()"
> xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
> <mx:Script>
> <![CDATA[
> import mx.collections.ArrayCollection;
> import mx.rpc.events.ResultEvent;
> import dto.*;
>
> [Bindable]
> public var treeArray:ArrayCollection = new
ArrayCollection;
>
> public function load():void{
> var aArrItem1:aArr = new aArr()
> aArrItem1.id = 0
> aArrItem1.description = "Root Node 1"
>
> var aArrItem2:aArr = new aArr()
> aArrItem2.id = 0
> aArrItem2.description = "child Node 1"
>
> var aArrItem3:aArr = new aArr()
> aArrItem3.id = 0
> aArrItem3.description = "child Node 2"
>
> var aArrItem4:aArr = new aArr()
> aArrItem4.id = 0
> aArrItem4.description = "Root Node 2"
>
> aArrItem1.children.addItem(aArrItem2)
> aArrItem2.children.addItem(aArrItem3)
> treeArray.addItem(aArrItem1)
> treeArray.addItem(aArrItem4)
> }
> ]]>
> </mx:Script>
> <mx:Tree height="100%" width="100%" dataProvider="{treeArray}"
> labelField="description" ></mx:Tree>
> </mx:Application>
>
> 3) You can see that to add a child you do addItem(). You can also
> specify where in the list you want to put the child with addItemAt
().
>
> I hope this helps. If you have problems getting ur array into this
> nested array collection structure or finding out parents just reply
> with a chunk of data and I'll show you how I would put it into this
form.
>
> -Nate
>
> --- In [email protected], "Sal" <nhp_ny@> wrote:
> >
> > hi,
> > i have an array of records received from an amfphp call and i
need
> > to fill a tree control with that data.
> >
> > the array has this format:
> >
> > aArr[0].id
> > aArr[0].description
> > aArr[0].parent
> > aArr[0].order
> >
> > aArr[1].id
> > aArr[1].description
> > aArr[1].parent
> > aArr[1].order
> >
> > ... and so on
> >
> > i didn't find any method of the control that adds node or that
sets
> > node parent.
> >
> > Any ideas?
> >
> > Thanks.
> >
>