Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / Javascript
Article

JScript Tree List Control v3.0

Rate me:
Please Sign up or sign in to vote.
4.88/5 (67 votes)
6 Dec 20024 min read 307.3K   5.2K   89   89
Completely rewritten and much more versatile than my two previous tree list controls, this version includes dynamic branch loading, user-specifiable tree column, and more!

Sample Image - treelistcontrol.v3.gif

Introduction

This is my third attempt at making a TreeListControl for Internet Explorer-based web applications. The first was very slow and required ASP to generate a tree. The second has lots of features, and even resizable columns, but, while it did not suffer from the freeze-up delays that occurred in the first one, was generally sluggish in operation if there were more than a few nodes on the screen. This latest version has tighter code and is generally more versatile all round. It doesn't have resizable columns, but it shouldn't be too hard to put them in. If I do that, I'll upload the updated version here. The only other major limitation of this control is that it is fixed-width. That is, you have to specify the exact width of each column, and this determines the control width.

Features that version 3.0 has are:

  • Enable/disable column headings
  • Enable/disable root node
  • The ability to choose which column the tree appears in
  • Dynamic loading of branches via XML files
  • Completely dynamic updating of the tree structure after it has been generated, including:
    • remove nodes
    • add new nodes
    • remove nodes and re-add them somewhere else
    • insert before or after a specified node
    • expand/collapse branches
  • optional row onclick action
  • row highlights while being pointed at
  • prevent specified columns from firing the row onclick event (e.g. for a column of checkboxes)

To use the control, simply include treelistcontrol.js and treelistcontrol.css in your html file, then, inside the element (body tag, div tag, or where ever) you want to render the tree inside, place a script tag and build the tree according to the API. A demo of how to do this is inside the included zip file.

Usage

See the API below for explanations of the tree's objects, and the file demo.htm for an example of how to use the tree.

  1. Create a tree control
  2. Create and add the columns to the tree
  3. Add all the initial nodes to the tree
  4. document.write the tree object to the page

Tree List Control v3.0 API

TreeListControl

TreeListControl(strRootNodeLabel, strRootNodeIconSrc, boolHideColumnHeadings, boolHideRootNode)

  • strRootNodeLabel: the label to use for the root node
  • strRootNodeIconSrc: the path to the node's icon
  • boolHideColumnHeadings: don't show the column headings
  • boolHideRootNode: don't show the root node

Properties

  • showColumnHeadings: hide/show the column headings. The visibility of the column headings will automatically change depending on the state of this property.
  • showRootNode: hide/show the root node. The visibility of the root node will automatically change depending on the state of this property.
  • all: this is a collection (array) contain references to all the nodes that were assigned a non-null refKey when added to the tree. usage: mytree.all['mynoderefKey']
  • allNodes: this is a collection containing references to every node in the tree, indexed by the node's automatically assigned ID property.
  • rootNode: a reference to the tree's root node. This is of type TreeListControlNode.
  • iconPath: the path to the location where the tree's images are stored. The default is 'tlcimages/'.

Methods

  • addColumn( oColumnObject ): adds a column to the tree. Do not use this once the tree has been rendered to the page. Pass in a reference to a TreeListControlColumn object that defines the column's parameters.
  • add( oNodeObject ): appends a node to the children of the root node. Pass in a reference to a previously created node.

TreeListControlColumn

TreeListControlColumn (intWidth, strTitle, boolPreventRowClickEvents)

  • intWidth: the width, in pixels, of the column.
  • strTitle: the heading/title of the column. Use a zero-length string if no title is desired.
  • boolPreventRowClickEvents: when a row is clicked, it fires the onclick event for that row. Setting this parameter to true will prevent clicks in this row from firing that event.

TreeListControlNode

TreeListControlNode (boolShowChildren, strIconSrc, strXMLSrc, strReferenceKey)

  • boolShowChildren: this specifies the initial expand/collapse state for the node.
  • strIconSrc: the path to the icon image to use.
  • strXMLSrc: if the children of this node are to be loaded at run-time when the node is expanded, this parameter should contain the URL of the XML file. Set to null or '' if not used.
  • strReferenceKey: if you want to use your own identifiers for each node, instead of the automatically-assigned ID that each node has, specify the identifier here. It can be an integer or a string, and, once the node has been added to the tree, you can access the node via the all property of the TreeListControl object.

