Main Blog

Free Code: "Unobtrusive JavaScript" Tree Control

Posted by Sam on 30 October 2005

Here's a tree control that uses semantic HTML, unobtrusive JavaScript, and is easily reusable in a bundle of applications.

Here's the first of what will hopefully be a bunch of code that we donate to the web at large.  I made this a few months ago, but it's been serving me well.

There are a lot of tree controls out there, but most of them are tacky, heavyweight relics of the"DHTML" era.  I felt it was time for a change.  In particular, I wanted to adopt "unobtrusive javascript" techniques.  Among other things, the script has these notable features:

  • All of the styling is in a seperate CSS file.  The only thing that the JavaScript manipulates is class names.   This gives you a lot of control over the design relatively easily.
  • There is a no "set up code".  The script searches for <ul class="tree"> and applies its magic to that.
  • All of the content as loaded in a set of nested <ul>s - the simplest, code that I could think of that was semantically linked to heirachical data.


Go here for demo, instructions, and a bit of information about how I put it together.

If you've got comments, requests, suggestions, etc, email me on sam@silverstripe.com.

 

Post your comment

Comments

  • You mention that there are a lot of tree controls out there, but I am having trouble finding one to use in rails apps that has the features I want.

    1. Use <ul>
    2. Unobtrusive javascript
    3. Manage node selection
    4. Manage node re-organization (move up/down/in/out)
    5. Manage creation of new nodes

    Creation/reorganization of nodes should happen server side, but it would be nice if the tree control abstracted the set of events. What would be really good is if the tree control would be smart enough to only redraw the section of tree that changed after a create/delete/re-organize event. So if I have a node selected and I click move_out, the remote service need only return the grandparent & children and the tree control manages the re-drawing. Similarly with delete, the remote service needs only return success/failure and the tree control removes the element on success.

    Does anything like this exist?

    Posted by Michael Johnston, 15/06/2007 7:15am (1 year ago)

RSS feed for comments on this page