Creating a Tree Item
We will use the following design to create a tree item based on a query and show how to get the EMP details by EMPNO for each tree node selection:
- A block named TREE_BLOCK with an item of type Tree named HTREE can be used. This item can belong to the primary canvas of the console window. Drag the tree item to cover a vertical rectangle visible enough to picture a hierarchical structure.
- The operations of expand and collapse are simulated as a toggle when a particular node is expanded or collapsed by clicking on it.
- The operations of Expand All and Collapse All are simulated as a toggle when a particular node is expanded or collapsed. These are relative to the depth of this starting node.
- A control block named CTRL_BLK with four iconic push buttons PB_EXPAND, PB_EXPAND_ALL, PB_COLLAPSE, and PB_COLLAPSE_ALL for the expand, expand all, collapse and collapse all operations on selected Tree nodes can be used. Expand and collapse are explained in the second point; Expand all and Collapse all are provided by clicking each node. These functions are also provided by means of buttons to give greater flexibility to the user .
- A data block EMP gets the details for the node selected EMPNO.
Begin by initializing the tree item with a data query based on a SELECT with CONNECT BY and START WITH clauses. This has to be specified in the Data Query property of the tree item:
SELECT -1, LEVEL, ename, NULL, TO_CHAR(empno) FROM emp START WITH mgr IS NULL CONNECT BY PRIOR empno = mgr;
This query lists all the organizations along with their reporting (parent) organizations at one level above them.
The data columns in the above query are selected as follows :
- The first column is to specify whether the tree is expandable or not. 1 is Expand to a Depth of All Levels Possible, 0 is Show Only One Level, and “1 is No Expand.
- The second column is self-explanatory ”the LEVEL pseudo column for any hierarchical query.
- The third column is the text that will appear on a node when it is clicked.
- The fourth column is an icon name that will appear by the side of a node.
- The fifth column is the actual data value held by a node. The node value always returns a character value.
Next, populate the tree item on form startup. Write a WHEN-NEW-FORM-INSTANCE trigger using FTREE.POPULATE_TREE :
DECLARE item_id Item; BEGIN item_id := FIND_ITEM(''TREE_BLOCK.HTREE''); IF NOT ID_NULL(item_id) THEN FTREE.POPULATE_TREE(item_id); END IF; END;
Simply specifying a data query does not automatically populate the tree.
Tip
Tree items should belong to single-row blocks, and the tree item should be the only item in the block.