How to implement a "Context Menu" in ALV Tree?

Context menu is triggered when the user pressed the right mouse key or SHIFT F10 (see screenshot below). It allows user to select list of functions dynamically defined. It may contain any functions, and is not restricted to function keys. Context menu can be assigned to the following screen elements namely: Input/output fields, Text fields, Table controls, Group boxes, subscreens pushbuttons, checkboxes, or radio buttons. 

In this tutorial, I will show to you how to add a context menu in ALV Tree.By the way, I am using here OOP approach. So if you dont have any background in OOP, I hope you have time to read some OOP materials so that you have an in-depth background for this implementation. Otherwise, you will not appreciate such tutorial.

1. Define a local class with a method handle_node_cm_req and handle_node_cm_sel.

2. In the method, handle_node_cm_req, define here the function list you want to appear as context menu. Use the method add_function to add the different context menus.

Also you can use the method add_separator to show a horizontal grayed bar between menus. Position this in between context menus.

3. In the method, handle_node_cm_sel, this will handle the PAI of the object. So once the user click on the menu, this method will be triggered and catches the sy-ucomm. For demonstration purposes, I haven't added any logic. Its up to you what to logic to add depending on your requirement.

4. Once the methods of the class is set-up, go to the PBO of your main screen where the ALV tree will be displayed. In OOP, ALV tree or ALV grid is displayed using the method set_table_for_first_display. So after this method is called, you need to register the events for the ALV. For context menu, you need to pass the attribute EVENTID_NODE_CONTEXT_MENU_REQ from the class cl_gui_column_tree. 

Then pass the internal table to the method set_registered_events.

5. Finally, register the context menu event on the backend for event handling using SET HANDLER commands. 

6. Once done, activate your program and try to run. Your ALV tree must look something like this.

I hope you enjoy reading my tutorial again and wishing to hear from you. By the way, there are other approach on how to implement a context menu using the PF-STATUS. On my succeeding post, I will discuss this subject matter for everyone.

Enjoy :)

No comments:

Post a Comment