Creating trees

In this recipe, we will see how we can display a tree in our Web Dynpro application. We will create a tree that will display the employee department-wise data, that is, nodes showing department names, each of which when opened will display a list of employees within the department along with the employee names.

For the sake of this recipe, we assume that we have an internal table DATA_TAB comprising of three fields, department, pernr, and sname containing the department name, employee number, and the employee names respectively sorted according to the department name.

Creating trees

How to do it...

We will now follow the steps as shown:

  1. We first define a context node TREE_NODE. The TREE_NODE context node has a cardinality of 0..N and the Singleton property should be Off. An IS_OPENED attribute will be based on the type WDY_BOOLEAN.
  2. Within the TREE_NODE context node there is another node, TREE_NODE_ITEM, which is also a non-singleton node having cardinality 0..N. An attribute ITEM_TEXT is created within the TREE_NODE_ITEM node. The ITEM_TEXT attribute has a type STRING.
    How to do it...
  3. We will then define a TREE UI element in our view layout. We create a TREE UI element along with two subelements. These are based on TreeNodetype and TreeItemtype, having the names MY_NODES and MY_ITEMS, respectively.
    How to do it...
  4. We bind the TREE UI element datasource property with the TREE_NODE context node. For MYNODES, the text property is bound with the TREE_NODE.TREE_NODE_TEXT attribute. Likewise, for MYITEMS, the text property is bound with the TREE_NODE.TREE_NODE_ITEM.TREE_ITEM_TEXT attribute. The IS_OPENED attribute of the context node is assigned to the expanded property of the MYNODES subelement.
  5. Next, we will write the code into the WDDOINIT method of the view created. We define reference variables for the context nodes root_node, TREE_NODE, and ITEM_NODE. We also define data variables pertaining to the ELEMENT_TREE_NODE element and the ELEMENT_TREE_NODE_ITEM element.
    How to do it...
  6. We then get the reference to the TREE_NODE context node by calling the GET_CHILD_NODE method. The reference is stored in root_node.
    How to do it...
  7. Next, we populate the context nodes with appropriate data that may be displayed in the tree.
  8. We run a loop at the DATA_TAB, the table that contains the department name with employee name and numbers. We place a AT NEW DEPARTMENT statement within which we assign the department name to the NODE structure corresponding to our TREE_NODE context node. We then call the BIND_STRUCTURE method of the root_node context node and supply the data to be added to the node's element collection. We then get the reference to the TREE_NODE_ITEM child context node and store it in the ITEM_NODE variable (to be used later within the loop).
  9. Outside the AT .. ENDAT block, we use the CONCATENATE statement in order to combine the personnel number and name and store it in the ITEM-ITEM_TEXT variable. This is then added to the ITEM_NODE context node's collection using the BIND_STRUCTURE method.
    How to do it...

How it works...

We create a TREE UI element on the view layout. We create two subelements for the TREE element; one is the TreeNodetype type and the other TreeItemtype type. The MYNODES subelement (of type TreeNodetype) and MYITEMS subelement (of type TreeItemtype) correspond to the department names, and the employee names and number respectively in the final display. Appropriate bindings are defined for MYNODE and MYITEMS with relevant context data node attributes.

A loop is used to populate the data for the TREE_NODE context node. We kept the IS_OPENED attribute as a space while populating the data for the TREE_NODE context node and its child node TREE_NODE_ITEM. When displayed, the department nodes are not expanded. When the application is run, within the browser, we see a list of departments as shown in the following screenshot:

How it works...

When the relevant nodes are expanded, the employees within the departments are displayed along with their number and name, as shown in the following screenshot:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.37.126