Reference on the local internal table from event handler in object oriented ALV

I prefer to use local, instead of global variables in my programs. It's sometimes difficult to manage it between various program events or screens. In case of object oriented ALV, I use the following construction to use local variables and access them from event handler to avoid global variables. This construction is based on the reference, which is passed and saved as static attribute in the class of event handler.
Here the complete example:
REPORT zkm_test.

TYPES:
* Define a table type, you can use it to store the reference
* on the internal table
  tt_usr TYPE TABLE OF usr02 WITH DEFAULT KEY.

*----------------------------------------------------------------------*
*       CLASS cl_event_handler DEFINITION
*----------------------------------------------------------------------*
CLASS cl_event_handler DEFINITION.

  PUBLIC SECTION.

*   Static attributes to store references on your local variables
    CLASS-DATA:
*     Reference on the internal table with data
      lr_usr   TYPE REF TO tt_usr,
*     Reference on the ALV object
      lo_table TYPE REF TO cl_salv_table.

    CLASS-METHODS on_link_click
      FOR EVENT if_salv_events_actions_table~link_click
        OF cl_salv_events_table
          IMPORTING row column.
ENDCLASS.                    "cl_event_handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_event_handler IMPLEMENTATION.

  METHOD on_link_click.

    DATA:
      ls_usr TYPE usr02.

*   So you can reach your data, read the complete table ...
    LOOP AT lr_usr->* INTO ls_usr.
    ENDLOOP.

*   ... or clicked row
    READ TABLE lr_usr->* INTO ls_usr INDEX row.

  ENDMETHOD.                    "on_link_click
ENDCLASS.                    "cl_event_handler IMPLEMENTATION

*----------------------------------------------------------------------*
*       START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
FORM display_alv.

  DATA:
    lt_usr     TYPE tt_usr,          " <- local internal table
    lo_table   TYPE REF TO cl_salv_table,
    lo_events  TYPE REF TO cl_salv_events_table,
    lo_columns TYPE REF TO cl_salv_columns_table,
    lo_column  TYPE REF TO cl_salv_column_list.

  SELECT * FROM usr02 UP TO 30 ROWS
    APPENDING CORRESPONDING FIELDS OF TABLE lt_usr
    ORDER BY bname.

  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = lo_table
        CHANGING
          t_table      = lt_usr.

*     Register event handler
      lo_events = lo_table->get_event( ).
      SET HANDLER cl_event_handler=>on_link_click FOR lo_events.

*     Get and store the reference on your local internal table
      GET REFERENCE OF lt_usr INTO cl_event_handler=>lr_usr.
*     Also store the reference on the ALV object, it can be useful
      cl_event_handler=>lo_table = lo_table.

*     Set column as hotspot
      lo_columns = lo_table->get_columns( ).
      lo_column ?= lo_columns->get_column( 'BNAME' ).
      lo_column->set_cell_type( if_salv_c_cell_type=>hotspot ).

      lo_table->display( ).

    CATCH cx_salv_msg.             " cl_salv_table=>factory()
      WRITE: / 'cx_salv_msg exception'.
      STOP.
    CATCH cx_salv_not_found.       " cl_salv_columns_table->get_column()
      WRITE: / 'cx_salv_not_found exception'.
      STOP.
  ENDTRY.
ENDFORM.                    "display_alv
See other related notes in my infodepot:
Events in the object oriented ALV based on class CL_SALV_TABLE
Full list of examples in my infodepot

If you have a question, have found an error or just want to contact me, please use this form.

Copyright (C) 2010 http://www.kerum.pl/infodepot/

Disclaimer: I am not affiliated or related to any division or subsidiary of SAP AG.
Trademarks or registered trademarks of any products or companies referred to on this site belong to those companies.
Anyone using the given solutions, is doing it under his/her own responsibility and at own risk.