<![CDATA[
// import com.render.TreeRender;
import mx.collections.HierarchicalData;
import mx.collections.ArrayCollection;
// import com.model.SettingModel;
// import com.manager.ConnectionConstants;
import mx.controls.Alert;
private var responder:Responder;
[Bindable]
private var result:ArrayCollection=new ArrayCollection();
// public var settingModel:SettingModel=SettingModel.getInstance();
private var childPresent:Boolean=false;
private var result2:ArrayCollection=new ArrayCollection();
public function init():void
{
// getSection();
result2.addItem({section_id:23,ref_section_id:13,name:"Abotus1",children:new Array()});
result2.addItem({section_id:24,ref_section_id:23,name:"Abotus2",children:new Array()});
result2.addItem({section_id:89,ref_section_id:10,name:"Terns2",children:new Array()});
result2.addItem({section_id:19,ref_section_id:3,name:"Contact",children:new Array()});
result2.addItem({section_id:14,ref_section_id:12,name:"Businewss",children:new Array()});
result2.addItem({section_id:13,ref_section_id:12,name:"Abotus",children:new Array()});
result2.addItem({section_id:12,ref_section_id:3,name:"Corporate",children:new Array()});
result2.addItem({section_id:11,ref_section_id:20,name:"Disclaimer",children:new Array()});
result2.addItem({section_id:10,ref_section_id:20,name:"Terns",children:new Array()});
result2.addItem({section_id:9,ref_section_id:20,name:"policy",children:new Array()});
result2.addItem({section_id:8,ref_section_id:3,name:"Release",children:new Array()});
result2.addItem({section_id:21,ref_section_id:null,name:"Music",children:new Array()});
result2.addItem({section_id:22,ref_section_id:null,name:"Blog",children:new Array()});
result2.addItem({section_id:3,ref_section_id:null,name:"Music",children:new Array()});
result2.addItem({section_id:2,ref_section_id:null,name:"Home",children:new Array()});
result2.addItem({section_id:20,ref_section_id:3,name:"Policy",children:new Array()});
/* result.addItem({section_id:2,ref_section_id:null,name:"Home",children:new Array()});
result.addItem({section_id:3,ref_section_id:null,name:"ThinkMusic",children:new Array()});
result.addItem({section_id:22,ref_section_id:null,name:"Blog",children:new Array()});
result.addItem({section_id:21,ref_section_id:null,name:"Music",children:new Array()});
result.addItem({section_id:8,ref_section_id:3,name:"Press Release",children:new Array()});
result.addItem({section_id:20,ref_section_id:3,name:"Policy",children:new Array()});
result.addItem({section_id:9,ref_section_id:20,name:"Privacy policy",children:new Array()});
result.addItem({section_id:10,ref_section_id:20,name:"Terns",children:new Array()});
result.addItem({section_id:11,ref_section_id:20,name:"Disclaimer",children:new Array()});
result.addItem({section_id:12,ref_section_id:3,name:"Corporate",children:new Array()});
result.addItem({section_id:13,ref_section_id:12,name:"Abotus",children:new Array()});
result.addItem({section_id:14,ref_section_id:12,name:"Businewss",children:new Array()});
result.addItem({section_id:19,ref_section_id:3,name:"Contact",children:new Array()});*/
filter1();
// formTree();
}
public function getSection():void
{
responder=new Responder(onGetResult,onFault);
// settingModel.connManager.call(ConnectionConstants.cmsBackEndClass+"."+ConnectionConstants.getAllSectionMethod,responder);
}
private function addClick():void
{
}
public function onGetResult(e:Object):void
{
result2.removeAll();
for(var i:int=0;i<e.length;i++)
{
var arr:Array=new Array();
result2.addItem({name:e[i].name,children:arr,description:e[i].description,flag:e[i].flag,ref_section_id:e[i].ref_section_id
,section_id:e[i].section_id,sequence:e[i].sequence,type:e[i].type});
}
filter1();
//formTree();
}
public function filter1():void
{
result.removeAll();
for(var i:int=0;i<result2.length;i++)
{
if(result2[i].ref_section_id!=null)
{
if(!childPresentOrnotresult2(result2[i].section_id))
{
result.addItem(result2[i]);
result2.removeItemAt(i);
i–;
}
}
}
for(i=0;i<result2.length;i++)
{
result.addItem(result2[i]);
}
formTree();
}
public function formTree():void
{
var j:int=0;
var count:int=0;
var lastId:int=0;
var refId:int=0;
for(var i:int=0;i<result.length;i++)
{
if(result[i].ref_section_id!=null)
{
lastId=chkForChilds(result[i].section_id);
refId=getRef(lastId);
var index:int=getSectionIndex(refId);
}
else
{
continue;
}
if(index!=-1)
{
for(var z:int=0;z0)
index–;
count++;
}
}
}
}
if(count!=0)
{
i–;
count=0;
}
}
for(i=0;i<result.length;i++)
{
if(result[i].ref_section_id==null)
{
j++;
}
}
if(j!=result.length)
formTree();
// settingModel.lastSectionSeq=j;
var hr:HierarchicalData=new HierarchicalData();
hr.source=result;
settingTree.dataProvider=hr;
settingTree.validateNow();
// settingTree.expandAll();
}
public function getSectionIndex(id:int):int
{
for(var i:int=0;i<result.length;i++)
{
if(result[i].section_id==id)
{
return i;
}
}
return -1;
}
public function getRef(id:int):int
{
for(var i:int=0;i<result.length;i++)
{
if(id==result[i].section_id)
{
return result[i].ref_section_id;
}
}
return 0;
}
public function onFault():void
{
Alert.show("Fault in Section service");
}
public function chkForChilds(id:int):int
{
var inP:int=0;
childPresent=false;
for(var j:int=0;j<result.length;j++)
{
if(result[j].ref_section_id!=null)
{
if(id==result[j].ref_section_id)
{
inP=result[j].section_id;
childPresent=true;
chkForChilds(result[j].section_id);
}
}
}
if(!childPresent)
{
return id;
}
else
return inP;
}
public function childPresentOrnot(id:int):Boolean
{
for(var j:int=0;j<result.length;j++)
{
if(id==result[j].ref_section_id)
{
return true;
}
}
return false;
}
public function childPresentOrnotresult2(id:int):Boolean
{
for(var j:int=0;j<result2.length;j++)
{
if(id==result2[j].ref_section_id)
{
return true;
}
}
return false;
}
public function removeChilds(id:int):void
{
for(var i:int=0;i<result.length;i++)
{
if(id==result[i].section_id)
{
result.removeItemAt(i);
break;
}
}
}
In the above component result2 array we have section id and refrence section id in each element,refrence section id is the section id of another element which indicates the parent node so here refsection id indicates the parent sectionid,with this we have to form a tree structure.
Here is how i have done .
First i have rearranged the array and formed anew array so that all childs appear first in the array then at topmost appears.The topmost
parents have refsectionid null .
Then we are taking the section id of first element and finding the whether it has any childs and chkforchilds() method will return the last child section id.from the last child section id we are taking the refrence id of the last child which is the parent and attach all nodes with that refsection id to the parent if the node is not having child and the node is removed from the result array
this continues tilll the topmost elements alone left in the array.
Preview :http://www.box.net/shared/bofeauioku
feedback