Yes, you can make CSS style calls through JavaScript and apply them directly to tree nodes in an HTML format CFTREE, here's how. First, let's populate a query with some values so we can output information to our CFTREE. I chose to name my record names in my query results the same as the three corresponding attributes in the CFTREEITEM; display, value, and parent.
<cfscript>
GetColor=QueryNew("display, value, parent");
QueryAddRow(GetColor, 3);
QuerySetCell(GetColor, "display", "Red", 1);
QuerySetCell(GetColor, "value", "Red", 1);
QuerySetCell(GetColor, "parent", "", 1);
QuerySetCell(GetColor, "display", "Cardinal", 2);
QuerySetCell(GetColor, "value", "Cardinal", 2);
QuerySetCell(GetColor, "parent", "Red", 2);
QuerySetCell(GetColor, "display", "Maroon", 3);
QuerySetCell(GetColor, "value", "Maroon", 3);
QuerySetCell(GetColor, "parent", "Red", 3);
</cfscript>
In the HEAD of our new document, we add the JavaScript function that our search results will use to expand our tree to the selected node and color the background of that selection.
<script type="text/javascript">
fnTreeExpand=function(obj){
GetTree = ColdFusion.Tree.getTreeObject("TreeData");
GetNode = GetTree.getNodeByProperty("id", obj);
GetNode.parent.expand();
GetLeaf = ColdFusion.DOM.getElement(GetNode.data.spanid, GetNode.tree.id);
GetLeaf.style.backgroundColor = "#33FF66";}
</script>
As the user clicks on a search result, it fires the "fnTreeExpand" function and sends it the current value of our tree item. The "TreeData" value is the name of our CFTREE (below). The function gets the node associated with the value sent to it, expands the parent (because it wouldn't be too useful if we didn't zero in on the selection for the user in a large tree). In this example we are setting the "spanid" around our node to a specific background color.
The BODY of our document contains our CFFORM and CFTREE tags and our search form.
<cfform name="FormTree" format="html">
<cftree name="TreeData" format="html">
<cfoutput query="GetColor">
<cftreeitem value="#GetColor.value#" display="#GetColor.display#" parent="#GetColor.parent#" queryasroot="no" expand="no">
</cfoutput>
</cftree>
</cfform>
<form action="<cfoutput>#CGI.SCRIPT_NAME#</cfoutput>" method="post" name="SearchFile">
Color Search
<input name="SearchColor" type="text" />
<input name="submit" type="submit" />
</form>
At the bottom of the page is the conditional check for the existence of a form submission. It loops over the query from the top of the page, which is included to show the structure expected from a more useful data source. If the search parameter is contained within a display result, then an anchor tag is generated with our "fnTreeExpand" JavaScript function call!
<cfif StructKeyExists(FORM, "SearchColor")>
Select Color<br />
<cfloop from="1" to="#GetColor.RecordCount#" index="LoopIndex">
<cfif GetColor.display[LoopIndex] CONTAINS FORM.SearchColor>
<a href="javascript:fnTreeExpand('<cfoutput>#GetColor.value[LoopIndex]#</cfoutput>');">
<cfoutput>#GetColor.display[LoopIndex]#</cfoutput>
</a><br />
</cfif>
</cfloop>
</cfif>
When you click on the anchor tag generated from the search, you'll end up with a green leaf in our tree.
If you find this post useful please leave a comment and let me know how you used the information.