首先给
Tree
设置一个数据源:
[Bindable]
private var dataTree:XMLList;
在个当前选中的树节点设置一个变量
private var currentItem:XML;
接着设置与服务器进行连接的HTTPSERVICE 控件
<mx:HTTPService id="treeService" result="ResultHandler(event)" fault="FaultHandler(event)" resultFormat="e4x" url="你自己的服务器端地址">
</mx:HTTPService>
然后在页面上放置一个树控件:
<mx: Tree id="sdbTree" height="100%" width="25%" dataProvider="{dataTree}" showRoot="true" creationComplete="InitSdbTree()" labelField="@label" itemOpening="TreeItemOpening(event)" fontSize="12"></mx: Tree >
InitSdbTree():是对数进行初始化
TreeItemOpening 节点展开时进行触发
private function InitSdbTree():void
{
sdbTree.setStyle("disclosureClosedIcon",Lplus);
sdbTree.setStyle("disclosureOpenIcon",Lminus);
treeService.method="GET";
treeService.request = {"subject":"root"};
var call:AsyncToken = treeService.send();
call.request_params = treeService.request;
}
private function TreeItemOpening(evt:TreeEvent):void
{
if(evt.type == TreeEvent.ITEM_OPENING)
{
var e:TreeEvent = TreeEvent(evt);
currItem = XML(e.item);
if(e.item.@isLoad == "false")
{
e.item.node.@label = "Loading...";
var parameters:*={"subject":e.item.@subject}
if(e.item.@schema != null)
{
parameters['server'] = e.item.@server;
parameters['database'] = e.item.@database;
parameters['schema'] = e.item.@schema;
}
else if(e.item.@database != null)
{
parameters['server']=e.item.@server;
parameters['database'] = e.item.@database;
}
else if(e.item.@server != null)
{
parameters['server']=e.item.@server;
}
treeService.request = parameters;
var call:AsyncToken = treeService.send();
call.request_params = treeService.request;
CursorManager.setBusyCursor();
}
}
}
private function ResultHandler(result:ResultEvent):void
{
if (result.token.request_params['subject']=="root")
{
dataTree = treeService.lastResult.node;
}
else
{
CursorManager.removeBusyCursor();
delete currItem.node[0];
currItem.@isLoad = "true";
var node:XMLList = treeService.lastResult.node;
currItem.node += node;
//currItem.appendChild(node); //注意:如果用这个语句,也能成功,但是数据量大后,速度相当的慢
}
setTimeout(sdbTree.validateNow,100);
}
[Bindable]
private var dataTree:XMLList;
在个当前选中的树节点设置一个变量
private var currentItem:XML;
接着设置与服务器进行连接的HTTPSERVICE 控件
<mx:HTTPService id="treeService" result="ResultHandler(event)" fault="FaultHandler(event)" resultFormat="e4x" url="你自己的服务器端地址">
</mx:HTTPService>
然后在页面上放置一个树控件:
<mx: Tree id="sdbTree" height="100%" width="25%" dataProvider="{dataTree}" showRoot="true" creationComplete="InitSdbTree()" labelField="@label" itemOpening="TreeItemOpening(event)" fontSize="12"></mx: Tree >
InitSdbTree():是对数进行初始化
TreeItemOpening 节点展开时进行触发
private function InitSdbTree():void
{
sdbTree.setStyle("disclosureClosedIcon",Lplus);
sdbTree.setStyle("disclosureOpenIcon",Lminus);
treeService.method="GET";
treeService.request = {"subject":"root"};
var call:AsyncToken = treeService.send();
call.request_params = treeService.request;
}
private function TreeItemOpening(evt:TreeEvent):void
{
if(evt.type == TreeEvent.ITEM_OPENING)
{
var e:TreeEvent = TreeEvent(evt);
currItem = XML(e.item);
if(e.item.@isLoad == "false")
{
e.item.node.@label = "Loading...";
var parameters:*={"subject":e.item.@subject}
if(e.item.@schema != null)
{
parameters['server'] = e.item.@server;
parameters['database'] = e.item.@database;
parameters['schema'] = e.item.@schema;
}
else if(e.item.@database != null)
{
parameters['server']=e.item.@server;
parameters['database'] = e.item.@database;
}
else if(e.item.@server != null)
{
parameters['server']=e.item.@server;
}
treeService.request = parameters;
var call:AsyncToken = treeService.send();
call.request_params = treeService.request;
CursorManager.setBusyCursor();
}
}
}
private function ResultHandler(result:ResultEvent):void
{
if (result.token.request_params['subject']=="root")
{
dataTree = treeService.lastResult.node;
}
else
{
CursorManager.removeBusyCursor();
delete currItem.node[0];
currItem.@isLoad = "true";
var node:XMLList = treeService.lastResult.node;
currItem.node += node;
//currItem.appendChild(node); //注意:如果用这个语句,也能成功,但是数据量大后,速度相当的慢
}
setTimeout(sdbTree.validateNow,100);
}
结果图: