最近有一项目,需要从mysql数据库读取数据,到Flex中呈现在Tree上,原来一直用读取xml,现在不想用xml了,直接读取数据后分析呈现。
如果是单层数据还好办,直接用XMLListCollection简要分析完就可以了,现在涉及到要分组显示,即相同职务的员工并到一组去
即:
设计师组: id employeeName employeeGroup .....
12 张三 设计师组
13 李四 设计师组
14 王二麻子 设计师组
烫染师组:
15 黑娃 烫染师组
16 张山 烫染师组
17 王三麻子 烫染师组
[img]http://dl.iteye.com/upload/attachment/455282/d820a85c-b4a6-3a90-819b-c9b3d38fc117.png[/img]
//项目的最终效果
[img]http://dl.iteye.com/upload/attachment/455971/d6df0858-dd50-3b58-b1c7-a83cb60616df.png[/img]
如果是单层数据还好办,直接用XMLListCollection简要分析完就可以了,现在涉及到要分组显示,即相同职务的员工并到一组去
即:
设计师组: id employeeName employeeGroup .....
12 张三 设计师组
13 李四 设计师组
14 王二麻子 设计师组
烫染师组:
15 黑娃 烫染师组
16 张山 烫染师组
17 王三麻子 烫染师组
[img]http://dl.iteye.com/upload/attachment/455282/d820a85c-b4a6-3a90-819b-c9b3d38fc117.png[/img]
//项目的最终效果
[img]http://dl.iteye.com/upload/attachment/455971/d6df0858-dd50-3b58-b1c7-a83cb60616df.png[/img]
import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.containers.Panel;
import mx.controls.Tree;
import mx.core.ClassFactory;
import mx.events.ListEvent;
//这里写的是hard data
//实际操作时 从数据库读取数据
private var companyEmployee:Array=[
{employeeGroup:"理疗师组", employeeID:1, employeeName:"张强", employeeRegPlace:"总部店"},
{employeeGroup:"烫染师组", employeeID:11, employeeName:"罗超", employeeRegPlace:"总部店"},
{employeeGroup:"设计师组", employeeID:12, employeeName:"当当", employeeRegPlace:"总部店"},
{employeeGroup:"前台收银", employeeID:3, employeeName:"伟伟", employeeRegPlace:"总部店"},
{employeeGroup:"伙伴员组", employeeID:7, employeeName:"罗兵", employeeRegPlace:"总部店"},
{employeeGroup:"烫染师组", employeeID:8, employeeName:"超超", employeeRegPlace:"城南店"}];
private var company:Array=[
{companyName:"总部店", id:"1"},
{companyName:"城南店", id:"2"}];
private function init():void
{
/****
* 相同组的员工可能不止一人
* 理疗师组------分析数据并归组
* **/
AnalyzeEmployeeGroup();
}
private var AnalyzedACOData:ArrayCollection=new ArrayCollection()
private function AnalyzeEmployeeGroup():void
{
for (var k:int=0; k < companyEmployee.length; k++)
{
var obj:Object=new Object();
var obj2:Object=new Object()
var childrenACO:ArrayCollection=new ArrayCollection();
if (k == 0)
{
obj.employeeGroup=companyEmployee[k].employeeGroup;
childrenACO.addItem(companyEmployee[k])
obj.children=childrenACO;
AnalyzedACOData.addItem(obj)
}
else
{
//判断companyEmployee[k].employeeGroup是否在数组中,如果存在,
//取得其索引,加入children
//否则 新加入数组
var returnNum:Number=findIsDataInArrays(companyEmployee[k].employeeGroup);
//找到有
if (returnNum != -1)
{
//索引值
var idx:Number=findDataInArraysOrder(companyEmployee[k].employeeGroup);
var oldObj:Object=AnalyzedACOData.getItemAt(idx);
ArrayCollection(oldObj.children).addItem(companyEmployee[k])
}
else
{
//没有找到
obj.employeeGroup=companyEmployee[k].employeeGroup;
childrenACO.addItem(companyEmployee[k])
obj.children=childrenACO;
AnalyzedACOData.addItem(obj);
}
}
}
customTreeData()
}
private function findIsDataInArrays(input:String):Number
{
for (var i:int=0; i < AnalyzedACOData.length; i++)
{
if (AnalyzedACOData[i].employeeGroup === input)
{
return i;
}
}
return -1;
}
private function findDataInArraysOrder(input:String):Number
{
for (var i:int=0; i < AnalyzedACOData.length; i++)
{
if (AnalyzedACOData[i].employeeGroup === input)
{
return i;
break;
}
}
return -1;
}
private var dataManageTreeDataXML:XMLListCollection
private function customTreeData():void
{
//tree呈现的数据
dataManageTreeDataXML=new XMLListCollection();
var insertDataXML:XML=<folder label="录入数据"/>;
var viewAdminDataXML:XML=<folder label="查看管理"/>;
//控制菜单一 公司员工
var employeeXML:XML=new XML(<folder label="公司员工"></folder>);
for (var i:int=0; i < AnalyzedACOData.length; i++)
{
var xmlStr:String="<folder label=" + "\"" + AnalyzedACOData[i].employeeGroup + "\"" + "/>";
var tempXML:XML=XML(xmlStr);
employeeXML.appendChild(tempXML)
//取得 Children
var childrenArr:*=(AnalyzedACOData[i].children);
for (var j:int=0; j < childrenArr.length; j++)
{
var infoForEmployeeStr:String="<folder label=" + "\"" + childrenArr[j].employeeName + "\"" + "employeeID=" + "\"" + childrenArr[j].employeeID + "\"" + "/>"
var sub_dataXML:XML=XML(infoForEmployeeStr);
//加入操作菜单
sub_dataXML.appendChild(insertDataXML)
sub_dataXML.appendChild(viewAdminDataXML);
tempXML.appendChild(sub_dataXML)
}
}
dataManageTreeDataXML.addItem(employeeXML);
//控制菜单二
var companyXML:XML=new XML(<folder label="公司各店"></folder>);
for (var k:int=0; k < company.length; k++)
{
var xmlForCompanyInfoStr:String="<folder label=" + "\"" + company[k].companyName + "\"" + "id=" + "\"" + company[k].id + "\"" + "/>"
var tempForCompanyXML:XML=XML(xmlForCompanyInfoStr);
//加入操作菜单
tempForCompanyXML.appendChild(insertDataXML)
tempForCompanyXML.appendChild(viewAdminDataXML);
companyXML.appendChild(tempForCompanyXML)
}
dataManageTreeDataXML.addItem(companyXML);
trace("===" + dataManageTreeDataXML)
createTree()
}
[Embed(source="/assets/plus.gif")]
private var PLUS:Class;
[Embed(source="/assets/minus.gif")]
private var MINUS:Class;
private function createTree():void
{
var panel:Panel=new Panel();
panel.width=150
panel.percentHeight=100;
this.addElement(panel);
var MyTree:Tree=new Tree();
MyTree.horizontalScrollPolicy="auto"
//MyTree.allowChangeHorizontalScroll=true
MyTree.percentHeight=100;
MyTree.percentWidth=100
//MyTree.minWidth=150;
MyTree.labelField="@label";
MyTree.showRoot=true;
MyTree.setStyle("borderAlpha", "0.6")
//MyTree.expandItem(treeData, true);
MyTree.setStyle("lineColor", 0xff00ff);
MyTree.setStyle("folderOpenIcon", null);
MyTree.setStyle("folderClosedIcon", null);
MyTree.setStyle("defaultLeafIcon", null);
MyTree.setStyle("disclosureClosedIcon", PLUS);
MyTree.setStyle("disclosureOpenIcon", MINUS);
MyTree.itemRenderer=new ClassFactory(TreeItemLinesRenderer)
MyTree.allowMultipleSelection=false
MyTree.allowDragSelection=false
MyTree.addEventListener(ListEvent.CHANGE, treeChanged);
panel.addChild(MyTree);
MyTree.dataProvider=dataManageTreeDataXML;
var nodeList:XMLListCollection=MyTree.dataProvider as XMLListCollection;
MyTree.validateNow();
MyTree.expandItem(nodeList[0], true)
}
public function treeChanged(event:ListEvent):void
{
var selectedNode:XML=Tree(event.target).selectedItem as XML;
trace(selectedNode.parent())
trace(selectedNode.@label)
//trace(selectedNode.parent().@id)
//执行analyzeID()
}