以前在网上找代码,找到的总是不合用,今天我整理一份Flex Tree 通过Arraycollection转化为XML数据源,新增、删除、修改功能的整合,有需要的拿过去,转载请注明出处,不瞎扯了,上图上代码。。。
调用代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application backgroundColor="white" creationComplete="resetData();" fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.utils.ArrayUtil;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.collections.XMLListCollection;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
var xml:XML=new XML();
[Bindable]
var arrayString:String='[{id:"1",name:"root",parentId:"-1"},\n'+
'{id:"2",name:"node1-1",parentId:"1"},\n'+
'{id:"3",name:"node2-1",parentId:"2"},\n'+
'{id:"4",name:"node2-2",parentId:"2"},\n'+
'{id:"5",name:"node4-1",parentId:"4"},\n'+
'{id:"6",name:"node1-2",parentId:"1"},\n'+
'{id:"7",name:"node5-1",parentId:"5"},\n'+
'{id:"7",name:"node3-1",parentId:"3"}]';
var array:ArrayCollection=new ArrayCollection([
{id:"1",name:"root",parentId:"-1"},
{id:"2",name:"node1-1",parentId:"1"},
{id:"3",name:"node2-1",parentId:"2"},
{id:"4",name:"node2-2",parentId:"2"},
{id:"5",name:"node4-1",parentId:"4"},
{id:"6",name:"node1-2",parentId:"1"},
{id:"7",name:"node5-1",parentId:"5"},
{id:"7",name:"node3-1",parentId:"3"}
]);
//显示树(转换数据)
private function resetData():void{
xml=new Array2XmlTree().getTreeData(array,"id","parentId");
}
//添加当前节点之后的兄弟节点
private function addAfterNode():void{
var selectedItemXML:XML=this.tree.selectedItem as XML;
if(null!=selectedItemXML){
var afterId:Number=new Date().getMilliseconds();
var sparentId:String=selectedItemXML.parent().@id;
var afterNode={id:afterId.toString(),name:testName.text,parentId:sparentId}
new Array2XmlTree().addAfterNode(afterNode,selectedItemXML);
changedData.text=xml.toXMLString();
}
}
//添加当前节点之前的兄弟节点
private function addBeforeNode():void{
var selectedItemXML:XML=this.tree.selectedItem as XML;
if(null!=selectedItemXML){
var beforeId:Number=new Date().getMilliseconds();
var sparentId:String=selectedItemXML.parent().@id;
var beforeNode={id:beforeId.toString(),name:testName.text,parentId:sparentId}
new Array2XmlTree().addBeforeNode(beforeNode,selectedItemXML);
changedData.text=xml.toXMLString();
}
}
//添加子节点
private function addChildNode():void{
var selectedItemXML:XML=this.tree.selectedItem as XML;
if(null!=selectedItemXML){
var childId:Number=new Date().getMilliseconds();
var sparentId:String=selectedItemXML.@id;
var childNode={id:childId.toString(),name:testName.text,parentId:sparentId}
new Array2XmlTree().addChildNode(childNode,selectedItemXML,tree);
changedData.text=xml.toXMLString();
}
}
//重命名节点
private function renameNode():void{
var selectedItemXML:XML=this.tree.selectedItem as XML;
if(null!=selectedItemXML){
new Array2XmlTree().renameNode(selectedItemXML,"name",testName.text);
changedData.text=xml.toXMLString();
}
}
//删除节点
private function delNode():void{
var selectedItemXML:XML=this.tree.selectedItem as XML;
if(null!=selectedItemXML){
new Array2XmlTree().delNode(selectedItemXML);
changedData.text=xml.toXMLString();
}
}
]]>
</mx:Script>
<mx:Button label="resetData" click="resetData();" x="0" y="479"/>
<mx:Button label="addAfterNode" click="addAfterNode();" x="414" y="479"/>
<mx:Button label="addChildNode" click="addChildNode();" x="569" y="479"/>
<mx:Button label="addBeforeNode" click="addBeforeNode();" x="256" y="479"/>
<mx:Button label="renameNode" click="renameNode();" x="719" y="479"/>
<mx:Button label="delNode" click="delNode();" x="867" y="479"/>
<mx:Tree id="tree" width="250" labelField="@name" dataProvider="{xml}" y="30" height="432" x="0"/>
<mx:TextArea editable="false" x="276" text="{arrayString}" y="30" width="362" height="432"/>
<mx:TextArea id="changedData" editable="false" x="674" text="{xml.toXMLString()}" y="30" width="368" height="432"/>
<mx:Label x="276" y="2" text="初始化arraycollection数据:" width="183"/>
<mx:Label x="10" y="2" text="树:" width="183"/>
<mx:Label x="674" y="2" text="转换XML后的数据:" width="183"/>
<mx:TextInput text="测试节点" id="testName" x="80" y="479"/>
</mx:Application>
工具类代码:
/**
* ArrayCollection转换树结构数据、添加、删除、重命名节点
* Date:2012-04-07
* Author:Tangdl
*
* 数据转换使用方法:
* treeData=new Array2Tree().getTreeData(array,"id","parentId","-9999");
* array:数据库查询数据ArrayCollection
* id:数据库查询字段标识
* parentId:数据库查询字段父亲标识
* -9999:根节点父亲标识,默认为-1
*
* 添加兄弟节点(之后)使用方法:
* new Array2XmlTree().addAfterNode(afterNode,selectedItemXML);
* afterNode:添加的节点对象
* selectedItemXML:当前选中节点的XML
*
* 添加兄弟节点(之前)使用方法:
* new Array2XmlTree().addBeforeNode(beforeNode,selectedItemXML);
* beforeNode:添加的节点对象
* selectedItemXML:当前选中节点的XML
*
* 添加子节点使用方法:
* new Array2XmlTree().addChildNode(childNode,selectedItemXML,tree);
* childNode:添加的节点对象
* selectedItemXML:当前选中节点的XML
* tree:树对象
*
* 重命名节点使用方法:
* new Array2XmlTree().renameNode(selectedItemXML,"name",testName.text);
* selectedItemXML:当前选中节点的XML
* "name":需要重名名的字段名称
* testName.text:新名称字符串
*
*
* 删除节点使用方法:
* new Array2XmlTree().delNode(selectedItemXML);
* selectedItemXML:当前选中节点的XML
* */
package{
import mx.collections.ArrayCollection;
import mx.controls.Tree;
public class Array2XmlTree {
/**
* 对比标识(parentNodeIdName,nodeIdName)
* 原始数据
* 根节点标识
*/
private var parentNodeIdName, nodeIdName, rootFlag:String;
private var assembleArray:ArrayCollection=new ArrayCollection();
/**
* 返回树形结构数据类型
*/
public function getTreeData(sourceArray:ArrayCollection, nodeIdName:String="NODE_ID", parentNodeIdName:String="PARENT_NODE_ID", rootFlag:String="-1"):XML {
//初始化数据
this.parentNodeIdName=parentNodeIdName;
this.nodeIdName=nodeIdName;
this.assembleArray=sourceArray;
this.rootFlag=rootFlag;
//拼装树数据
var treeDataArray:ArrayCollection=new ArrayCollection();
var treeXml:XML=assembleTree();
return treeXml;
}
/**
* 递归拼装Flex树需要的数据类型
*/
public function assembleTree(nodeObject:Object=null):XML {
//查找根节点
if (nodeObject == null) {
for each (var tmplNodeObject:Object in assembleArray) {
if (tmplNodeObject[parentNodeIdName].toString() == rootFlag) {
nodeObject=tmplNodeObject;
break;
}
}
}
//创建xml对象
var treeXml:XML=new XML("<node></node>");
for(var objectKey:String in nodeObject){
treeXml["@"+objectKey]=nodeObject[objectKey];
}
//查找子节点
if (hasChild(nodeObject)) {
for each (var childObject:Object in getChildList(nodeObject)) {
treeXml.appendChild(assembleTree(childObject));
}
}
return treeXml;
}
/**
* 判断是否有子节点
*/
private function hasChild(nodeObject:Object):Boolean {
return getChildList(nodeObject).length > 0 ? true : false;
}
/**
* 得到子节点列表
*/
private function getChildList(nodeObject:Object):ArrayCollection {
var childArray:ArrayCollection=new ArrayCollection();
for each (var childObject:Object in assembleArray) {
if (childObject[parentNodeIdName].toString() == nodeObject[nodeIdName].toString()) {
childArray.addItem(childObject);
}
}
return childArray;
}
/**
* 添加兄弟节点(之后)
* */
public function addAfterNode(afterNodeObject:Object,selectedNodeXml:XML):void{
//获取当前选择对象的父亲对象并转化为XML
var parentNodeXml:XML=selectedNodeXml.parent();
//新增节点
var treeXml:XML=new XML("<node></node>");
for(var objectKey:Object in afterNodeObject){
treeXml["@"+objectKey]=afterNodeObject[objectKey];
}
//在当前节点之后添加兄弟节点
parentNodeXml.insertChildAfter(selectedNodeXml,treeXml);
}
/**
* 添加兄弟节点(之前)
* */
public function addBeforeNode(beforeNodeObject:Object,selectedNodeXml:XML):void{
//获取当前选择对象的父亲对象并转化为XML
var parentNodeXml:XML=selectedNodeXml.parent();
//新增节点
var treeXml:XML=new XML("<node></node>");
for(var objectKey:Object in beforeNodeObject){
treeXml["@"+objectKey]=beforeNodeObject[objectKey];
}
//在当前节点之前添加兄弟节点
parentNodeXml.insertChildBefore(selectedNodeXml,treeXml);
}
/**
* 添加子节点
* */
public function addChildNode(childNodeObject:Object,selectedNodeXml:XML,currentTree:Tree,isExpand:Boolean=true):void{
//新增节点
var treeXml:XML=new XML("<node></node>");
for(var objectKey:Object in childNodeObject){
treeXml["@"+objectKey]=childNodeObject[objectKey];
}
//在当前节点添加子节点
selectedNodeXml.appendChild(treeXml);
// 添加后是否展开
if(isExpand){
currentTree.expandChildrenOf(selectedNodeXml,true);
}
}
/**
* 重命名节点
* */
public function renameNode(selectedNodeXml:XML,renameField:String,renameString:String):void{
selectedNodeXml["@"+renameField]=renameString;
}
/**
*删除节点
* */
public function delNode(selectedNodeXml:XML):void{
if(selectedNodeXml!=null) {
var parent:XML=selectedNodeXml.parent();
if(parent!=null) {
var children:XMLList=parent.children();
for(var i:int=0;i<children.length();i++) {
if(children[i]==selectedNodeXml) {
delete children[i];
break;
}
}
}
}
}
}
}
上图,上附件。。。。。
搜索功能正在整理中,ing。。。。。
有问题请留言,如有改进也请留言,谢谢,哈哈。。。。