www.SmarteGuru.com
  Home | Blogs | Recipe | Find a Friend | Discussion Board | Resources | Developers Area | Articles | Health |  Login | Register Now 
smart Dev Categories
HTML & XHTML Tutorials
CSS Tutorials
JavaScript & AJAX Tutorials
XML, XSLT & Web Services
Linux Side Programming
Ruby on Rails
Apache & IIS Configuration
Server Side Essentials
PHP & MySQL Reviews and Apps
PHP & MySQL Tutorials
PHP & MySQL News & Interviews
Java and J2EE
ColdFusion Tutorials
CGI & Perl Tutorials
ASP & .NET Tutorials
Window Side Programming
smart Dev Home > ColdFusion Tutorials

Build a Dynamic Menu in ColdFusion

 
By By John Wilker on September 24th 2003 No of views:

Static content is so yesterday. Websites, whether internal or external, are all about staying current.

To that end, on a recent internal project I created a new intranet site and opted to develop a dynamically populated menu in ColdFusion. I went this route so that managing the links didn?t require us to open up the HTML files each time new content was added. This made updating the menu much less trouble for me as well as the content owners.

Why go through the trouble of building a dynamic menu in ColdFusion when an HTML/JavaScript menu could be written and maintained with relative ease? Sure, we could have built the menu in HTML and JavaScript and just modified the code as needed, but where?s the fun in that? More importantly, project maintenance wasn?t what I was looking for. I didn?t want to be tied to the project in perpetuity; the team I was on was too small and had too many new projects to be spending time on the maintenance of old projects.

The end users and I came up with a menu that could be managed easily, and took up very little space on the site. The reason why space was an issue was that the site had several embedded sections, each of which had several embedded sections, which had? you see where I?m going.

Before the Code?

We?ll start by designing the database -- we can?t write code to access a database until we know how the database is designed. The database for our dynamic menu is straightforward. To accomplish our menu we only need two tables -- no stored procedures or anything else (though a stored procedure would be the easiest way to manage the data in the menu), so you can use any database system you?d like.

1224_image1
Image 1

Image 1 shows the database tables we?ll use for our menu. The MENU_ITEMS table is where all the actual menu tree items will live. Name (menu_item_name) and HREF (menu_item_url) are stored along with the primary key (menu_item_id). The table MENU_ITEMS_XREF holds the structure of the tree itself. Both menu_item_id and child_id refer to the menu_item_id from the MENU_ITEMS table. IDs that appear in the child_id column indicate that that ID will be nested under the item in the menu_item_id column. Image 2 illustrates this concept.

1224_image2
Image 2

The last column in the MENU_ITEMS_XREF table is the sort_order field. Initially, I just let the menu query that generates the menu handle the sorting (alphabetically). Wouldn?t you know it, though -- the business unit had other ideas. So now there is a sort_order field, which allows the content owner to place more important or popular topics closer to the top of the page.

That?s it for the database. Let?s code!

The Code

The code we use to create the menu is pretty easy to follow. Basically, we?ll create a custom ColdFusion tag that recurses over a database structure and builds the appropriate menu structure as it goes. We?ll break down the pieces of create_menu.cfm, which you can download here, and discuss each section. The end result will be a completely de-mystified, dynamically built tree menu.

The code in Section A defines some initial parameters we?ll need as default. You may or may not need these, depending on whether your site has pages in sub folders and such. Our site did, so we needed a way to get up the folder tree to the images directory.

Section A

<CFPARAM DEFAULT="1" NAME="ATTRIBUTES.Level">
<CFPARAM DEFAULT="" NAME="ATTRIBUTES.divID">

<CFIF ATTRIBUTES.Level EQ 1>
   <CFSET VARIABLES.RootLevel = "">
<CFELSEIF ATTRIBUTES.Level EQ 2>  
   <CFSET VARIABLES.RootLevel = "../">
<CFELSEIF ATTRIBUTES.Level EQ 3>  
   <CFSET VARIABLES.RootLevel = "../../">
</CFIF>

The following sections of code all combine into a single JavaScript block, but for the sake of making it easier to explain, I?ve broken it into smaller, more digestible pieces. This is the JavaScript that will handle the menu, collapsing and expanding the appropriate branches, and changing the image states of those branches as required.

? Page 1 2 3
 


Rate this Article :  
Current Rating:
 
Home - About Us - Help - Terms and Conditions - Site Map - Link to Us - Resources - Contact Us
Google Rank Calculator | Suggest developer resource | Suggest Article
All rights reserved © 2007 SmarteGuru.com.