Self-hierarchy is a state where one row in your data is related to another row through a parent/child relationship. The common example is managers and employees – managers are also employees so you will use a single employees business object to store all the levels of the organization, and then have a field that points from each employee to who is the other employee that is their manager. If you then want to show this hierarchy you would probably looking to show it as a tree. I blogged in the past an example of working with trees leveraging parent and child objects, but in a self-reference scenario there are a few more tricks and techniques you would need to use. Here are the basic steps:
Manage the Data Model
After you create the reference field (manager field in the employees example), you will want to edit the relationship to also expose the accessor that will let you fetch the employees of each manager (beyond the manager of each employee).
This will let you drill down only one level of data. So the next step is to edit the endpoints we expose and specify how many levels down the tree we want to let people drill.
Now developers accessing your object through the REST endpoint will have the opportunity to drill down as needed.
Adjust the REST Call
In the case of business objects one way you can specify that you want to drill down the hierarchy is to pass in the list of fields you want to fetch. In our example this will look something like this for fetching the id and name for 3 levels of employees:
id,name;employeesCollection:id,name;employeesCollection.employeesCollection:id,name
In the video demo we let VB generates this for us by adding a table to a page and using the field selection quick start.
Another adjustment we might want to do is fetch only the top level of the tree in the UI, and allow drilling from there. To do that we add a q parameter that will limit the rows returned to only those who don't have a manager so:
manager is null
Set the UI Tree
From now on, the process is similar to my older tree blog. You add the oj-tree-view component and item template to your UI.
Then create an action chain that starts with calling the REST endpoint, passing the results array to a JavaScript function that transforms the array into an arrayTreeDataProvider object that you assign as the data attribute of the oj-tree-view.
You can see the full development process in this tutorial video:
