日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

利用Ext Js生成動態(tài)樹

系統(tǒng) 2357 0

利用Ext Js生成動態(tài)樹

今天在公司幫同事寫了個用Ext Js生成動態(tài)樹的Demo,在這里分享一下,也好供以后自己查閱。

一. 需求

  1. 要求生成一顆部門樹,初始只列出根部門
  2. 當點擊一個部門節(jié)點時,動態(tài)載入該部門下的直屬子部門,并展開該部門節(jié)點
  3. 部門節(jié)點要求支持右鍵單擊事件,當點擊右鍵時,列出相關(guān)操作菜單

二. 關(guān)鍵類

這里主要涉及Ext JS的兩個類:

  • Ext.tree.TreeNode
  • Ext.menu.Menu

相關(guān)API可以參考: http://extjs.com/deploy/ext/docs/

三. 代碼示例

1. 先看一下測試頁面

  1. < html >
  2. < head >
  3. < meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" >
  4. < title > ReorderTreePanel </ title >
  5. < link rel = "stylesheet" type = "text/css" href = "../../resources/css/ext-all.css" />
  6. < script type = "text/javascript" src = "../../adapter/ext/ext-base.js" > </ script >
  7. < script type = "text/javascript" src = "../../ext-all.js" > </ script >
  8. < script type = "text/javascript" src = "reorder.js" > </ script >
  9. <!--CommonStylesfortheexamples-->
  10. < link rel = "stylesheet" type = "text/css" href = "../shared/examples.css" />
  11. < link rel = "stylesheet" type = "text/css" href = "../shared/lib.css" />
  12. < script type = "text/javascript" >
  13. /**************
  14. onload事件
  15. ***************/
  16. window.onload = function (){
  17. createTree(3);
  18. }
  19. </ script >
  20. </ head >
  21. < body >
  22. < script type = "text/javascript" src = "../shared/examples.js" > </ script >
  23. < h1 > 現(xiàn)在要生成一顆動態(tài)樹 </ h1 >
  24. < div id = "container" >
  25. </ div >
  26. </ body >
  27. </ html >

2. 再看一下生成樹的函數(shù)

  1. /***********************************
  2. 創(chuàng)建樹
  3. bychb
  4. ************************************/
  5. function createTree(n){
  6. Ext.QuickTips.init();
  7. var mytree= new Ext.tree.TreePanel({
  8. el: "container" ,
  9. animate: true ,
  10. title: "Extjs動態(tài)樹" ,
  11. collapsible: true ,
  12. enableDD: true ,
  13. enableDrag: true ,
  14. rootVisible: true ,
  15. autoScroll: true ,
  16. autoHeight: true ,
  17. width: "30%" ,
  18. lines: true
  19. });
  20. //根節(jié)點
  21. var root= new Ext.tree.TreeNode({
  22. id: "root" ,
  23. text: "集團公司" ,
  24. expanded: true
  25. });
  26. for ( var i=0;i<n;i++){
  27. var sub1= new Ext.tree.TreeNode({
  28. id:i+1,
  29. text: "子公司" +(i+1),
  30. singleClickExpand: true ,
  31. listeners:{
  32. //監(jiān)聽單擊事件
  33. "click" : function (node){
  34. myExpand(node);
  35. },
  36. //監(jiān)聽右鍵
  37. "contextmenu" : function (node,e){
  38. //列出右鍵菜單
  39. menu= new Ext.menu.Menu([
  40. {
  41. text: "打開當前節(jié)點" ,
  42. icon: "list.gif" ,
  43. handler: function (){
  44. myExpand(node);
  45. }
  46. },
  47. {
  48. text: "編輯當前節(jié)點" ,
  49. icon: "list.gif" ,
  50. handler: function (){
  51. alert(node.id);
  52. }
  53. },
  54. {
  55. text: "刪除當前節(jié)點" ,
  56. icon: "list.gif" ,
  57. handler: function (){
  58. alert(node.id);
  59. }
  60. }]);
  61. //顯示在當前位置
  62. menu.showAt(e.getPoint());
  63. }
  64. }
  65. });
  66. root.appendChild(sub1);
  67. }
  68. mytree.setRootNode(root); //設置根節(jié)點
  69. mytree.render(); //不要忘記render()下,不然不顯示哦
  70. }

3. 展開子節(jié)點的代碼

  1. /******************************************
  2. 展開節(jié)點
  3. ******************************************/
  4. function myExpand(node){
  5. if (node.id== '1' ){
  6. if (node.item(0)==undefined){
  7. node.appendChild( new Ext.tree.TreeNode({
  8. id:node.id+ '1' ,
  9. text:node.text+ "的第一個兒子" ,
  10. hrefTarget: "mainFrame" ,
  11. listeners:{ //監(jiān)聽
  12. "click" : function (node,e){
  13. expand2(node)
  14. }
  15. }
  16. }));
  17. }
  18. node.expand();
  19. } else if (node.id== '2' ){
  20. node.appendChild( new Ext.tree.TreeNode({
  21. id:node.id+ '2' ,
  22. text:node.text+ "的第一個兒子" ,
  23. hrefTarget: "mainFrame" ,
  24. listeners:{ //監(jiān)聽
  25. "click" : function (node){
  26. expand2(node)
  27. }
  28. }
  29. }));
  30. } else {
  31. alert(node.id+ "沒有子部門了" );
  32. }
  33. }

讀者可以自己運行一下如上代碼,會發(fā)現(xiàn)如下現(xiàn)象:無論點擊“子公司1”多少次,只會列出“子公司1的第一個兒子”一個節(jié)點,而每點擊一次“子公司2”,就會多出一個“子公司2的第一個兒子”節(jié)點,這是為什么呢?

因為每次點擊都會激發(fā)myExpand函數(shù),而“子公司1”上加了node.item(0)==undefined的判斷,這里明白了?

即:如果該部門下沒有子部門,則載入子部門,否則只展開,不重新載入。

截圖

好了,就到這里吧,困了,就不詳細解釋了o(∩_∩)o...哈哈

利用Ext Js生成動態(tài)樹


更多文章、技術(shù)交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 孟州市| 凤冈县| 高台县| 定兴县| 南岸区| 渝中区| 榆中县| 称多县| 新宁县| 永康市| 九龙城区| 鲁甸县| 宜昌市| 饶阳县| 苍山县| 论坛| 竹溪县| 吉木萨尔县| 新密市| 陇南市| 博罗县| 台北县| 游戏| 满城县| 黑山县| 青浦区| 武义县| 突泉县| 长治市| 卢龙县| 舟曲县| 嘉黎县| 内江市| 东明县| 阿荣旗| 工布江达县| 聂荣县| 泰州市| 康马县| 辽阳县| 肥城市|