Using ASP script to make the tree structure of asynchronous loading (b) (turn)--Good dongdong!!

Source: Internet
Author: User
Scripting | Asynchronous three, client code

The following is the program's startup page tree.htm:

< HTML >
< SCRIPT src= "renderer.js" language= "JavaScript" ></script >
< head >< body id=bodytree name= "Bodytree" onload= "Gettree ();" >
< IFRAME id=getdata style= "Display:none" ></iframe >
</body >
The Gettree function in Renderer.js is executed when the page is loaded. The IFRAME part of the HTML code implements the communication mechanism between the client and server side. The code for the JavaScript function gettree is as follows:

function Gettree () {
if (Event.type = = ' Load ') {
if (typeof (DivTree0)!= ' object ')
GetData.window.location.href = ' gettreedata.asp? Level=0 ';
} else {
try {
Objmanip = eval (' Divtree ' + event.srcElement.getAttribute (' ElementID '));
if (ObjManip.style.display = = ' None ') {
ObjManip.style.display = ';
} else {
ObjManip.style.display = ' None ';
}
catch (e) {
GetData.window.location.href = ' gettreedata.asp? Level= ' + event.srcElement.getAttribute (' ElementID ');
}
Event.cancelbubble = true;
}
}
When the document is loaded, the OnLoad event is triggered and the Gettree function is executed. The function checks whether the container DIVTREE0 exists and reads the first level node for the IFRAME (ID GetData) (the parent node ID is 0). As mentioned earlier, all nodes must handle mouse click events, and event handles are gettree functions. When a node (such as DIV1) receives a mouse event, the program executes the else part of the Gettree function. If the node that sent the event has read the child nodes, the program checks to see if the child nodes are displayed, and then toggles the display state of the child nodes to achieve the expansion or collapse effect of the layer node. Check if the child node is displayed is encapsulated in a try block, so when the child node does not exist, the program executes the catch part, invoking the server script gettreedata.asp read the child node contents. Finally, the program settings event.cancelbubble = True to prevent the last layer of the container from handling the event.

The server script gettreedata.asp returns an HTML code class such as:

< HTML >
< body onload= "parent. Populatetree (' 1|0| node 1|2|0| node 2|3|0| node 3|4|0| node 4| '); ">
</body >
As you can see, the OnLoad event here calls another JavaScript function Populatetree. The Populatetree function code is as follows:

function Populatetree (strdata) {
var arrsplitdata;
var icnt;
var Objtempdiv;
var Objmaindiv;
 
if (strdata== ') return;
Arrsplitdata = Strdata.split ("|");
 
Objmaindiv = document.createelement (' DIV ');
objmaindiv.id = ' Divtree ' + arrsplitdata[1];
ObjMainDiv.style.cssText = ' position:relative;left:10px;cursor:hand; ';
for (icnt=0;icnt< arrsplitdata.length-1;icnt+=3) {
Objtempdiv = document.createelement (' < DIV onclick= ' Gettree () ' onselectstart= ' return false; ' > ');
objtempdiv.id = ' div ' + arrsplitdata[icnt];
objtempdiv.innerhtml = arrsplitdata[icnt+2];
Objtempdiv.setattribute (' ElementID ', arrsplitdata[icnt]);
Objtempdiv.setattribute (' Parentelementid ', arrsplitdata[icnt+1]);
ObjTempDiv.style.cssText = ' position:relative;cursor:hand;color:red;width:100px;font-size:x-small; ';
Objmaindiv.appendchild (OBJTEMPDIV);
}
if (arrsplitdata[1]== ' 0 ')
Document.body.appendChild (OBJMAINDIV);
Else
Eval (' div ' + arrsplitdata[1]). appendchild (Objmaindiv);
}
We already know that the Populatetree function is called by the OnLoad event, and its argument is a string, such as the "1|0| node 1|2|0| node 3|4|0| node 4|" In the example above, which is a "Node ID | parent Node | node text | ..." The list.

If a node does not contain a child node, the argument is an empty string, at which point Populatetree returns directly. If a child node exists, you can use the Split function to save the list of child nodes as an array. Next, you can create a container for the layer node, such as DivTree0, and then iterate through the array to create each node, such as Div1,div2 .... If a node has a parent node ID of 0, the node does not have a parent node, the program will divTree0 the container into the body of the document, or if the node's parent node ID is not 0, create the container "divtree< < parent Node ID > >" corresponding to its parent node. The handle gettree function of the mouse click event is specified while the node is being created.

Note: You can modify the gettreedata.asp to return a list of child nodes (that is, populatetree arguments) as "Node id| node text | ...", which means that the parent node ID is omitted because any set of child node lists whose parent nodes are always the same. With



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.