aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes can anyone help in this javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "can anyone help in this javascript?" Watch "can anyone help in this javascript?" New topic
Author

can anyone help in this javascript?

Akhil Jain
Ranch Hand

Joined: Oct 14, 2004
Posts: 74
Hi friends,
Sorry for the long POsting but i am stuck somewhere....please help me out in this.....

I am creating a tree like structure by creating trees wth the values of the beans i have.

i have RootBeans and ChildBeans,and RootBeans can contain ChildBean also.
now the problem is that there can be any number of RootBeans havng dfferent Structure...means one RootBean can have one ChildBean instance and two LeafNodes and another can have Two ChildBean Instances and one Leaf Node.

i am trying to implement this thing but not gettng any solution for it..
if you can help me out it would be of great help from your side.

main concept is to have tree structure like this that....on clicking the "+" sign in fron of the rootBeans they should expand according to their contents.
as for example.

+ RootBean1
__ + ChldBean1
______- LeafNodeChild1
______-LeafNodeChild2
__ -LeafNodeRoot10

+ RootBean2
__ +ChldBean1
_____ -LeafNodeChild10
_____ -LeafNodeChild11
__ +ChldBean2
_____ - LeafNodeChild20
_____ - LeafNodeChild21
__ - LeafNodeRoot20
__ - LeafNodeRoot21


i hope the point is clear......

************JAVA SCRIPT CODE***********
// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
if (typeof(window[name])=="undefined" || window[name]==null) {
window[name]=val;
}
}


// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
if (!ul.childNodes || ul.childNodes.length==0) { return false; }
// Iterate LIs
for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
var item = ul.childNodes[itemi];
if (itemId!=null && item.id==itemId) { return true; }
if (item.nodeName == "LI" {
// Iterate things in this LI
var subLists = false;
for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
var sitem = item.childNodes[sitemi];
if (sitem.nodeName=="UL" {
subLists = true;
var ret = expandCollapseList(sitem,cName,itemId);
if (itemId!=null && ret) {
item.className=cName;
return true;
}
}
}
if (subLists && itemId==null) {
item.className = cName;
}
}
}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
setDefault("treeClass","mktree" ;
setDefault("nodeClosedClass","liClosed" ;
setDefault("nodeOpenClass","liOpen" ;
setDefault("nodeBulletClass","liBullet" ;
setDefault("nodeLinkClass","bullet" ;
setDefault("preProcessTrees",true);
if (preProcessTrees) {
if (!document.createElement) { return; } // Without createElement, we can't do anything
uls = document.getElementsByTagName("ul" ;
for (var uli=0;uli<uls.length;uli++) {
var ul=uls[uli];
if (ul.nodeName=="UL" && ul.className==treeClass) {
processList(ul);
}
}
}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
if (!ul.childNodes || ul.childNodes.length==0) { return; }
// Iterate LIs
for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
var item = ul.childNodes[itemi];
if (item.nodeName == "LI" {
// Iterate things in this LI
var subLists = false;
for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
var sitem = item.childNodes[sitemi];
if (sitem.nodeName=="UL" {
subLists = true;
processList(sitem);
}
}
var s= document.createElement("SPAN" ;
var t= '\u00A0'; // &nbsp;
s.className = nodeLinkClass;
if (subLists) {
// This LI has UL's in it, so it's a +/- node
if (item.className==null || item.className=="" {
item.className = nodeClosedClass;
}
// If it's just text, make the text work as the link also
if (item.firstChild.nodeName=="#text" {
t = t+item.firstChild.nodeValue;
item.removeChild(item.firstChild);
}
s.onclick = function () {
this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
return false;
}
}
else {
// No sublists, so it's just a bullet node
item.className = nodeBulletClass;
s.onclick = function () { return false; }
}
s.appendChild(document.createTextNode(t));
item.insertBefore(s,item.firstChild);
}
}




}
*****************JAVA SCRIPT CODE ENDS***********************


Thanks and Regards
AKHIL
[ December 09, 2004: Message edited by: Akhil Jain ]

-------------------------------<br />you can't move ahead, unless you try for it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone help in this javascript?
 
Similar Threads
Event for any change in HTML Form
howto make popup form scrollable
retreaving the value in servlet which is set in session in a jsp page
floating image in firefox ? still in trouble ^_^
java coding problem ~ system.out.println