Skip to content

Instantly share code, notes, and snippets.

/trycf-gist.cfm

Created Aug 26, 2016
Embed
What would you like to do?
TryCF Gist
<cfscript>
TestData = querynew("id,parentId,nodeText");
queryAddRow(TestData,{id:1,nodeText:"File"});
queryAddRow(TestData,{id:2,nodeText:"New",parentId:1});
queryAddRow(TestData,{id:3,nodeText:"File",parentId:2});
queryAddRow(TestData,{id:4,nodeText:"Folder",parentId:2});
queryAddRow(TestData,{id:5,nodeText:"Edit"});
queryAddRow(TestData,{id:6,nodeText:"Copy",parentId:5});
queryAddRow(TestData,{id:7,nodeText:"Cut",parentId:5});
queryAddRow(TestData,{id:8,nodeText:"Paste",parentId:5});
queryAddRow(TestData,{id:9,nodeText:"Help"});
queryAddRow(TestData,{id:10,nodeText:"About",parentId:9});
function nested_tree( data, parentId = 0, depth = 0 ) {
var tree_node = [ ];
if ( depth > 3 ) throw 'That''s a lot of recursion!!';
for ( var row in data ) {
if ( ( len( row.parentId ) ? row.parentId : 0 ) == parentId ) {
var node = { nodeText: row.nodeText, children: nested_tree( data, row.id, depth + 1 ) };
if ( !node.children.len() ) node.delete( 'children' );
tree_node.append( node );
}
}
return tree_node;
}
writeDump( nested_tree( TestData ) );
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.