JavaScript
  Home arrow JavaScript arrow Page 2 - Adding Elements to a Tree with TreeView jQuery
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVASCRIPT

Adding Elements to a Tree with TreeView jQuery
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-05-13


    Table of Contents:
  • Adding Elements to a Tree with TreeView jQuery
  • Review: persisting hierarchical menus with TreeView's persist option
  • Adding elements to an existing tree with the add option
  • Appending multiple sub links with the add option

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Adding Elements to a Tree with TreeView jQuery - Review: persisting hierarchical menus with TreeView's persist option
    ( Page 2 of 4 )

     

    Before I proceed to demonstrate how to dynamically add elements to a hierarchical tree, I'd like to reintroduce the two examples developed in the previous part of the series. These were aimed at illustrating how to use the "persist" option to save the state of a targeted tree. 

    Having said that, here's how these code samples were created originally:

     

    (example on using the 'persist: location' option)

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Example on jQuery TreeView (using the persist: location option)</title>

    <link rel="stylesheet" href="jquery.treeview.css" type="text/css" />

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript" src="jquery.treeview.js"></script>

    <script>

    // build treeview after web page has been loaded

    $(document).ready(function(){

    $("#menu").treeview({

    persist: "location"

    });

    });

    </script>

    </head>

    <body>

    <ul id="menu">

    <li>Link 1

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 2

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 3

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 4

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 5

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    </ul>

    </body>

    </html>

     

     

    (example on using the 'persist: cookie' option)

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Example on jQuery TreeView (using the persist: cookie option)</title>

    <link rel="stylesheet" href="jquery.treeview.css" type="text/css" />

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript" src="jquery.treeview.js"></script>

    <script>

    // build treeview after web page has been loaded

    $(document).ready(function(){

    $("#menu").treeview({

    persist: "cookie"

    });

    });

    </script>

    </head>

    <body>

    <ul id="menu">

    <li>Link 1

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 2

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 3

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 4

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    <li>Link 5

    <ul>

    <li>Sub link 1</li>

    <li>Sub link 2</li>

    <li>Sub link 3</li>

    <li>Sub link 4</li>

    <li>Sub link 5</li>

    </ul>

    </li>

    </ul>

    </body>

    </html>

     

    As shown above, it's fairly simple to maintain the state of a selected tree across several HTTP requests by using the "persist" option. In the first example, this argument has been assigned a "location" value, implying that the settings of the tree will be maintained according to the value of the "href" attribute corresponding to the tree's hyperlinks. The second case is a bit more intuitive; it will perform a similar saving process by using some cookies. 

    At this point, you'll have surely recalled how to work with the previous "persist" argument, so the next thing that I'm going to cover will consist of explaining how to dynamically add elements to an existing tree via JavaScript. 

    As you might have guessed, this process will be accomplished by means of another handy argument provided by the "TreeView" plug-in, and its implementation will be discussed in the section to come. So click on the link below and proceed to read the new few lines.



     
     
    >>> More JavaScript Articles          >>> More By Alejandro Gervasio
     

       

    JAVASCRIPT ARTICLES

    - Introduction to JavaScript
    - Adding Elements to a Tree with TreeView jQue...
    - Using the Persist Argument in a TreeView jQu...
    - Using Unique and Toggle in a TreeView jQuery...
    - Using Event Delegation for Mouseover Events ...
    - Using the Animate Option in a Treeview jQuer...
    - Using HTML Lists with Event Delegation in Ja...
    - Opened and Closed Branches on a TreeView jQu...
    - Mouseover Events and Event Delegation in Jav...
    - Creating a TreeView JQuery Hierarchical Navi...
    - Event Delegation in JavaScript
    - A Look at the New YUI Carousel Control
    - Working with Draggable Elements and Transpar...
    - Displaying Pinned Handles with Resizable Con...
    - Building Resizable Containers with the Ext J...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek