This post is part of a series on code snippets. The complete list of posts in the series is available in the document Code Snippets: A Blog Series.
In a view’s layout, individual MenuItems may be added as child elements to a Menu, ButtonChoice or LinkChoice parent element, but what if the list of menu options is not known at design time? An example is needing to present the user with a ButtonChoice that lists employees who report directly to the user. This list will be different for each user and must be read from a database table, so these menu options cannot be created at design time.
The Menu, ButtonChoice or LinkChoice element may be created in the view’s layout at design time and its child elements created dynamically at runtime, for example in the view’s WDDOINIT hook method. The example below creates MenuActionItems for a ButtonChoice. The same approach may be used to dynamically create menu items for a Menu or LinkChoice UI element.
DATA lo_view TYPE REF TO if_wd_view. DATA lo_buttonchoice TYPE REF TO cl_wd_button_choice. DATA lo_menuactionitem TYPE REF TO cl_wd_menu_action_item. * Get a reference to the ButtonChoice view object lo_view ?= wd_this->wd_get_api( ). lo_buttonchoice ?= lo_view->get_element( 'BUTTONCHOICE_ID' ). * Add a MenuActionItem to the ButtonChoice for each record in the source data table LOOP AT lt_data ASSIGNING <data>. CALL METHOD cl_wd_menu_action_item=>new_menu_action_item EXPORTING id = <data>-id on_action = 'VIEW_ACTION' " action to be executed upon selection of the menu item text = <data>-text RECEIVING control = lo_menuactionitem. CALL METHOD lo_buttonchoice->add_choice EXPORTING the_choice = lo_menuactionitem. ENDLOOP. " <data>