abapalv

How to trigger code when the user clicks an ALV hotspot field?


I am struggling with the following issue:

I have two tables (header and items) that I want to connect them by using a hotspot and the class CL_SALV_TABLE. I managed to display the header table and set a hotspot on the column with the number of the order. I want that the second table opens as a popup window after I click once on the number field (which was my hotspot). However, I don't know how to define the event. I know how to get a popup window by using the display method of CL_SALV_TABLE, e.g., this code:

CALL METHOD cl_salv_table=>factory(
*  EXPORTING
*    list_display   = IF_SALV_C_BOOL_SAP=>FALSE
*    r_container    =
*    container_name =
  IMPORTING
    r_salv_table   = o_alv
  CHANGING
    t_table        = it_tab )
    .
 CATCH cx_salv_msg .
ENDTRY.


o_alv->set_screen_popup( start_column = 1
                         end_column   = 150
                         start_line   = 1
                         end_line     = 30 ).

o_alv->display( ).

Any comment or help is highly appreciated. Thank you in advance!


Solution

  • Here is a minimal example for CL_GUI_ALV_GRID, to execute code when an ALV hotspot field is clicked (when any cell in the column "Book number" is clicked, a popup is displayed with a text, but you can do whatever you want of course).

    What is important to remember:

    • METHODS on_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid ... : this is the method to define the code to run when the hotspot field is clicked
    • SET HANDLER on_hotspot_click ... : to tell the Control Framework to trigger the method when the event occurs

    Code:

    CLASS lcl_app DEFINITION.
      PUBLIC SECTION.
        METHODS constructor.
        METHODS on_hotspot_click
          FOR EVENT hotspot_click OF cl_gui_alv_grid
          IMPORTING e_row_id e_column_id es_row_no.
        DATA go_alv TYPE REF TO cl_gui_alv_grid.
        DATA gt_sbook TYPE TABLE OF sbook.
    ENDCLASS.
    CLASS lcl_app IMPLEMENTATION.
      METHOD constructor.
        CREATE OBJECT go_alv EXPORTING i_parent = cl_gui_container=>screen0.
        SELECT * FROM sbook INTO TABLE gt_sbook.
        DATA(fieldcatalog) = VALUE lvc_t_fcat(
        ( fieldname = 'BOOKID' ref_table = 'SBOOK' ref_field = 'BOOKID' hotspot = 'X' ) ).
        SET HANDLER on_hotspot_click FOR go_alv.
        go_alv->set_table_for_first_display(
            EXPORTING i_structure_name = 'SBOOK'
            CHANGING it_outtab = gt_sbook it_fieldcatalog = fieldcatalog ).
      ENDMETHOD.
      METHOD on_hotspot_click.
        READ TABLE gt_sbook INDEX es_row_no-row_id INTO DATA(ls_sbook).
        IF sy-subrc = 0.
          MESSAGE |click { ls_sbook-bookid } col { e_column_id-fieldname } row { es_row_no-row_id }| TYPE 'I'.
        ENDIF.
      ENDMETHOD.
    ENDCLASS.
    DATA go_app TYPE REF TO lcl_app.
    PARAMETERS dummy.
    AT SELECTION-SCREEN OUTPUT.
      IF go_app IS NOT BOUND.
        go_app = NEW lcl_app( ).
      ENDIF.