zgzczzw 2008-09-08
代码如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Reorder TreePanel</title> <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" /> <script type="text/javascript" src="../../adapter/ext/ext-base.js"></script> <script type="text/javascript" src="../../ext-all.js"></script> <script type="text/javascript" src="reorder.js"></script> <!-- Common Styles for the examples --> <link rel="stylesheet" type="text/css" href="../shared/examples.css" /> <link rel="stylesheet" type="text/css" href="../shared/lib.css" /> <script type="text/javascript"> /************** onload事件 ***************/ window.onload=function(){ createTree(3); } </script> </head> <body> <script type="text/javascript" src="../shared/examples.js"></script> <h1>现在要生成一颗动态树</h1> <div id="container"> </div> </body> </html>
代码如下:
/*********************************** 创建树 by chb ************************************/ function createTree(n){ Ext.QuickTips.init(); var mytree=new Ext.tree.TreePanel({ el:"container", animate:true, title:"Extjs动态树", collapsible:true, enableDD:true, enableDrag:true, rootVisible:true, autoScroll:true, autoHeight:true, width:"30%", lines:true }); //根节点 var root=new Ext.tree.TreeNode({ id:"root", text:"集团公司", expanded:true }); for(var i=0;i<n;i++){ var sub1=new Ext.tree.TreeNode({ id:i+1, text:"子公司"+(i+1), singleClickExpand:true, listeners:{ //监听单击事件 "click":function(node){ myExpand(node); }, //监听右键 "contextmenu":function(node,e){ //列出右键菜单 menu=new Ext.menu.Menu([ { text:"打开当前节点", icon:"list.gif", handler:function(){ myExpand(node); } }, { text:"编辑当前节点", icon:"list.gif", handler:function(){ alert(node.id); } }, { text:"删除当前节点", icon:"list.gif", handler:function(){ alert(node.id); } }]); //显示在当前位置 menu.showAt(e.getPoint()); } } }); root.appendChild(sub1); } mytree.setRootNode(root);//设置根节点 mytree.render();//不要忘记render()下,不然不显示哦 }
代码如下:
/****************************************** 展开节点 ******************************************/ function myExpand(node){ if(node.id=='1'){ if(node.item(0)==undefined){ node.appendChild(new Ext.tree.TreeNode({ id:node.id+'1', text:node.text+"的第一个儿子", hrefTarget:"mainFrame", listeners:{//监听 "click":function(node,e){ expand2(node) } } })); } node.expand(); }else if(node.id=='2'){ node.appendChild(new Ext.tree.TreeNode({ id:node.id+'2', text:node.text+"的第一个儿子", hrefTarget:"mainFrame", listeners:{//监听 "click":function(node){ expand2(node) } } })); }else{ alert(node.id+"没有子部门了"); } }
好了,就到这里吧,困了,就不详细解释了o(∩_∩)o...哈哈