Properties

  • onclick: the javascript to execute when the onclick event fires for that node. Only set this before the node has been added to the tree.

Methods

  • expand: shows the node's children.
  • collapse: hides the node's children.
  • setText( strColumn1, strColumn2, ..., strColumnN ): sets the node's text in each column.
  • setColumnText( intColumn, strText ): sets the text for the node in the specified column.
  • add( oNodeObject ): appends a node object to this node's children. DO NOT use this method if this node has not yet been added to the tree.
  • insertBefore( oNodeObject ): inserts the supplied node immediately before this node.
  • insertAfter( oNodeObject ): inserts the supplied node directly after this node.
  • remove(): removes this node from the tree completely and returns a reference to the node so that it can be reinserted elsewhere if desired.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
Web application developer, graphic designer, aspiring entrepreneur, snowboarder and electronic music afficianado.


Comments and Discussions

 
Generalicon src Pin
Anonymous11-Sep-03 1:38
Anonymous11-Sep-03 1:38 
Questionhow to create .xml to show the tree control? Pin
yanlinzhang1-Sep-03 23:04
yanlinzhang1-Sep-03 23:04 
Generalit's so good Pin
Anonymous3-Aug-03 17:13
Anonymous3-Aug-03 17:13 
GeneralFreeze up Pin
pobrob23-Jul-03 11:32
pobrob23-Jul-03 11:32 
GeneralRe: Freeze up Pin
tigertoy18-Dec-03 9:25
tigertoy18-Dec-03 9:25 
GeneralRe: Freeze up Pin
pobrob19-Dec-03 6:44
pobrob19-Dec-03 6:44 
GeneralRe: Freeze up Pin
Oliver Haskell28-Jan-04 2:25
Oliver Haskell28-Jan-04 2:25 
GeneralTreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
M.E.-P._Germany16-Jul-03 22:28
M.E.-P._Germany16-Jul-03 22:28 
Hi first of all, I've to say Great Job you did with that tree control. I know I'm not the first to tell you that, but it's really just great.
Unfortunatly I'm encountering a problem that I don't really understand.

I'm using the tree to display certain elements of an XML File. The XML File has an XSL Stylesheet that contains a reference to a Javascript file. The XML File is parsed by my Javascript and a recursive function within adds nodes to the tree.
That all works fine and the tree is shown in my Internet Explorer Window.

In my Page created by the XSL Stylesheet there is also a button to change the current view. (i.e. display all data, display filtered data). One of the functions is a Print Preview function that should open a new Internet Explorer Window and display the tree just in a different position of the window.

So here's my problem:
Normally I create the code for the new Explorer Window by defining a string within my Javascript and add HTML Code to that string. Afterwards I use the Javascipt function document.write(string) to write the string to the new window and the html code is displayed. If I try to do that with my tree I get the following error:

"TreeListControlHandler.allTrees.6.showRootNode is Null or no Object"

I have no Idea what this error means or how to fix that. Maybe anyone of you knows. Would be really glad.

My Code that doesn't work looks like this:
------------------------------------------

F1 = window.open ( "about:blank", "Print_View_Window", "width=700,height=650, left=0, top=0, scrollbars=yes, menubar=yes, toolbar=no, location=no");
F1.focus();
.
.
.
var tree = new TreeListControl('Test Tree','../../../dtd_xsl/tlcimages/my_icon_folder.gif', false, false);
//tree.addColumn ( new TreeListControlColumn(20, '') );
tree.addColumn ( new TreeListControlColumn(20, '', true) );
tree.addColumn ( new TreeListControlColumn(310, 'Stepnr') );
tree.addColumn ( new TreeListControlColumn(45, 'Status') );
tree.addColumn ( new TreeListControlColumn(65, 'Lower Limit') );
tree.addColumn ( new TreeListControlColumn(90, 'Measurement') );
tree.addColumn ( new TreeListControlColumn(65, 'Upper Limit') );
tree.addColumn ( new TreeListControlColumn(55, 'Unit') );
tree.rootNode.setText('','Begin Test Results','',' ');
tree.nLabelColumn = 1;

