ColdFusion 8: AJAX UI Menus

Continuing on with my tour of the new AJAX UI controls in ColdFusion 8, today I want to talk about menus. It seems like I've been building DHTML menus since before the dinosaurs walked the Earth, so it's nice to see this made a heck of a lot easier in ColdFusion 8. Let's get into it.

At the simplest level, menus are made with the cfmenu and cfmenuitem tags:

<cfmenu bgColor="##c0c0c0">
   <cfmenuitem display="Item One" href="http://www.adobe.com" />
   <cfmenuitem display="Item Two" href="http://www.cnn.com" />
</cfmenu>
This example creates a menu with two items. One that will show "Item One", and link to Adobe, and another one labeled "Item Two" that will link to CNN. I supplied an optional background color. By default the menu will be a normal horizontal menu. This creates the menu you see here. (I put some text under the menu so you could see it in context.)

How do you create sub menus? Just nest the menu items!

<cfmenu bgColor="##c0c0c0">
   <cfmenuitem display="Adobe" href="http://www.adobe.com" />
   <cfmenuitem display="Products">
      <cfmenuitem display="ColdFusion" href="http://www.adobe.com/go/coldfusion" />
      <cfmenuitem display="Flash" href="http://www.adobe.com/go/flash" />
   </cfmenuitem>
</cfmenu>

This example creates a submenu under a menu named Products. You can see this in action here.

How about a vertical menu? Just add a type to the cfmenu tag:

<cfmenu bgColor="##c0c0c0" selectedItemColor="##ff0000" type="vertical" width="200">

I added a few things here. First the type="vertical". Then notice I also specified a highlight color for the menu. I think the red goes great with the gray. Next - since this isn't a menu going across the page, I supplied a width. I put this into a table and you can see the result here.

Another option - you can supply images to go along with your menu items. These should (obviously) be smaller sized images. I'm using a Silk Icon in the next example:

<cfmenuitem display="ColdFusion" href="http://www.adobe.com/go/coldfusion" image="page_white_coldfusion.png" />

Another modification you can make is to add dividers. This would be useful for breaking up a long menu into sections:

<cfmenuitem divider="true" />

Demo here.

So thats a quick look at cfmenu. I didn't cover everything (that's what docs are for!), but hopefully this will give you a taste of the control.

Comments

I played with it yesterday while watching Tiger miss putts and noticed a couple of minor things. If you see something that I overlooked in the docs, let me know.

The images are set as css background images. On a horizontal menu with more than a few items and images, the text may overlap the image some. You can over-ride the width by adding css for the name of the item, such as cfmenuitem name="foo" would be #foo {width:120px;}

I couldn't get the text to right or center align with the cfmenu style. Had to add in css for the element such as #foo {text-align:right}

On large menus, the menu shows up in vertical order then 'flashes' to horizontal. Think maybe this is lazyload but not sure.

Of you have a large amount of sub-menu items, the scrolling is slow.

And lastly, I see that showdelay is set to 200ms. Do you know how I can change that after the page is loaded?
# Posted By Scott P | 6/18/07 6:59 PM
Very nice feature. Too bad it's nearly too ugly in Opera to use :(
# Posted By Tomas Fjetland | 6/18/07 7:17 PM
@ScottP - not sure. File an ER? :)

@Thomas - Ditto - file an ER. Adobe _does_ listen to bug reports.
# Posted By Raymond Camden | 6/18/07 9:57 PM
basic HTML markup problems with the generated content. This isn't valid:
<div id="_cf_menu1182244792322"

because all ids have to start with a letter.
# Posted By duncan | 6/19/07 4:35 AM
Will do - think there is a already some of those on file but I'll dbl-check.
# Posted By Scott p | 6/19/07 5:35 AM
Can these new menus in CF8 be built dynamically, for example, with data pulled from a query?
# Posted By Doug S. | 6/19/07 8:56 AM
I had no problem making dynamic menuitems.
# Posted By Raymond Camden | 6/19/07 9:55 AM
Is it me or do the menus seem to have odd hide/display issues. I was playing around with submenus and the hide/display on them just does not seem consistant.

--Dave
# Posted By Dave Ferguson | 6/19/07 12:56 PM
I didn't see that - but as always - file a bug. :)
# Posted By Raymond Camden | 6/19/07 1:00 PM
It looks like the target attribute doesn't work correctly. When I add target="_blank" so that it will open a new window, the new window gets displayed behind the calling window.
# Posted By BJ McShane | 9/17/07 9:11 AM
You can't use cfmenu/cfmenuitem with a recursive technique like those you reinvoke a .cfm file (using customtag or cfmodule) to build a multiple item menu.

Also, on IE (so far I found IE7), you can't finish a cfmenuitem block with a "cfmenuitem divider=true", it will throw a JavaScript error. You must finish with a "content" cfmenuitem. On Firefox, Safari and Opera runs fine.
# Posted By Alex Hubner | 10/20/07 2:42 AM