2007-01-02

Cross-browser JavaScript TreeView

I'm going to have a go at putting up a working example of my Cross-browser JavaScript TreeView Control working, plus an example of how you can use it on your website!

So here I go...



It works under Firefox (version 1.0.8 through 2 (even the new Beta version)) and the dreaded Internet Explorer (version 5.0 and up).

I've currently finished my first Web-based Content Management System (WebCMS) which uses this treeView for the organisation of your files on the web server, but I build the Tree Nodes dynamically using my beloved Perl!

Please feel free to download the files and use them as you wish. I only ask that you leave my name in them for my personal credit/ego q:) I've created a .TAR.GZ for you to download

Code Explanation

  1. Firstly, create an HTML file and link the Style Sheet and JavaScript files like so:

    <link rel="stylesheet" type="text/css" href="tv.css" />
    <script type="text/javascript" src="treeView3.js"></script>
    <script type="text/javascript" src="treeViewConfig.js"></script>


  2. Next, include the call to the function that starts it all off, renderTreeView();
    <script type="text/javascript">

    renderTreeView();

    </script>

    somewhere inside the BODY tag

  3. Save your HTML file and edit your treeViewConfig.js file. It doesn't have to be called this, but it makes sense that's all. The treeViewConfig.js file must have at least the following items for the renderTreeView(); function to do anything of interest at all

    imageDir = "[path_to_tv_directory_with_images_in_it]";

    createRootNode([rootNodeID], "[Root Node Label To Display]", "javascript:toggleNode([roodNodeID]);", "[frame_to_perform_action_in]", 0, 1);

    addNode([nodeID], [parentNodeID], "[Child Node Label To Display]", "[action_to_perform_when_node_is_clicked]", "[frame_to_perform_action_in]", 2, 2);


  4. Load your HTML file in a browser

I'll give you a bit more explanation of the functions mentioned in step 3.

imageDir = "[path_to_tv_directory_with_images_in_it]";

This sets the path to all the images that the TreeView will use during it's rendering. Without this set correctly, the TreeView will render but all these broken image links will appear, making it look like crap. It's relative to the HTML file I believe (I could be wrong), just have a play around with it.

createRootNode([rootNodeID], "[Root Node Label To Display]", "javascript:toggleNode([roodNodeID]);", "[frame_to_perform_action_in]", 0, 1);

