user_unknown007
user_unknown007

Reputation: 57

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!

Upvotes: 1

Views: 8481

Answers (2)

Sandra Rossi
Sandra Rossi

Reputation: 13628

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.

Upvotes: 5

Sandra Rossi
Sandra Rossi

Reputation: 13628

Here is a minimal example for CL_SALV_TABLE, to execute code when an ALV hotspot field is clicked (when any cell is clicked, a popup is displayed with a text, but you can do whatever you want of course).

What is important to remember:

  • set_cell_type( if_salv_c_cell_type=>hotspot ): to define cells of type hotspot
  • METHODS on_link_click FOR EVENT link_click OF cl_salv_events ...: 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 a hotspot field is clicked

Code:

REPORT zdemo.
CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
    METHODS on_link_click
      FOR EVENT link_click OF cl_salv_events_table
      IMPORTING column row.
    DATA gt_sbook TYPE TABLE OF sbook.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
  METHOD constructor.
    SELECT * FROM sbook INTO TABLE gt_sbook.
    cl_salv_table=>factory( EXPORTING r_container  = cl_gui_container=>screen0
                            IMPORTING r_salv_table = DATA(salv)
                            CHANGING  t_table      = gt_sbook ).
    SET HANDLER on_link_click FOR salv->get_event( ).
    LOOP AT salv->get_columns( )->get( ) INTO DATA(column).
      CAST cl_salv_column_table( 
          salv->get_columns( )->get_column( column-columnname ) 
          )->set_cell_type( if_salv_c_cell_type=>hotspot ).
    ENDLOOP.
    salv->display( ).
  ENDMETHOD.
  METHOD on_link_click.
    READ TABLE gt_sbook INDEX row INTO DATA(ls_sbook).
    IF sy-subrc = 0.
      ASSIGN COMPONENT column OF STRUCTURE ls_sbook TO FIELD-SYMBOL(<field>).
      MESSAGE |click value "{ <field> }" col { column } row { row }| 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.

Screenshots:

  • When the program is run: ALV CL_SALV_TABLE all cells with hotspot/hyperlink
  • When the user clicks on one cell: ALV CL_SALV_TABLE single click of one hotspot/hyperlink cell

Upvotes: 0

Related Questions