Creating navigation lists

In this recipe, we will see how we can display data in the form of a navigation list. The navigation list is similar to a tree. However, the nodes appear in expanded form when displayed and may not be compressed. The entire navigation list may be expanded and compressed (however, not the data nodes within it).

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

Creating navigation lists

How to do it...

We will now see the steps required to create a navigation list:

  1. We will first create the context node related to the navigation list in the component controller. We create a context node by the name NAVLIST.
    How to do it...
  2. The Init Lead Selection field must be set to No (since, in this case, we do not require lead selection and its initialization). The Singleton property of the node must be set as No. The Cardinality field is set as 0..n.
  3. We then add three attributes to the context node NAVLIST, namely DISPLAYED_ TEXT, PERNR, and IF_SELECTED based on the type STRING, PERSNO, and WDY_BOOLEAN, respectively.
  4. We then add a recursive node to the NAVLIST context node and name it NAVLISTREC. Right-click on the NAVLIST context node and from the context menu that appears, navigate to Create | Recursive Node.
    How to do it...
  5. We then drag-and-drop the context node NAVLIST from the component controller to the relevant view controller (define the mapping).
  6. Next, we will create the navigation list UI element on the Web Dynpro view. Right-click on the ROOTUIELEMENTCONTAINER option from the right pane of the layout editor and choose the context menu option Create Element. Enter Navigation in the ID field and choose the type NavigationList in the pop up that appears.
    How to do it...
  7. Right-click on the NAVIGATION node in the right pane of the layout editor and choose the insert header option from the context menu that appears. This will add the EXPANDABLETITLE header to the navigation list.
    How to do it...
  8. Enter the text Department Wise List in the title property.
    How to do it...
  9. For the defined navigation list, we will set the itemSource property as the MAIN.NAVLIST context node defined earlier. For the itemText property, we set the value as the DISPLAYEDTEXT attribute of the context node.
    How to do it...
  10. Once this is done, we will see the navigation list added in our layout editor, as shown in the following screenshot:
    How to do it...
  11. The next step is to write the code for populating the context node with the appropriate department data that is to be shown in the navigation list.
  12. Appropriate variables are first defined for the navigation list node (NODE_NAVLIST) and the navigation list recursive context node (RECNODE).
  13. Also ELEMENT_NAVLIST is defined as a reference to a context element using interface IF_WD_CONTEXT_ELEMENT.
    How to do it...
  14. The next step is to write the code for populating the data within the navigation list. We run a loop at the DATA_TAB internal table that contains data of the department and the personnel number and names.
  15. At the beginning of a new department, we use the AT NEW DEPARTMENT statement to add data to the context node NAVLIST.
  16. The GET_CHILD_NODE method is called to get a reference to the NAVLIST context node; the reference is stored in the NODE_NAVLIST navigation list node. The NAVLIST structure is assigned the department name. The IS_SELECTABLE property is kept as false.
  17. The BIND_STRUCTURE node is then used to add the department name to the NAVLIST context node element collection. The reference to the added element is returned and stored in ELEMENT_NAVLIST. The GET_CHILD_NODE method is then called to get the reference to the NAVLISTREC recursion node for the element referred to by the ELEMENT_NAVLIST variable (this reference is stored in RECNODE) to be used later.
  18. Outside the AT .. ENDAT block is the area that is executed for all rows in the data internal table DATA_TAB. This area pertains to the employee data (employee name and employee). We use the NAVLIST structure in this block.
  19. We assigned the value ABAP_TRUE and the name and employee number to the IS_SELECTABLE property to be displayed in the DISPLAYED_TEXT field. The BIND_STRUCTURE node is then used for adding the employee data to the NAVLISTREC recursive node using the RECNODE reference variable.
    How to do it...

How it works...

We first created a NAVLIST context node containing a NAVLISTREC recursion node. We then inserted a navigation list UI element on the view layout. Appropriate binding between the navigation list UI element and the context nodes was defined. In the code, we formed the higher nodes in the navigation list displaying departments (at the beginning of a new department within the data table DATA_TAB). We then added child nodes (to the department nodes) displaying the employee names and number contained within the department.

The employee names within the department are selectable but may not be compressed (the IS_SELECTABLE property is set to TRUE in the code). This navigation list is displayed as shown in the following screenshot:

How it works...

There's more...

We can further enhance the application by activating the selection feature of the selectable nodes, that is, displaying the details of an employee selected on the user's screen. For this, we first need to enter a suitable name in the Onselect property of the navigation list in the layout editor. Then double-click in order to write the code. The GET_CHILD_NODE method will be called when a particular selectable row is clicked by the user. The handler method will be called when the SELECT event is raised.

This handler method contains one parameter, WDEVENT. In order to determine which employee number and name has been selected, we will add a new parameter, SEL_NODE, having type IF_WD_CONTEXT_ELEMENT. We then need to call the GET_STATIC_ATTRIBUTES method for the SEL_NODE parameter in order to retrieve the details of the selected node (including the employee number pernr).

..................Content has been hidden....................

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