Every TreeView requires a Root Node (ok, that's not entirely true simply because you can have a TreeView without a Root Node but that's not the way I've designed this one). This TreeView requires a Root Node q:)

The createRootNode() function allows you to give the Root Node an ID to which every other Child Node can be added to (typically make this a Numerical Value, it's easier to parse trees this way, though String Values will work, just slower). It also allows you to specify the Label that appears for the Root Node. This can include any HTML you see fit.

Next is the "javascript:toggleNode([roodNodeID]);" parameter. This forms the <A HREF=""> part of the code that's generated, so whatever you can stick inside a normal <A HREF=""> attribute will happily go in here.

The "[frame_to_perform_action_in]" parameter in most cases for you, will be a blank string (ie. ""). This was included incase you decided to do a two framer version where the left frame would hold the TreeView and the content accessed by the Nodes would be displayed in the right frame. If this is the case, the value for this typically would always be the name of the right frame.

And lastly, the 0, 1 parameters. These are simply image indicies within the IMAGELIST array. At the moment, the first image that I load is the Plus Node icon which means that the Root Node by default would be closed, second image that I load is the Minus Node icon, so this specifies that if I have not selected this Node, use IMAGELIST[0]. If I've selected the Node use IMAGELIST[1] and so on.

Finally, the addNode([nodeID], [parentNodeID], "[Child Node Label To Display]", "[action_to_perform_when_node_is_clicked]", "[frame_to_perform_action_in]", 2, 2); code. This line is almost identical to the createRootNode() function call except this now applies to Nodes within the Root Node. Same details apply to these parameters as they do for the createRootNode() except the [parentNodeID] obviously references a Node that this Child is going to belong to.

Well that's about it for my second post. A whole lot of waffling going on here, but somehow, I now kinda understand what Programming books are like 1000 pages long!

20 comments:

Anonymous said...

It is necessary to be the optimist. [url=http://cgi3.ebay.fr/eBayISAPI.dll?ViewUserPage&userid=acheter_levitra_ici_1euro&achat-levitra]acheter levitra[/url] Radically the incorrect information

Anonymous said...

Besides, we accord lessen Cialis pharmacopoeia, which is somewhat impressive in exercise than other famous cheap Viagra now Erectile dysfunction Viagra dispensary online drugs.

Anonymous said...

At our purchase you can regard sundry medication and of course such popular http://ipod-playlist.com/viagra.html generic Viagra Online solitary – Cheap Generic Cialis pharmacy.

Anonymous said...

trГЁs utile topic http://lettresdudroit.com viagra sur le net [url=http://lettresdudroit.com]acheter viagra generique[/url]

Anonymous said...

votre idГ©e est brillante cialis cialis [url=http://www.ci2s.org]cialis en ligne[/url]

Anonymous said...

Dans cela quelque chose est. Maintenant tout m'est devenu clair, le Merci bien pour l'information. viagra viagra generique [url=http://lettresdudroit.com]viagra[/url]

Anonymous said...

la piГЁce trГЁs prГ©cieuse http://runfr.com/acheter-cialis cialis generique en pharmacie achat viagra pharmacie

Anonymous said...

sonne d'une maniГЁre sГ©duisante cialis generique cialis 20mg

Anonymous said...

je FГ©licite, vous Г©tiez visitГ©s par l'idГ©e remarquable http://runfr.com/tag/10mg cialis 10mg acheter cialis 20mg

Anonymous said...

Et comme lui comprendre http://runfr.com/acheter-cialis cialis prix acheter cialis en belgique

Anonymous said...

Es ist die sehr wertvolle Antwort viagra viagra kaufen [url=http//t7-isis.org]viagra[/url]

Anonymous said...

http://www.badbeats.com/forum/showthread.php?p=91758
http://forum.kiwiphp.com/viewtopic.php?f=2&t=27433&p=34942
http://www.lobkov.org/forum/viewtopic.php?f=2&t=13847&p=20564

Anonymous said...

Quels bons interlocuteurs :) cialis 20mg cialis

Anonymous said...

je FГ©licite, l'idГ©e magnifique http://runfr.com acheter cialis achat cialis original

Anonymous said...

So will not go.

Anonymous said...

Lipitor delivered overnight kamagra jelly nl Cephalexin Next Day ONLINE Avelox NO PRESCRIPTION Buy Lasix COD kamagra oral jelly c.o.d. buy Clomid free consultation generic form of cialis buy levitra online no prescription buy online prescription levitra without

Anonymous said...

alesse generic Premarin Cheap buy Avelox cheap without prescription diflucan flexeril keflex generic brand keflex injection generic mexico pharmacy provera order kamagra oral jelly online with cod Generic Buying Lasix Order Augmentin Pills Buy Flagyl ER in USA Online Pharmacy

Anonymous said...

tadalafil capsule Best Boniva generic pharmacies buy buy Flagyl ER Canada buy in online uk cephalexin fedex Avelox online cod how to get a Generic clomid prescription Buy best weightloss pills online avelox shipped cod protocol of multiple doses of diflucan Clomid canadian pharmacy

Anonymous said...

cheapest generic tadalafil Buy Augmentin Doctor Prescription fake online cephalexin Buy Clomid (Serophene) Epharmacist Ditropan For Cheap avodart dosage 100mg Amoxil prescription Ditropan for sale uk Take Cephalexin Without Prescription online buy alesse

Anonymous said...

diflucan chronic Purchase Online Without Prescription Buy Ditropan With No Prescription Augmentin Pharmacy Store subaction showcomments cialis smile posted Avelox cash delivery cod free avodart without prescription Buy Cephalexin Without Prescription Cheap Clomid Over Night capsule 500mg cephalexin red