eDocEngine VCL
Create documents and reports in 18 formats
Compatibility
Delphi C++Builder
PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder
XtremePDFConverter VCL
Intelligently convert PDF to easily editable Word documents
Compatibility
Delphi 7/2005 RAD Studio 2007/2009/2010
PDFOne (for Java™)
Create, edit, view, print & enhance PDF documents and forms on Java™ platform
Compatibility
J2SE J2EE Windows Linux Mac (OS X)
PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0
XtremeDocumentStudio .NET
Multi-format document-processing component suite for .NET developers
Compatibility
Visual Studio 2010/2012 Delphi Prism
Free PDF Reader
A free, fast and portable application for viewing, printing & converting PDF documents
Compatibility
Windows Linux Mac

Making An Interactive TreeView Control From An HTML Nested List

Create a list, give it an ID, and set this event handler on the list.
By V. Subhash

I was preparing for a presentation and I did not want use PowerPoint. The presentation was about HTML. So, what better way to demonstrate HTML than using it as the PowerPoint replacement. Here is what I did.

First, I created a nested HTML list and gave it an ID.

<ul id="LinkedList1" class="LinkedList">
  <li>History of WWW
    <ul>
      <li>Arpanet - Packets - 1969</li>
      <li>TCP/IP - Vinton Cerf - 1974</li>
      <li>WorldWideWeb (Internet and program) - Tim Berners Lee - 1991</li>
…

Then, I added this style so that the list tree is collapsed by default.

<style type="text/css">
  ul.LinkedList { display: block; }
  ul.LinkedList ul { display: none; } 
  .HandCursorStyle { cursor: pointer; cursor: hand; }  /* For IE */
</style>

If you want the list to be expanded by default, say for JavaScript-disabled clients, then you can comment out the second-line.

<style type="text/css">
  ul.LinkedList { display: block; }
  /* ul.LinkedList ul { display: none; } */
  .HandCursorStyle { cursor: pointer; cursor: hand; }  /* For IE */
</style>

Then, I add the click event handler.

  <script type="text/JavaScript">
    // Add this to the onload event of the BODY element
    function addEvents() {
      activateTree(document.getElementById("LinkedList1"));
    }

    // This function traverses the list and add links 
    // to nested list items
    function activateTree(oList) {
      // Collapse the tree
      for (var i=0; i < oList.getElementsByTagName("ul").length; i++) {
        oList.getElementsByTagName("ul")[i].style.display="none";            
      }                                                                  
      // Add the click-event handler to the list items
      if (oList.addEventListener) {
        oList.addEventListener("click", toggleBranch, false);
      } else if (oList.attachEvent) { // For IE
        oList.attachEvent("onclick", toggleBranch);
      }
      // Make the nested items look like links
      addLinksToBranches(oList);
    }

    // This is the click-event handler
    function toggleBranch(event) {
      var oBranch, cSubBranches;
      if (event.target) {
        oBranch = event.target;
      } else if (event.srcElement) { // For IE
        oBranch = event.srcElement;
      }
      cSubBranches = oBranch.getElementsByTagName("ul");
      if (cSubBranches.length > 0) {
        if (cSubBranches[0].style.display == "block") {
          cSubBranches[0].style.display = "none";
        } else {
          cSubBranches[0].style.display = "block";
        }
      }
    }

    // This function makes nested list items look like links
    function addLinksToBranches(oList) {
      var cBranches = oList.getElementsByTagName("li");
      var i, n, cSubBranches;
      if (cBranches.length > 0) {
        for (i=0, n = cBranches.length; i < n; i++) {
          cSubBranches = cBranches[i].getElementsByTagName("ul");
          if (cSubBranches.length > 0) {
            addLinksToBranches(cSubBranches[0]);
            cBranches[i].className = "HandCursorStyle";
            cBranches[i].style.color = "blue";
            cSubBranches[0].style.color = "black";
            cSubBranches[0].style.cursor = "auto";
          }
        }
      }
    }
  </script>
</head>
<body onload="addEvents();">

Demo (Click The Faux Links In The List)

Here is how it works. The blue-colored text are not hyperlinks. They hide nested branches. Click the blue-colored text to expand the hidden branches.

Example HTML Source

Here is the test page source code. It has been tested OK in IE (v5.5 to v9 Beta) and recent versions of (Google Chrome-based) Comodo Dragon, Firefox and Opera.

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).