Flex tree基于数据库的数据源
2009-09-21 00:00:00 来源:WEB开发网最近在研究flex,关于flex tree基于数据库数据的网上的例子基本没有,大部分都是基于xml的对xml的操作实现tree的改变,通过改变数据库数据实现tree的改变例子没有找到,所以分享给大家一个例子:
我是用hessian实现flex端与java端通讯的
1.flex端代码
Java代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
verticalAlign="middle"
backgroundColor="white" creationComplete="init();">
<mx:Script>
<![CDATA[
import hessian.service.FlexTestService;
import mx.collections.ICollectionView;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.Tree;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
import mx.managers.PopUpManager;
var noteId:String;
//初始化从数据库中获得tree
private function init():void {
getTree();
}
//监听tree item的click事件
private function tree_itemClick(evt:ListEvent):void {
var item:Object = Tree(evt.currentTarget).selectedItem;
if (tree.dataDescriptor.isBranch(item)) {
tree.expandItem(item, !tree.isItemOpen(item), true);
}
}
//tree的item有子节点的显示(子节点个数)
private function tree_labelFunc(item:XML):String {
var children:ICollectionView;
var suffix:String = "";
if (tree.dataDescriptor.isBranch(item)) {
children = tree.dataDescriptor.getChildren(item);
suffix = " (" + children.length + ")";
}
return item[tree.labelField] + suffix;
}
[Bindable]
public var selectedNode:XML;
//当tree发生change事件时的事件
public function treeChanged(event:Event):void {
//将选中的节点转换成xml,赋予selectedNode,as为转换类型
selectedNode = Tree(event.target).selectedItem as XML;
noteId = selectedNode.@sid;
}
//获得tree的方法
public function getTree():void {
//构造service时指定回调方法名称
var service:FlexTestService = new FlexTestService(result);
service.getTree();
}
var returnString:String;
var treeDate:XML
//回调方法
public function result(data:Object):void {
//java端返回的数据转化成String类型
returnString = data as String;
//利用returnString构造一个xml
treeDate = new XML(returnString);
//把构造的xml文件绑定到tree上
tree.dataProvider = treeDate;
}
//删除某一节点
public function delNote():void {
var service:FlexTestService = new FlexTestService(result);
service.delNote(noteId);
}
//增加一个节点
public function addNote():void {
var service:FlexTestService = new FlexTestService(result);
service.addNote(noteId, nodeName.text);
}
//重命名节点
public function updateNote():void {
var service:FlexTestService = new FlexTestService(result);
service.updateNote(noteId, nodeName.text);
}
]]>
</mx:Script>
<mx:Tree id="tree"
showRoot="false"
labelField="@label"
labelFunction="tree_labelFunc"
width="258"
rowCount="6"
itemClick="tree_itemClick(event);" x="301" y="205" height="208" change="treeChanged(event);"/>
<mx:Button width="98" height="31" click="addNote();" x="374" y="440" label="添加" fontSize="16"/>
<mx:Button width="98" height="31" click="updateNote();" x="374" y="482" label="修改" fontSize="16"/>
<mx:Button width="98" height="31" click="delNote();" x="374" y="521" label="删除" fontSize="16"/>
<mx:TextArea text="您选择的节点: {selectedNode.@label}" x="576" y="207" height="206" width="261"/>
<mx:Text x="576" y="482" text="节点名称" fontSize="14" fontWeight="bold"/>
<mx:TextInput id="nodeName" x="660" y="479" fontSize="16"/>
</mx:Application>
更多精彩
赞助商链接