为Flex Tree控件构造树形结构数据

将数据库里查出来的列表结构转为树形结构的ArrayCollection,需要数据有id字段(可以不叫id),parent项(对应父节点id),有children项(实现ITreeData接口)。

示例:
var ac:ArrayCollection = new ArrayCollection();
var d1:Department = new Department();
d1.id = "1";
d1.name = "校";
var d2:Department = new Department();
d2.id = "2";
d2.name = "班1";
d2.parent = "1";
var d3:Department = new Department();
d3.id = "3";
d3.name = "班2";
d3.parent = "1";
var d4:Department = new Department();
d4.id = "4";
d4.name = "生1";
d4.parent = "2";
var d5:Department = new Department();
d5.id = "5";
d5.name = "生2";
d5.parent = "2";

ac.addItem(d1);
ac.addItem(d2);
ac.addItem(d3);
ac.addItem(d4);
ac.addItem(d5);

var td:ArrayCollection = TreeDataCreator.toTreeAC(ac);
trace(td);


输出结果:
{id:1, name:校, parent:null, children:[{id:2, name:班1, parent:1, children:[{id:4, name:生1, parent:2, children:[]}, {id:5, name:生2, parent:2, children:[]}]}, {id:3, name:班2, parent:1, children:[]}]}

详细代码见附件

你可能感兴趣的:(数据,递归,tree)