Use ext js to generate a dynamic tree

Source: Internet
Author: User

Use ext js to generate a dynamic tree

 

Today, I wrote a demo for my colleagues to use ext js to generate a dynamic tree. Here, I will share it with you for future reference.

I. Requirements

  1. A department tree is required to be generated, and only the root department is listed initially.
  2. When a department node is clicked, It is dynamically loaded into the direct sub-departments under the department, and the department node is expanded.
  3. Department node requirements support right-click events. When right-click an event, related operation menus are listed.

II. Key Categories

Two classes of ext JS are involved:

  • Ext. Tree. treenode
  • Ext. Menu. Menu

Related API can refer to: http://extjs.com/deploy/ext/docs/

Iii. Sample Code

1. Check the test page first.

  1. <HTML>
  2. <Head>
  3. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  4. <Title> reorder treepanel </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. <! -- Common styles for the examples -->
  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 event
  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> Generate a dynamic tree now
  24. <Div id = "Container">
  25. </Div>
  26. </Body>
  27. </Html>

2. Let's look at the function of generating the tree.

  1. /***********************************
  2. Create tree
  3. By CHB
  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 Dynamic Tree ",
  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. // Root Node
  21. VaR root = new Ext. Tree. treenode ({
  22. ID: "root ",
  23. Text: "Group Company ",
  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: "subsidiary" + (I + 1 ),
  30. Singleclickexpand: True,
  31. Listeners :{
  32. // Listen to the Click Event
  33. "Click": function (node ){
  34. Myexpand (node );
  35. },
  36. // Right-click the listener
  37. "Contextmenu": function (node, e ){
  38. // List the right-click menu
  39. Menu = new Ext. Menu. menu ([
  40. {
  41. Text: "Open current node ",
  42. Icon: "list.gif ",
  43. Handler: function (){
  44. Myexpand (node );
  45. }
  46. },
  47. {
  48. Text: "editing current node ",
  49. Icon: "list.gif ",
  50. Handler: function (){
  51. Alert (node. ID );
  52. }
  53. },
  54. {
  55. Text: "deleting the current node ",
  56. Icon: "list.gif ",
  57. Handler: function (){
  58. Alert (node. ID );
  59. }
  60. }]);
  61. // Display at the current position
  62. Menu. showat (E. getpoint ());
  63. }
  64. }
  65. });
  66. Root. appendchild (sub1 );
  67. }
  68. Mytree. setrootnode (Root); // you can specify the root node.
  69. Mytree. Render (); // do not forget to render (), otherwise it will not be displayed.
  70. }

3. Expand the subnode code

  1. /*************************************** ***
  2. Expand a node
  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 + "the first son ",
  10. Hreftarget: "mainframe ",
  11. Listeners: {// listener
  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 + "the first son ",
  23. Hreftarget: "mainframe ",
  24. Listeners: {// listener
  25. "Click": function (node ){
  26. Expand2 (node)
  27. }
  28. }
  29. }));
  30. } Else {
  31. Alert (node. ID + "no sub-door ");
  32. }
  33. }

You can run the above Code on your own and discover the following phenomena: no matter how many times you click "subsidiary 1", only one node of "first son of subsidiary 1" will be listed, every time you click "subsidiary 2", there will be an additional "first son of subsidiary 2" node. Why?

 

Because every click will inspire the myexpand function, and node. Item (0) = undefined is added to "subsidiary 1". Do you understand this?

That is, if there is no sub-department under the department, the sub-department is loaded; otherwise, the sub-department is expanded and not re-loaded.

 

Okay, let's get there. If it's sleepy, I won't explain it in detail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.