recursive_BuildTreeForPrintView(xmlroot.firstChild,tree.rootNode,my_string);

my_string += tree;
.
.
.
F1.document.write(s);
F1.document.close();





My Code that works looks like this:
-----------------------------------

var tree = new TreeListControl('TestTree', '../../../dtd_xsl/tlcimages/my_icon_folder.gif', false, false);
//tree.addColumn ( new TreeListControlColumn(20, '') );
tree.addColumn ( new TreeListControlColumn(20, '', true) );
tree.addColumn ( new TreeListControlColumn(310, 'Stepnr') );
tree.addColumn ( new TreeListControlColumn(45, 'Status') );
tree.addColumn ( new TreeListControlColumn(65, 'Lower Limit') );
tree.addColumn ( new TreeListControlColumn(90, 'Measurement') );
tree.addColumn ( new TreeListControlColumn(65, 'Upper Limit') );
tree.addColumn ( new TreeListControlColumn(55, 'Unit') );

tree.rootNode.setText('','Begin Test Results','',' ');
tree.nLabelColumn = 1;

recursive_BuildTree(xmlroot.firstChild,tree.rootNode);
my_string += tree;

DisplayData.innerHTML = my_string;


So the only difference in the code that doesn't work is that I attempt to write the tree to a new Explorer Window instead of writing to the current window.
This Error also occurs when I delete the call of the recursive function, so it has nothing do do with that.
Hope anyone has a clue how to fix it and get it to work.
Hopefully there's a workaround for that problem.

Thanks in Advance,
Mathias
GeneralRe: TreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
pobrob17-Jul-03 3:49
pobrob17-Jul-03 3:49 
GeneralRe: TreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
M.E.-P._Germany17-Jul-03 10:00
M.E.-P._Germany17-Jul-03 10:00 
GeneralRe: TreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
Nathan Ridley17-Jul-03 14:01
Nathan Ridley17-Jul-03 14:01 
GeneralRe: TreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
M.EP_Germany17-Jul-03 21:26
sussM.EP_Germany17-Jul-03 21:26 
GeneralRe: TreeListControlHandler.allTrees.6.showRootNode is Null or no Object Pin
Nathan Ridley29-Apr-04 19:15
Nathan Ridley29-Apr-04 19:15 
GeneralHad to change Status to 200 Pin
pobrob16-Jul-03 5:42
pobrob16-Jul-03 5:42 
GeneralRe: Had to change Status to 200 Pin
Nathan Ridley16-Jul-03 12:30
Nathan Ridley16-Jul-03 12:30 
GeneralRe: Had to change Status to 200 Pin
rcolonia19-Jul-03 11:05
rcolonia19-Jul-03 11:05 
GeneralVery Good Pin
eugenesun15-Jul-03 0:04
eugenesun15-Jul-03 0:04 
Generalwired tree behavior Pin
iwavelet10-Jul-03 11:01
iwavelet10-Jul-03 11:01 
GeneralRe: wired tree behavior Pin
Nathan Ridley10-Jul-03 12:29
Nathan Ridley10-Jul-03 12:29 
GeneralRe: wired tree behavior Pin
iwavelet14-Jul-03 5:42
iwavelet14-Jul-03 5:42 
GeneralClipped checkboxes + SOAP + XML Pin
Ola Strandberg26-Jun-03 1:01
sussOla Strandberg26-Jun-03 1:01 
GeneralWhich Versions of IE Pin
pobrob25-Jun-03 9:06
pobrob25-Jun-03 9:06 
GeneralRe: Which Versions of IE Pin
Nathan Ridley25-Jun-03 12:40
Nathan Ridley25-Jun-03 12:40 
GeneralUsing showchildren in an XML Pin
bsleek11-Jun-03 11:00
bsleek11-Jun-03 11:00 
GeneralRe: Using showchildren in an XML Pin
Nathan Ridley11-Jun-03 12:27
Nathan Ridley11-Jun-03 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.