Overview
Application Scenario
A tree dataset is used to display hierarchical structures through drop-down or collapsible trees, such as the department hierarchy shown in the following figure.
If you bind a tree dataset to a widget, a tree-like hierarchical structure will automatically be generated when viewed on the web. How to convert the two-dimensional structure of a data table to this tree-like hierarchical relationship? This document will explain the transformation.
Applicable Data Table Structure
In FineReport, two types of table structures can be used to build tree datasets:
The following figure shows the table structure in which a series of hierarchical data contains regular ID numbers. For details about how to convert the table structure to a tree dataset, see Example one in this document.
The following figure shows the table structure which includes the current ID and the parent ID numbers. For details about how to convert the table structure to a tree dataset, see Example two in this document.
To convert the two-dimensional structure of a data table to this tree-like hierarchical relationship, FineReport provides the concept of a tree dataset. If you bind a tree dataset to a widget, a tree-like hierarchical structure will automatically be generated when viewed on the web.
Example one: Data with Regular ID Number Lengths
Dataset Creation
Enter the SQL statement SELECT DepartmentID,Department_name FROM CorporationDepartment to create a dataset named ds1, as shown in the following figure.
Tree Dataset Creation
1. Click the icon, and select Tree Dataset, as shown in the following figure.
2. Select Create Tree Depending on Length of Selected Dataset's Tag Field, and select DepartmentID in Original Tag Field, as shown in the following figure.
3. Click the Preview button, as shown in the following figure.
4. Click OK, and the tree dataset is created.
Example Two: Data with ID and Parent ID Numbers
Dataset Creation
1. Enter the SQL statement SELECT * FROM CorporationDepartment to create a dataset named ds2, as shown in the following figure.
2. Create a tree dataset as shown in example one. Select ds2, and tick Create Tree Depending on Parent Tag Field of Selected Dataset, as shown in the following figure.
Select DepartmentID in Original Tag Field, and select SuperioID in Parent Tag Field
3. Click the Preview button, and the effect is the same as that shown in example one. Click OK to save the tree dataset.
Usage Format
The tree dataset must be set in the fixed column structure of ID, Parent ID, and Name. The ID value can be a number or a string.
For data not in this format, SQL Union or recursive methods can be used to combine the results before placing them into the dataset.
The following example illustrates a format conversion for better understanding. The former data format displays the parent-child relationship between two columns.
The SQL must be converted before being included in the dataset