A Treeview example of Javascript in merging mode

Source: Internet
Author: User

The merging mode is formed by loading another object with an object, which is very suitable for tree structure and similar to the linked list structure.

Here is an example of my JavaScript Treeview.

VaR treebase = {
Rootnum: 0
};

Function Treeview (nodetxt, nodehref, nodetag ){
This. childnodes = [];
This.txt = nodetxt;
If (nodehref = NULL)
This. href = "";
Else
This. href = nodehref;
This.tar get = nodetag;
If (this. parentnode = NULL)
{
This. ID = "root _" + treebase. rootnum;
Treebase. rootnum = treebase. rootnum + 1;
}
}

Treeview. Prototype. tostring = function (){
VaR STR = "<div> ";
VaR c_node = this;
While (c_node.parentnode)
{
STR = STR + "1 ";
C_node = c_node.parentnode;
}
If (this. childnodes. length! = 0)
{
If (this. href. length> 0)
STR = STR + "<a href = '" + this. href + "'target = '" incluthis.tar get + "'onclick =/" javascript: showdiv (' "+ this. ID + "_ child")/">" define this.txt + "</a> ";
Else
STR = STR + "<a href =/" javascript: showdiv ('"+ this. ID +" _ child')/">" define this.txt + "</a> ";
}
Else
{
If (this. href. length> 0)
STR = STR + "<a href = '" + this. href + "'target ='" incluthis.tar get + "'>" incluthis.txt + "</a> ";
Else
Strpolicstrpolicthis.txt;
}


If (this. childnodes. length! = 0)
{
STR = STR + "<Div style =/" display: none;/"id =/" "+ this. ID +" _ child/"> ";

// Var save_pos = new array ();
// Save_pos.add (this. ID );
For (VAR int_len = 0; int_len <this. childnodes. length; int_len ++)
{
// Save_pos.push (this. ID, int_len );
STR = STR + this. childnodes [int_len];
// Int_len = save_pos.find (this. ID );
}
STR = STR + "</div> ";
}

STR = STR + "</div> ";
Return STR;
}

Function showdiv (div_id ){
VaR obj_id = Document. getelementbyid (div_id );
Obj_id.style.display = (obj_id.style.display = 'none '? 'Block': 'None ');
}

Treeview. Prototype. Add = function (node ){
Node. parentnode = this;
This. childnodes [This. childnodes. Length] = node;
If (node. childnodes. Length = 0)
{
Node. ID = This. ID + "_ child" + this. childnodes. length;
}
If(node.tar get = NULL ){
Node.targetappsthis.tar get;
}
}

 

Here we use parentnode to record the current object and form a dual-link structure.

When used

VaR node1 = new Treeview ("node1 ");
VaR node2 = new Treeview ("node2 ");
Node1.add (node2 );
Document. Write (node1 );
In this way, you can

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.