XML Tree : JavaScript DHTML examples (example source code) » GUI Components » Tree

JavaScript DHTML
C++
Java Products
Java Articles
JavaScript DHTML Home  »   GUI Components   » [  Tree  ]   
 



XML Tree

Please note that some example is only working under IE or Firefox.

/*
Mastering JavaScript, Premium Edition
by James Jaworski 

ISBN:078212819X
Publisher Sybex CopyRight 2001
*/



<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <title>Tree Component</title>
   <style type="text/css" >
BLOCKQUOTE {
margin-top: -5;
margin-bottom: -5;
}
TABLE {
margin-top: 0;
margin-bottom: 0;
}
A:link, A:visited {
  color: black;
  text-decoration: none;
}   
   </style>
   <script language="JavaScript">
var nodes = new Array("XML, XSLT, and JavaScript Tree Component",
"Features",
"The nodes and structure of the tree are specified in XML.",
"An XSLT stylesheet enables the tree to be rendered in HTML.",
"The display and operation of the tree is handled in JavaScript.",
"Design""The tree component consists of the following files:",
"tree.xml""Defines the tree's text and structure.",
"tree.dtd""Provides a DTD for tree.xml.",
"tree.xsl""Transforms the tree into HTML and JavaScript.",
"tree.js""Defines the Tree object.",
"tree.css""Styles the transformed tree."
)
var branches = new Array(new Array(0,1),new Array(1,2),
new Array(1,3),new Array(1,4),new Array(0,5),new Array(5,6),
new Array(6,7),new Array(7,8),new Array(6,9),new Array(9,10),
new Array(6,11),new Array(11,12),new Array(6,13),new Array(13,14),
new Array(6,15),new Array(15,16),new Array(-1,-1))
</script>
<script language="JavaScript" >
var tree = createTree()
function createTree() {
 branchID = 0
 var subtrees = new Array()
 for(var i=0; i<nodes.length; ++i)
  subtrees[inew Tree(nodes[i])
 for(var i=0; i<branches.length - 1; ++i)
  subtrees[branches[i][0]].addBranch(subtrees[branches[i][1]])
 return subtrees[0]
}
function Tree(root) {
 this.text = root
 this.id = branchID
 ++branchID
 this.expanded = true
 this.branches = new Array()
 this.addBranch = Tree_addBranch
 this.changeState = Tree_changeState
 this.handleClick = Tree_handleClick
 this.processClick = Tree_processClick
 this.display = Tree_display
 this.getTreeString = Tree_getTreeString
}
function Tree_addBranch(tree) {
 this.branches[this.branches.length= tree
}
function Tree_changeState() {
 this.expanded = !this.expanded
}
function Tree_handleClick(branch) {
 this.processClick(branch)
 var d = document.getElementById("tree")
 if(d != nulld.innerHTML = this.getTreeString()
}
function Tree_processClick(branch) {
 if(this.id == branchthis.changeState()
 else {
  for(var i=0; i<this.branches.length; ++i)
   this.branches[i].processClick(branch)
 }
}
function Tree_getTreeString() {
 var s = "<blockquote>"
 s += '<table border="0">'
 s += "<tr>"
 s += "<td>"
 if(this.branches.length > 0)
  s += '<a href="javascript:tree.handleClick('+this.id+')">+</a>'
 else s += "-"
 s += "</td>"
 s += "<td>"
 s += this.text
 s += "</td>"
 s += "</tr>"
 s += "</table>"
 if((this.branches.length > 0&& (this.expanded == true)) {
  for(var i=0; i<this.branches.length; ++i)
   s += this.branches[i].getTreeString()
 }
 s += "</blockquote>"
 return s
}
function Tree_display() {
 document.writeln(this.getTreeString())
}

</script>
</head>
<body>
<hr>
<div id="tree"><script language="JavaScript">
tree.display()
</script>
</div>
<hr>
</body>
</html>
Related examples in the same category
1.  Build a tree in JavaScript
2.  Delete, insert items in a tree
3.  Tree selection action handler
4.  Expand, Collapse, Close, Open selected Tree item and branch
5.  Change Tree Node Color and Icon
6.  Checkbox tree node: checked, unchecked, get the checked items
7.  Change tree expand and collapse icons
8.  Drag and Drop between trees
9.  Build tree from xml
10.  Tree navigation bar
11.  Navigation Tree
12.  Navigation Tree menu based on XML
13.  Building Collapsible Trees
14.  Nano Tree








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.