+ Start a Discussion
Cool_DevloperCool_Devloper 

Tree Structure Display on VF page

Hello Friends, I am stuck with an issue related with diplaying records in a hierarchy. I need to display the fetched records from the controller in a Tree Structure dynamically. The number of levels is not fixed here. Has anyone worked on a similiar requirement? Is there a way i can achieve this?? Please Guide!! Thanks, Cool_D
Best Answer chosen by Admin (Salesforce Developers) 
aebenaeben

I have not done this. But have done something similar

 

You need to use a Wrapper and have actions on nodes that can be expanded and update the tree object everytime you load something.

 

Something like this:

 

public List<Tree> accountTree {get; set;} 

class Tree

{

public accountName {get; set;} 

public List<Contact> contacts {get; set;} 

 

If you were to do it in a table:

 

<apex:pageBlockTable value="accountTree" var="a">

<apex:columns>{!a.accountName}</apex:column> 

  <apex:columns>

<apex:pageBlockTable value="{!a.contacts}" var="c">

//Display call contacts 

</apex:pageBlockTable> 

</apex:column> 

</apex:pageBlockTable> 

 

Hope this gets you going. 

All Answers

aebenaeben

I have not done this. But have done something similar

 

You need to use a Wrapper and have actions on nodes that can be expanded and update the tree object everytime you load something.

 

Something like this:

 

public List<Tree> accountTree {get; set;} 

class Tree

{

public accountName {get; set;} 

public List<Contact> contacts {get; set;} 

 

If you were to do it in a table:

 

<apex:pageBlockTable value="accountTree" var="a">

<apex:columns>{!a.accountName}</apex:column> 

  <apex:columns>

<apex:pageBlockTable value="{!a.contacts}" var="c">

//Display call contacts 

</apex:pageBlockTable> 

</apex:column> 

</apex:pageBlockTable> 

 

Hope this gets you going. 

This was selected as the best answer
Cool_DevloperCool_Devloper

Thanks a ton aeben!!

 

I think i got what you suggested. I should have thought about the wrapper a lot brefore :(

 

Anyways, Thanks

Cool_D 

Cool_DevloperCool_Devloper

Aeben,

 

I was able to display the hierarchy on click of expand button, but i am unable to do it for more then 2 levels.

 

What can i do to handle this dynalic level display?? 

 

Waiting for some solution :)

 

Many Thanks,

Cool_D 

aebenaeben
What do you mean dynamic level display? Give me an example.
Cool_DevloperCool_Devloper

Hello Aeben,

 

Well the display i want is exactly like the Roles Display of the standard salesforce Role Hierarchy which comes like a tree structure and you can expand the nodes at each level.

 

So, in my case also, i need to build the tree as and when the user either expands or collapses a node.

 

Hope i am clear here!!

 

Thanks,

Cool_D

aebenaeben

The roles hierarchy display data from the same object where both a parent and the child node is of same type. If you are doing something like this, your wrapper might look something like this.

 

class Tree

{

public Role parent {get; set;}

 

public List<Tree> children {get; set;}

 

If you want to display an object and its relationships, then it gets a little complex. For Example, a relationship hierarchy such as Teacher - Course - Students. If you are thinking of doing something like this, then  the best way to go about it is to just display the name (and nothing else) and use a generic wrapper like this.

 

class Tree

{

public SObject parent {get; set;} //upcast an object to SObject. Can hold Id and Name.

 

public List<Tree> children {get; set;}

}

 

This way you can have just one wrapper and get the job done. Otherwise, you will create multiple wrappers and could easily get  out of control.

 

 

When you create wrappers like any of the above, your VF might have problem rendring it. So you might have to write some helper methods (or wrapper method) in your controller to do the translation.

 

Hopefully I didn't confuse you.

Cool_DevloperCool_Devloper

Thanks a ton aeben for such an elaborate explanation. It really helps a lot!!

 

Well, you did not confuse me ;)

 

I am ok with the wrapper here but will it work for getting all the levels. I used it and could get all child records related to a parent. So, basically i was able display the top parent and all its childs. But could not get the hierarchy beyond that :(   (for each level)

 

And then comes the rendering issue. What do you exactly mean by translation here? How can methods help me in rendering??

 

I am sorry for my ignorance but it seems i long way to go :(

 

 

Many Thanks,

Cool_D

aebenaeben

The generic wrapper should work for getting all the levels.

 

What do you mean you couldn't get the hierarchy beyond the second level? Query issues?

 

I agree with you that rendering is the biggest challenge. Because VF cannot satisfy all your requirments. If you find something that cannot be done using the VF tag library, you can use HTML directly.