Simple tree-shaped menu

Source: Internet
Author: User
Menus <!---
Simple tree-shaped menu

Xu Zuning (nagging)
2003.03
czjsz_ah@stats.gov.cn

A tree menu is a little more complicated than a sliding menu. The main difficulty is to create an easily manipulated HTML structure from a concise data description.
This example is used to show the writing of a tree menu. Using a wireless table, the algorithm uses recursion and theoretically constructs an infinitely branched tree.
This code is free to spread.
--->
<style>
Table {font-size = 9pt}
td {Height = 10px}
</style>
<body>
<span id= "Menus" ></span>
<span id= "View" ></span>
</body>

<script>
/**
* Construct tree with initial value of 0
*/
function tree (n) {
var id = new Array ("bar", "pad", "#", "+");
if (n = = 0) {//Initialize variable
n = 1;
i = 0;
s = "";
}
S + + "<table>";
for (; i<tree_ar.length-1;i++) {
var k = (n >= tree_ar[i+1][0])? 0:1;
S + + "<tr id= '" +id[k]+ "' value=" +i+ "><td>" +id[k+2]+ "</td><td>" +tree_ar[i][1]+ "</td> </tr> "; Construct the node, note the custom attribute value here. The function is to simplify the description of the construction node and share the parameter array information.
if (n > tree_ar[i+1][0]) {//If the expected level is greater than the current level, end this level to return to the previous level.
S + + "</td></tr></table>";
return tree_ar[i+1][0];
}
if (n < tree_ar[i+1][0]) {//If the expected level is less than the current level, recursion goes to the next level.
S + + "<tr style= ' Display:none ' v=1><td></td><td>";
var m = tree (tree_ar[++i][0]);
S + + "</td></tr>";
if (M < n) {//when the recursive return value is less than the current level expectation, a sequential return action is generated.
S + + "</table>";
return m;
}
}
}
S + + "</table>";
return s;
}
</script>

<script For=pad event=onclick>
Click response for split points
v = this.parentelement.rows[this.rowindex+1].style;
if (V.display = = ' block ') {
V.display = ' None ';
this.cells[0].innerhtml = "+";
view.innerhtml = ""; Self-modifying a closed action defined as a parameter array
}else {
V.display = ' block ';
this.cells[0].innerhtml = "-";
view.innerhtml = "<b>" +tree_ar[this.value][1]+ "</b>"; Self-modifying an unwind action defined for a parameter array
}

/**
* The following code is used to close other branched branches that have been expanded
* To return or delete this code directly from here if the branch is required to close itself
*/
if (! Tree_ar[this.value].type)//If the node is first entered, record the level information
Gentreeinfo (this);
var n = 1*this.value+1;
for (i=n;i<tree_ar.length-1;i++) {//close the tree that is arranged after the current node
if (Tree_ar[i].type = = "Pad") {
Tree_ar[i].obj2.style.display = ' None ';
tree_ar[i].obj1.cells[0].innerhtml = "+";
}
}
while (Tree_ar[--n][0] > 1); Backtrack to the beginning of the current tree
while (--n >= 0)//close the tree before the beginning of the current tree
if (Tree_ar[n].type = = "Pad") {
Tree_ar[n].obj2.style.display = ' None ';
tree_ar[n].obj1.cells[0].innerhtml = "+";
}

/** Records hierarchy information to simplify the complex node description when traversing a tree **/
function Gentreeinfo (o) {
var el = o.parentelement;
for (Var i=0;i<el.rows.length;i++) {
if (el.rows[i].id!= "") {
Tree_ar[el.rows[i].value].type = el.rows[i].id;
}
if (el.rows[i].id = = "Pad") {
Tree_ar[el.rows[i].value].obj1 = El.rows[i];
Tree_ar[el.rows[i].value].obj2 = el.rows[i+1];
}
}
}
</script>

<script For=bar event=onclick>
Click-Response without branching points
view.innerhtml = "<b>" +tree_ar[this.value][1]+ "</b>"; Self-Modifying the click action defined for the parameter array
</script>

<script>
/**
* The basic parameter array, expands according to the concrete application
* Data is simpler to be provided by the server side
* Column 1: node hierarchy
* Column 2: node title
* Other self-expansion
*/
Tree_ar = new Array (
New Array (1, "Node 1"),
New Array (1, "Node 2"),
New Array (2, "Node 3"),
New Array (2, "Node 4"),
New Array (3, "Node 5"),
New Array (4, "Node 6"),
New Array (5, "Node 7"),
New Array (6, "Node 8"),
New Array (7, "Node 9"),
New Array (2, "Node 10"),
New Array (1, "Node 11"),
New Array (2, "Node 12"),
New Array (2, "Node 13"),
New Array (1, "Node 14"),
New Array (1, "")//additional empty data items for simplified termination judgment
);

/*** Create Menu ***/
Menus.innerhtml =tree (0);
</script>



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.