Skip to content

Instantly share code, notes, and snippets.

/trycf-gist.cfm

Created Aug 29, 2016
Embed
What would you like to do?
TryCF Gist
<cfscript>
function makeATree( testData ) {
var parents = { "" = {} };
for ( var row in testData ) {
parents[ row.id ] = { "nodeText" = row.nodeText };
var parent = parents[ row.parentId ];
param parent.children=[ ];
parent.children.append( parents[ row.id ] );
}
return parents[ "" ].children;
}
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});
writeDump( makeATree( 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.