Copyright 2024 - BV TallVision IT

To make an ALV report interactive, the class needs a local class definition for it's menu handling and cell clicks. Cell content can be set up as hotspot, icon hotspot or button. ALV also supports line or column selection, for which the selecton results can be made available when a menu item is clicked.

An example of the output for this demo - an ALV report with hotspot's, double click response and menu response (although for the menu response, you would need to create your own menu (by copy from the SAP standard) and add your own action code (DO_IT). Instructions on how to do this follow below.

For this setup to work, we will need to introduce an event manager class - which is invoked when the end user does something we want to respond to. A field click, a double click or the selection(click) of a menu option. Let's start with the definition of the event manager, lcl_event_manager:

report  ZDEMO_ALV_4_INTERACTIVE.

include ZABAPCADABRA_EASY_ALV.
types: begin of lty_mara,
         matnr type mara-matnr,
         something type char20,
       end of lty_mara.

class lcl_event_manager definition deferred.
data: gt_data type table of lty_mara,
      gw_data type lty_mara,
      go_alv type ref to lcl_easy_alv,
      go_salv type ref to cl_salv_table,
      go_event_man type ref to lcl_event_manager.

Now define and implement and event manager class. For menu options (the user clicks on a menu item), method on_user_command, link clicks and click links have their own methods in which the row and column is made available.

In the following coding block, the menu MAIN_MENU is referred to. This menu is copied from the all standard SAP version: SAPLSLVC_FULLSCREEN menu STANDARD_FULLSCREEN. Use the menu painter to get your copy: start transaction SE41 and click on "Copy status"(button) (Ctrl-F6). Note that after copying a status, it needs to be activated on your report. Note that you can also leave the menu where it is (on report SAPLSLVC_FULLSCREEN) and invoke it from your report, which is what was done in this demo. 

*-------------------------------------------------------------
* CLASS lcl_event_manager DEFINITION
*-------------------------------------------------------------
* An example subclass for processing events
*-------------------------------------------------------------
class lcl_event_manager definition.

  public section.
    methods: constructor importing r_object
               type ref to cl_salv_table,
             on_user_command for event
               added_function of cl_salv_events
               importing e_salv_function,
             on_double_click for event
               double_click of cl_salv_events_table
               importing row column,
             on_link_click for event
               link_click of cl_salv_events_table
               importing row column.
  private section.
    data: go_salv type ref to cl_salv_table.

endclass.                   
*-------------------------------------------------------------
* CLASS lcl_event_manager IMPLEMENTATION
*-------------------------------------------------------------
* Allow response for double clicks and user commands (menu
* actions)
*-------------------------------------------------------------
class lcl_event_manager implementation.
  method constructor.
    data: lo_events type ref to cl_salv_events_table.

    go_salv = r_object.
    lo_events = go_salv->get_event( ).
    set handler on_user_command for lo_events.
    set handler on_double_click for lo_events.
    set handler on_link_click for lo_events.
  endmethod.
  method on_user_command.
    data: lt_rows type salv_t_row,
          lv_row type int4,
          lv_footer_text type string,
          lo_selections type ref to cl_salv_selections.

    case e_salv_function.
      when 'DO_IT'. "<= this could be on your own menu definition
* Get the selected entries
        lo_selections = go_salv->get_selections( ).
        lt_rows = lo_selections->get_selected_rows( ).
        loop at lt_rows into lv_row.
          read table gt_data index lv_row into gw_data.
          gw_data-something = '*processed*'.
          modify gt_data index lv_row from gw_data.
        endloop.
        describe table lt_rows lines lv_footer_text.
        concatenate lv_footer_text 'rows processed'
          into lv_footer_text separated by space.
        go_alv->set_footer_line( col1 = lv_footer_text
          reset_footer = abap_true ).
        go_alv->display( refresh = abap_true ).
    endcase.
  endmethod.                    "on_user_command
  method on_double_click.
    read table gt_data index row into gw_data.
    gw_data-something = '*doubleclicked*'(004).
    modify gt_data index row from gw_data.
    go_alv->set_footer_line( col1 = 'Double click'
      reset_footer = abap_true ).
    go_alv->display( refresh = abap_true ).
  endmethod.                    "on_double_click
  method on_link_click.
    go_alv->set_footer_line( col1 = 'Hotspot click'
      reset_footer = abap_true ).
    go_alv->display( refresh = abap_true ).
  endmethod.                    "on_link_click
endclass.

And finally, get some data to "work on" and the actual ALV object create and use

start-of-selection.

  select * from mara into corresponding fields of table gt_data
    up to 30 rows.

  create object go_alv.
  go_alv->set_alv_from_template(
    exporting menu_report = 'SAPLSLVC_FULLSCREEN'
              menu_pfstatus = 'STANDARD_FULLSCREEN'
    changing content = gt_data
             salv_table  = go_salv ).

  go_alv->set_selection_mode( 2 ).
  go_alv->set_sort( fieldname = 'MATNR' ). "2nd sort field
  go_alv->set_field( fieldname = 'MATNR'
    is_hotspot = abap_true
    is_key = abap_false ).
  go_alv->set_footer_line( '30 lines from material table' ).
  create object go_event_man exporting r_object = go_salv.
  go_alv->display( ).

The above coding is also available as downloadable Abap source:

.