Powered By Railo

Wish List

My Amazon.com Wish List

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui

Recent Entries

The Pirate Bay Sold!
Railo Documentation
iPhone Navi Software Now Available!
iPhone OS3 Cracked!

Search

RSS


Subscribe

Enter your email address to subscribe to this blog.

Blogroll

An Architect's View
Ben Forta
CFSilence
Coldfusion Jedi
Rey Bango
TalkingTree



Coldfusion 8's CFTREE tag

Coldfusion 8 introduced support for an HTML tree. With the HTML tree you can build an AJAX driven tree, which means you only need to load the to level nodes at first and all subsequent child nodes and leaves are only loaded when needed. The old requirements of the cftree tag, that being living inside of a cfform still exists.

You set up your tree like so:


<cfoutput>
    <cfform name="myform">
        <cftree name="treeTest" height="400" width="200" format="html">
            <cftreeitem bind="cfc:components.tree.getNodes({cftreeitempath},{cftreeitemvalue})">
        </cftree>
    </cfform>
</cfoutput>

The bind expression, like in my previous post about the cfgrid, is used to bind the creation of the tree elements to the output of a CFC.

Your cfc should return an array of structures that contain a value and a display value for nodes and an additional key "leafnode"=true when you want to show a leaf.

I created a simple function that would generate a two level tree just for example purposes.


<cffunction name="getNodes" access="remote" returntype="array">
    <cfargument name="path" type="String" required="false" default=""/>
    <cfargument name="value" type="String" required="true" default=""/>
    <!--- set up return array --->
        <cfset var result= arrayNew(1)/>
        <cfset var s =""/>

        <!--- if arguments.value is empty the tree is being built for the first time --->
        <cfif arguments.value is "">
            <cfset x = 0/>
            <cfloop from="1" to="10" index="i">
                <cfset x = x+1/>
                <cfset s = structNew()/>
                <cfset s.value=#x#>
                <cfset s.display="Node #i#">
                <cfset arrayAppend(result,s)/>
            </cfloop>
        <cfelse>
        <!--- arguments.value is not empty --->
        <!--- to keep it simple we will only make children nodes --->
        <cfset y = 0/>
            <cfloop from="1" to="#arguments.value#" index="q">
                <cfset y = y + 1/>
                <cfset s = structNew()/>
                <cfset s.value=#q#>
                <cfset s.display="Leaf #q#">
                <cfset s.leafnode=true/>
                <cfset arrayAppend(result,s)/>
            </cfloop>
        </cfif>
    <cfreturn result/>
</cffunction>

Since the tree binds directly to the cfc, and is therefore called immediately on generating the tree, we need to make sure that we only generate the top level nodes once, that is why we check arguments.value at the very top. If arguments.value eq '' then we generate the parent nodes, otherwise we process the children.

The default styling of the cftree is pretty ugly so you may want to create your own styles.

Related Blog Entries

Comments
jason's Gravatar thanks for this...how do you stylize this cftree?
# Posted By jason | 12/3/07 10:50 PM
Gary Gilbert's Gravatar Take a look at the code generated for the cftree it should include a couple of css styles you can also use firebug to inspected the generated html for the specific class attributes used. I personally haven't bothered to style it as I prefer the tree that you can get from using EXT JS
# Posted By Gary Gilbert | 12/4/07 11:48 AM
Andy Sandefer's Gravatar I've got an HTML cftree as follows...
<cfform name="frmFileTree" format="html" style="font-family:Verdana; font-size:11px;">
<cftree name="clientFileTree" format="html">
<cftreeitem bind="cfc:connect2a.cfc.filemgmt.getFileTreeForMember({cftreeitempath}, {cftreeitemvalue}, #SESSION.appUserID#)">
</cftree>

I've bound a cfinput type text in another form to this tree. Is there anyway that I can show the Display of the selected node (not the node or path) in my bound cfinput?
# Posted By Andy Sandefer | 1/15/08 3:07 PM
Glyn's Gravatar playing around with your example i seem to get a repeat on the top level nodes. i have made no changes to your example and i have arguments.value eq '' does anyone else get this?

node 1
node 1
leaf 1
node 2

etc
# Posted By Glyn | 2/4/08 2:08 PM
Glyn's Gravatar If anyone is interested I found why I have duplicated nodes from this example. This problem happens when you are using the Application.cfc method onRequestEnd() to display content. comment found here: http://www.forta.com/blog/index.cfm/2007/6/4/ColdF...

That also reminds me that onrequest() can not be used either.
# Posted By Glyn | 2/8/08 11:12 AM
Gary Gilbert's Gravatar Also if you are using Application.CFC you should not use the onRequest method as the CF engine will simply ignore any Ajax, flash remoting or event gateway requests.

Also don't forget to turn off debugoutput ...
# Posted By Gary Gilbert | 2/8/08 11:34 AM
karen's Gravatar I am knew at cfc componets and cf 8. I did this but the tree doesn't display on the page - is there something else I need to activate the tree being loaded?
# Posted By karen | 3/4/08 4:53 PM