Using CFTREE for Navigation

A user on cf-talk today asked if it was possible to use ColdFusion 8’s new HTML CFTREE as a navigation tool. Turns out it is rather simple, and like most things in ColdFusion, there are multiple solutions.

First off - when you use the HREF attribute of CFTREEITEM, you are allowed to use JavaScript. One simple solution would be to just do:

<cftreeitem display="products" href="javaScript:doProducts('...')">

You could use document.href.location to move the entire page, or use the new ColdFusion.navigate function to load a URL into a UI item like a CFDIV or CFWINDOW.

While that works ok for static, hard coded trees, it isn’t a good solution for dynamic trees, and, it can be done simpler if you just bindings. Consider this example:

<cfform name="form">

<cftree format=”html” name=”mytree”> <cftreeitem display=”Navigation” value=”root”> <cftreeitem display=”Page A” parent=”root” value=”a”> <cftreeitem display=”Page B” parent=”root” value=”b”> </cftree> </cfform>

<cfdiv bind=”url:content.cfm?value={mytree.node}” /> </code>

I’ve built a simple tree with one main node and two child nodes. I then added a CFDIV that is bound to the tree. When binding to a tree, you can get two values: node and path. Node returns just the value for the selected item. Path returns the path of the node itself. Thanks to Todd Sharp for finding these values in the docs.

p.s. As a side question - who would like to see an API doc that covers stuff like this? I wasn’t able to find it in the reference and it was hard to find in the developers guide. I’d like a guide that more clearly describes working with ColdFusion’s new UI elements.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments