Dynamically generate trees and talk about the "visible position"

Source: Internet
Author: User
Technorati labels: Tree, Treeview, spanning tree, dynamic, layered, visible light

Copyright statement: You can reprint it at will.Hyperlink formIndicate the source and author information of the following articles andThis statement

Author: Xixi

Source: http://blog.csdn.net/slowgrace/archive/2008/12/09/3483683.aspx

The so-called"Dynamically generate trees"Means that not all nodes on the tree are added at a time, but only the first several layers (such as three layers) of nodes are generated at a time. The lower-level nodes are generated when the user clicks. This prevents the tree from being too slow, and only the nodes used by the user are generated, saving resources.

1. How many layers of nodes are generated at first?

How many layers of nodes are generated at a time? Layer 3: yourself, your children, and your children's children (grandchildren ). In this way, the +/-number in front of the child node of the current node can be correctly displayed, so the user can clearly see whether there are several children on the current node and whether the children have children, this is the same effect as generating the complete tree. Or,Make sure that the addition and subtraction numbers of each displayed node are correctly displayed.The child nodes of all displayed nodes must be generated in advance.

2. When will the remaining nodes be generated?

When a user clicks a node that does not generate a grandson node.

3. How can I determine whether a node's grandson node does not exist?

When generating a tree node, add a flag to the key of the node to indicate whether its son node has been generated. When you click a node to cause an expand event, read the child node flag of the node in the event processing code. If the child that identifies the child node has not been generated, it indicates that the current node's grandson node does not exist.

4. Can you elaborate on this flag?

In this article, we mentioned the design of tags for Tree nodes. This flag is set and maintained by the program. This flag is Y for each node that has generated a child node, in fact, all nodes marked with "Y" are also "Visible Light" nodes. The so-called"Shiguang"Refers to the nodes that have been viewed by users (including those that have been viewed and hidden by clicking the fold button). Since these nodes have been seen, you must ensure that their addition and subtraction numbers are correctly displayed, therefore, make sure that all of its sons are generated. Therefore, "seeing light" is equivalent to "having children. We may call this flag as"Visible position"(Or"Birth mark"OK, hey ).

5. How do I generate only three layers at first when a node is generated? In this article, is the tree generated recursive?

Add a number of layers to the parameter of the recursive call. When the number of layers is reduced by 1 and 0, the recursive call is exited.

6. This method is good. How can I provide the sample code?

I have read it. Main Code of the Spanning Tree:

Private sub treepopulation () <br/> ...... <Br/> set objnewnode = m_tree.nodes.add (image: = 1) <br/> objnewnode. Expanded = true <br/> call addchildtree (objnewnode, 2) <br/> ...... <Br/> end sub

Recursive functions:

Private sub addchildtree (byval nodefather as mscomctllib. node, bytlevel as byte) <br/> 'populate the whole child tree of the node father. <br/> ...... <Br/> If bytlevel = 0 Then exit sub </P> <p> 'select the first level child node of node father from node table. <br/> strsql = "select * from [" & m_strtreetable & "] Where [lngfatherid] =" <br/> strsql = strsql & node2id (nodefather) <br/> strsql = strsql & "order by [strnodename];" <br/> set rsttree = new ADODB. recordset <br/> ...... </P> <p> 'iterate through the selected recordset and add first level Child Nodes of the father node. <br/> do until rsttree. EOF <br/> set objnewnode = m_tree.nodes.add (relative: = nodefather. key, relationship: = tvwchild, image: = 2) <br/> objnewnode. TEXT = rsttree! [Strnodename] <br/> ...... </P> <p> 'add all the child nodes of this particle first-level tree node. <br/> call addchildtree (objnewnode, bytlevel-1) <br/> 'move to the next first-level node record <br/> rsttree. movenext <br/> loop <br/> ...... <Br/> end sub

7. You have set a flag.

In the Code omitted above, when bytlevel is greater than 1, the flag is set to Y; otherwise (that is, when it is equal to 1, the recursion ends when it is less than 1 ), the flag is set to n. the node generated in this recursion will no longer generate its son node. Of course, the flag of a node that does not have children is N.

8. dynamically generate the remaining nodes and give them code.

This work is done in the expand event Process

Private sub m_tree_expand (byval node as mscomctllib. node) <br/> ...... <Br/> set nodechd = node. child <br/> 'if no child node or child node has been generated, you do not need to repeat it. <br/> If nodechd is nothing or isnodeexposed (nodechd) then exit sub </P> <p> ...... <Br/> call setnodeexposedflag (nodechd, "Y") <br/> call addchildtree (nodechd, 1) <br/> DO <br/> set nodechd = nodechd. next <br/> If nodechd is nothing then exit do <br/> call setnodeexposedflag (nodechd, "Y") <br/> call addchildtree (nodechd, 1) <br/> loop <br/> end sub

The above isnodeexposed function is used to determine whether the flag of this node is Y; The setnodeexposedflag node sets the corresponding flag to y. Because these nodes all have children, set them to Y. Because only one child layer is generated and no grandson is born, the number of calls is 1.

More tree articles

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.