Recently in the study of Flex, on the network of Flex tree based on database data is basically not, most of the XML based on the operation of the tree to implement the change, by changing the database data to achieve tree changes are not found, so share an example:
I am using Hessian to implement the flex-side and Java-side communication
1.flex-Port Code
Java code
<?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;
Initialization obtains tree from database
Private Function init (): void {
Gettree ();
}
Monitor the Click event of the tree item
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's item has child node display (number of child nodes)
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;
event when tree changes event
Public Function treechanged (event:event): void {
Converts the selected node to XML, giving Selectednode,as the conversion type
SelectedNode = Tree (event.target). SelectedItem as XML;
Noteid = SelectedNode. @sid;
}
The way to get a tree
Public Function Gettree (): void {
Specify callback method name when constructing service
var service:flextestservice = new Flextestservice (result);
Service.gettree ();
}
var returnstring:string;
var treedate:xml
callback method
Public function result (Data:object): void {
The data returned from the Java end is converted to a string type
returnstring = data as String;
Using returnstring to construct an XML
Treedate = new XML (returnstring);
Bind the constructed XML file to the tree
Tree.dataprovider = treedate;
}
Delete a node
Public Function delnote (): void {
var service:flextestservice = new Flextestservice (result);
Service.delnote (Noteid);
}
Add a Node
Public Function addnote (): void {
var service:flextestservice = new Flextestservice (result);
Service.addnote (Noteid, Nodename.text);
}
Renaming nodes
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=", "y=" 205 "height=", "change=" treechanged (event); />
<mx:button width= "height=" "click=" Addnote (); "x=" 374 "y=" 440 "label=" Add "fontsize="/>
<mx:button width= "height=" click= "Updatenote" x= "374" y= "482" label= "Modify" fontsize= "/>"
<mx:button width= "height=" click= "Delnote" x= "374" y= "521" label= "delete" fontsize= "/>"
<mx:textarea text= "The node you selected: {SelectedNode. @label}" x= "576" y= "207" height= "206" width= "261"/> "
<mx:text x= "576" y= "482" text= "node name" fontsize= "fontweight=" "bold"/>
<mx:textinput id= "nodename" x= "660" y= "479" fontsize= ""/>
</mx:Application>