I was having a severe performance problem with large trees when I used the DOM approach to building them. I switched to one that built a big fat innerHTML string and it's about 30 times faster. This is why I'm doing the onClick thing. I could still probably write something to parse the tree once its rendered to add all of the eventListener stuff but I suspect that parsing the DOM tree to locate objects was the root of my performance issue.
At the moment, I have this working:
function myOnClick (el, evt) {
// Firefox passes evt, IE does not thus we have...
if (!evt) evt = window.event;
// I'm shocked this works but apparently it does...
if (evt.stopPropagation) evt.stopPropagation(); // for Firefox
evt.cancelBubble = true; // for IE
// grab some custom attributes from the element
var iNodeID = el.getAttribute('nodeID');
var childDivID = el.getAttribute('childDivID');
// do stuff...
}
The onclick assigned to the div tags is like this:
<div id="tree_445_i" class="itemDefault" onclick="myOnClick(this, arguments[0]);" nodeid="445" isselected="0" childDivID="tree_445_c">
It works perfectly for me in IE7 and Firefox 2. I have no idea about safari or opera or whatever. Putting arguments[0] in that onclick html was just something I tried on a whim so I tend to doubt that it does.
Javascript: wonk.
EDIT: I'm considering adding listeners to the tree and some other events and stuff but i'm a bit confused about how to add few listeners so that i:
save memory
avoid leaks
* avoid parsing the whole DOM to find each element thereby causing performance problems
while still being able to distinguish between event targets properly. If I add a 'click' event listener to my whole tree, I have a hard time determining what needs to be done unless I add more data to each of the DOM nodes I'm creating.
At the moment, I'm mulling over how best to connect actions to a particular node. Doing an onSelectNode sort of event seems like a good idea but I'm not sure how yet. The problem is partly about specifying a tree's structure in as little code as possible and partly avoiding an event listener on every single DOM node.
EDIT: At the moment, it's quite fast. On my intel machine (dual core 2.67ghz--unsurprsingly, the 2nd core doesn't help) it can render about 8000 nodes in firefox in about 2.5 seconds. IE takes much longer. I can't imagine ever needing that many nodes in a javascript tree but it's nice to know it can be done.