function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Colin LoretzColin Loretz 

Tree/List with unknown levels

Hey everyone,

I'm trying to build a tree component and am able to go down two levels but I'm not sure how (or if) it would be possible to continue adding levels.

I'm already building the above tree with the following code:
public ProjectTask__c [] getTasks()
  return [Select Id, Name, (Select Id, Name from Tasks__r)  from ProjectTask__c where Project__c = :projectId];

VF Page:
<ul id ="tree" class="treeview">
 <apex:repeat value="{!tasks}" var="t">
  <li><span class = "file"><a href = "/{!}">{!}</a></span>
    <apex:repeat value="{!t.Tasks__r}" var="st">
     <li><span class = "file"><a href = "/{!st.Id}" />{!}</a></span></li>

 If I have any items below 1.1 or 2.1, such as 1.1.1,, etc. what would be the best way to build a list of this nature?



 I would not recommend your approach as you will not be able to dynamically add tags to your page based on multiple levels of the tree.
I would suggest to create a javascript API for your tree or you may be able to use one you already have or can get from the internet and then using your component's Controller inject javascript function calls to your page to render the tree on the screen.

This way you can do recursive method calls on the controller side to render your tree branches.


Colin LoretzColin Loretz
I think I follow you on this one. I thought recursion would be best but I'm not very strong in programming recursion.

I'm going to give the YUI treeview another shot because I have seen it work for Ron Hess in one of his demonstrations.

Thanks for the tip.

Hello Colin,


I am working on an exactly similar requirement wherein i need to show the levels in a tree dynamically.


Were you able to get a solution for this?


Would be really obliged if you can help me on this!!


Many Thanks,



I too am working on something similar to this. I'll post again if I come up with anything cool.






I am trying to build a tree view in which i want to display territory name and when we click on territory name it should expand and show users in that territory. Please help me out with this. I have a very aggressive timeline


To build a recursive tree, you'd have to build a treeNode component that calls itself recursively. Basically, it would look like this:



<apex:component controller="nodeController" selfClosing="true"> <apex:param name="nodeId" type="string" assignTo="{!nodeId}" /> <apex:commandButton action="{!togglecollapse}" /> <apex:outputText value="{!nodeText}" /> <apex:repeat value="{!nodeList}" var="node" rendered="{!nodeCollapsed}"> <c:treeNode nodeId="{!node.nodeId}" collasped={!node.collasped}" /> </apex:repeat> <apex:repeat value="{!leafList}" var="leaf"> <apex:commandLink action="{!leaf.action}" value="{!leaf.label}" /> </apex:repeat> </apex:component>


 You'd have to flesh out the action commands and controller, but that's basically what you'd need to do.



Are you sure you can do recursive components? I've tried to do this in the past but always got a "Save error: Failed Validation: ApexComponent" when I tried to do recursion with components.



Actually, I've never tried it. I'm all booked for today, but I'll check it out tonight when I get a moment, unless someone has time to do it earlier than that. All I know is that is the theory one would use to make such a thing.



I just now tried recursing Apex components and I also get a 'Validation Failed' error upon compile.  So nothing's changed on that front.


In an attempt to be clever, I created a component wrapper and used that to indirectly recurse - exact same desired result but now the component doesn't have to explicitly refer to itself.   But when I try to render the page that contains the component, it tells me "An internal server error has occurred", which I take to mean that I'm out of luck.


Time to start thinking about a JS implementation.........