# DBTreeView

**URL:** <https://forum.servoy.com/t/dbtreeview/8909>\
**Category:** Classic Servoy\
**Created:** [March 22, 2008, 1:35am UTC](https://forum.servoy.com/t/dbtreeview/8909 "2008-03-22T01:35:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![full\_noize](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@full\_noize](https://forum.servoy.com/u/full_noize)\
**Post date:** [March 22, 2008, 1:35am UTC](https://forum.servoy.com/t/dbtreeview/8909/1 "2008-03-22T01:35:36Z")

</div>

Hello,

Is there a user guide for the various beans that ship with Servoy? I am a new Servoyian and am wasting a lot of my time trying to work out what, for example, is the difference between setNrelation and setMrelation in the DBTreeView. I would be able to work this sort of stuff out a lot quicker with instructions. There appears to be nothing in the Deleoper Guide or the User Guide about the beans that Servoy comes with.

What I’m currently wanting to do is create a treeview based on one table with Parent,Child,Child (3 levels). The table has company\_id, division\_id/parent-id, subdivision\_id/subdivisionparent\_id.

The problem is there doesn’t seem to be any information around that can tell me if this is even possible with one table. From the topic at [http://forum.servoy.com/viewtopic.php?t … dbtreeview](http://forum.servoy.com/viewtopic.php?t=9957&highlight=dbtreeview) it is possible to have 3 levels with a multitable.

I feel like I’m stumbling around in the dark! Can someone help turn on the lights? Any help would be appreciated! ![:roll:]( "Rolling Eyes")

---

<div class="post-metadata">

**Author:** ![axterics](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@axterics](https://forum.servoy.com/u/axterics)\
**Post date:** [March 24, 2008, 10:33am UTC](https://forum.servoy.com/t/dbtreeview/8909/2 "2008-03-24T10:33:17Z")

</div>

I’m very interested too. And another thing, selected a parent node is there a method to return all the childs node (recursively in a multi-depth tree)?

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [March 25, 2008, 8:00am UTC](https://forum.servoy.com/t/dbtreeview/8909/3 "2008-03-25T08:00:23Z")

</div>

Hi,

Beans come in three variants:

- Beans that ship with Servoy and that are specifically build for Servoy, like the DBTree(TableView) bean and the InMemDatagrid bean
- Beans that ship with Servoy but are not specifically addapted for Servoy (all the J\* beans, several charting beans)
- 3rd party beans, like the Calendar bean from it2be or the Tableview bean from Dr. Maison for example).

The first category ought to be documented by Servoy. the second category we ship with Servoy, but is not activly supported. Documentation on these beans is everywhere on the internet. For documentation on beans in the third category you need to turn to your bean vendor.

When talking specifically about the DBTreeView bean: this bean has seen some major enhancements in several of the 3.5.x releases. While these are maintenance releases, we did not want to hold back the improved DBTreeView bean untill Servoy 4.0.

Since our documentation is only updated with major releases (so the upcomming 4.0), the documentation on the latest release of the DBTreeView bean is poor. Therefor, we posted the sample solution + comments on the forementioned thread.

with regards to your specific questions: The sample solution on the aforementioned thread shows you how to build a tree from records in one table using 1 and the same relation and also an example how to build a tree using different relations.

It soulnds like you need to create 2 relations: parent\_to\_subdivision and subdivision\_to\_subsubdivision.

If you compare your setup with the multitable example in the sample solution the only difference is that in your scenario, the 2 different relations point to the same table, whereas int eh sample solution they point to the same table. Technically, it doesn’t matter where the relation points. As long as you have created a binding for the table(s).

> And another thing, selected a parent node is there a method to return all the childs node (recursively in a multi-depth tree)?

You can set the onClick method for when a node is clicked and have an argument send into the method you set. From there, you should be able to get all the childNodes yourself in scripting. There is no function on the bean to do that for you.

Paul

---

<div class="post-metadata">

**Author:** ![full\_noize](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@full\_noize](https://forum.servoy.com/u/full_noize)\
**Post date:** [March 26, 2008, 12:28am UTC](https://forum.servoy.com/t/dbtreeview/8909/4 "2008-03-26T00:28:58Z")

</div>

Paul,

Thanks for your post.

> It soulnds like you need to create 2 relations: parent\_to\_subdivision and subdivision\_to\_subsubdivision.
> 
> If you compare your setup with the multitable example in the sample solution the only difference is that in your scenario, **the 2 different relations point to the same table, whereas int eh sample solution they point to the same table.** Technically, it doesn’t matter where the relation points. As long as you have created a binding for the table(s).

If the relations are created as required (parent\_id to division\_id and division\_id to subdivision\_id), how does the bean know what the relation is, in a single table example, between parent and division, division and subdivision when you are creating a binding to one table only and in the post on how to use the bean, you say _“You can bind the bean to mulitple tables but a table can only be binded to the bean once.”_ It would appear that you can only provide one relation with one table and hence get only one child node, whereas when you create a binding with multiple tables, the relation between the tables is ‘figured out’ by the bean’s coding and voila, you get the related child nodes.

If you can do a 3 level tree from one table then I’ve missed something here! ![:oops:]( "Embarassed")

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [March 26, 2008, 10:42am UTC](https://forum.servoy.com/t/dbtreeview/8909/5 "2008-03-26T10:42:17Z")

</div>

You can let an (unstored) calculation return the relation name, so then each node can use a different relation.

With regards to the binding: in your scenario, you use 1 table, so you bind the bean only once to that table.

Using (unstored) calculations, you can make it as dynamic as you want.

For example, you set the relation name dataprovider to a calculation named “my RelationNameProviderCalc”. So, that is your binding for this table. For each node through, this calculation is called to get the relationName and the calc can return a different value for each node.

In your scenarion, you do need to be able to determine on row level what type of records you are dealing with, so you can return the proper relation name.

Does this make sense a bit?

Paul

---

<div class="post-metadata">

**Author:** ![full\_noize](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@full\_noize](https://forum.servoy.com/u/full_noize)\
**Post date:** [March 27, 2008, 1:23pm UTC](https://forum.servoy.com/t/dbtreeview/8909/6 "2008-03-27T13:23:42Z")

</div>

Paul,

What you say makes sense and I suspect it’s the syntax of the method that is letting down my efforts.

So we all can relate to this topic, I have used your TreeBeanSample and added the following columns to the CRM database ‘Companies’ table:

division\_id  
subdivision\_id  
subdivparent\_id.

I retained the ‘companies\_to\_companies’ relation which is company\_id to parent\_id (this gets the child nodes in the top left tree in the solution (company\_tree).

I added the relation ‘companies\_to\_companies\_divtosubdiv’ to create the child nodes from division to sub division. All aforementioned relations are within the companies table.

I then created the following unstored calc as you suggested, called ‘relation\_name’:

if ( parent\_id != null && subdivparent\_id == null )  
{  
return ‘companies\_to\_companies’;  
}  
else if (parent\_id != null && subdivparent\_id != null)  
{  
return ‘companies\_to\_companies\_divtosubdiv’;  
}

I put some test data in and then tried many ways to try and get the nodes to render with no success. I have used the bottom left tree of your solution as the test and this is last variant of code I have tried in the ‘onLoad’ method:

//Init Multi Table Tree

\_binding = elements.multiple\_related\_tables\_tree.createBinding(controller.getServerName(), controller.getTableName());  
\_binding.setNRelationDataprovider(‘relation\_name’);  
\_binding.setImageURLDataprovider(‘type\_icon’);  
\_binding.setTextDataprovider(‘company\_name’);

\_binding = elements.multiple\_related\_tables\_tree.createBinding(controller.getServerName(), controller.getTableName());  
\_binding.setNRelationDataprovider(‘relation\_name’);  
\_binding.setImageURLDataprovider(‘type\_icon’);  
\_binding.setTextDataprovider(‘company\_name’);

controller.find();  
parent\_id = ‘^’;  
subdivparent\_id = ‘^’;

controller.search(true,true);  
elements.multiple\_related\_tables\_tree.addRoots(foundset);

This method gets the parents OK but renders no children (I gues you could say the tree is barren… ![:D]( "Very Happy") ) The icons render OK, with different pretty colours for the different company\_type\_id.

The top left (company\_tree) renders the division nodes, but that is it.

As a new Servoyian, I have to say that this TreeBean has proved harder to master than any of the J\* beans I have tried so far (I guess the internet helps here) and all the SQL stuff. I hope your reply may help others with this bean!

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [March 31, 2008, 8:06am UTC](https://forum.servoy.com/t/dbtreeview/8909/7 "2008-03-31T08:06:55Z")

</div>

I think the problem is in your relation\_name calc.

```auto
if ( parent_id != null && subdivparent_id == null ) 
{ 
return 'companies_to_companies'; 
} 
else if (parent_id != null && subdivparent_id != null) 
{ 
return 'companies_to_companies_divtosubdiv'; 
}

```

The rootNodes do not have a parent\_id filled, because you (I think correctly) set all records without a parent\_id filled as rootnodes with the code below:

```auto
controller.find(); 
parent_id = '^'; 
subdivparent_id = '^'; 

controller.search(true,true);

```

In you calc through, you only return a relationName if the parent\_id is not null, to no relationName is ever returned for the RootNodes, so therefor you get a barren tree ![:)]( "Smile")

with regards to the DBTree bean being harde to master than JTree (for example): Well, partly that is because the documentation is not there yet, but on the other hand the DBTree beans is a lot more powerfull than the JTree, with it’s automatic databinding and such.

Paul

---

<div class="post-metadata">

**Author:** ![full\_noize](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@full\_noize](https://forum.servoy.com/u/full_noize)\
**Post date:** [April 2, 2008, 6:47am UTC](https://forum.servoy.com/t/dbtreeview/8909/8 "2008-04-02T06:47:26Z")

</div>

Thank you Paul, that was enlightening. I got it working with a simple NrelationName populating 3 levels fine. The reason I wanted a 2 relation tree from the one table was simply due to the stucture of the existing SQL data I’m working with that has the 2 relation structure. I’ll work with what I’ve got and see what happens!

It is a good thing that bean - actually very simple until you try and make it too complicated. ![:lol:]( "Laughing")

---

<div class="post-metadata">

**Author:** ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)\
**Post date:** [April 2, 2008, 7:19am UTC](https://forum.servoy.com/t/dbtreeview/8909/9 "2008-04-02T07:19:27Z")

</div>

Glad you got it working! ![:D]( "Very Happy")

Paul
