Interactive ALV with factory method using events

Interactive ALV with factory method using events and event handler methods, using events in ALV factory methods

+ -

The below example explains you of using events in ALV factory methods, first thing I recommend you is to go through what are events on Object Oriented ABAP .

Step 1: Data declarations , get instance of ALV factory.

TYPES: BEGIN OF TY_MARA, "MARA internal table
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
       WA_MARA TYPE TY_MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "SALV table instance
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS. "fetch data
*  TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
*    EXPORTING
*      LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*      R_CONTAINER    =
*      CONTAINER_NAME =
  IMPORTING
    R_SALV_TABLE = LR_ALV
  CHANGING
    T_TABLE      = IT_MARA.
*   CATCH CX_SALV_MSG .
*  ENDTRY.

Step 2: Get ALV columns and insert hotspot.

**get ALV columns
DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS  "get all columns
  RECEIVING
    VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
*   Get MATNR column
  TRY.
      LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to insert hotspot
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

* Set the HotSpot for MATNR Column
  TRY.
      CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
        EXPORTING
          VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
      .
    CATCH CX_SALV_DATA_ERROR .
  ENDTRY.
ENDIF.

In-order to implement hotspot click there is a event called LINK_CLICK, we need to implement this event to implement hotspot click.

Step 3: Follow below steps to implement LINK_CLICK event.

  • Create a local class.
  • Create a event handler method in that local class to handle LINK_CLICK of CL_SALV_EVENTS_TABLE.
  • CLASS LCL_HANDLE_EVENTS DEFINITION.
      PUBLIC SECTION.
        METHODS:
          ON_LINE_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE 
            IMPORTING ROW COLUMN. "event handler method, imports row and column of clicked value
    ENDCLASS.                    
    
  • Implement event handler method.
  • CLASS LCL_HANDLE_EVENTS IMPLEMENTATION.
      METHOD ON_LINE_CLICK. "double click implementation
    **handle double click here
        READ TABLE IT_MARA INTO WA_MARA INDEX ROW.
        IF SY-SUBRC EQ 0.
          SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR.
          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
        ENDIF.
      ENDMETHOD.                    "on_link_click
    ENDCLASS.                    "lcl_events IMPLEMENTATION
    
  • Get events of ALV using GET_EVENT method.
  • *Register events
    DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
    LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
    
  • Register event handler method.
  • DATA: GR_EVENTS TYPE REF TO LCL_HANDLE_EVENTS.
    CREATE OBJECT GR_EVENTS.
    SET HANDLER GR_EVENTS->ON_LINE_CLICK FOR LR_EVENTS. "register event handler method
    

Step 4: Add Functions to ALV.

To set functions to ALV, we use SET_SCREEN_STATUS method, to implement this create a menu in SE41 for this program and add using above method.

In this example I am using standard ALV functions (menu).

**set standard ALV functions visible
LR_ALV->SET_SCREEN_STATUS(
  PFSTATUS      =  'SALV_STANDARD'
  REPORT        =  'SALV_TEST_FUNCTIONS'
  SET_FUNCTIONS = LR_ALV->C_FUNCTIONS_ALL ).

Final Code will be

REPORT ZSAPN_ALV_MARA_FACTARY.
CLASS LCL_HANDLE_EVENTS DEFINITION DEFERRED. "class definition deferred
TYPES: BEGIN OF TY_MARA, "MARA internal table
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
       WA_MARA TYPE TY_MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "SALV table instance
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS. "fetch data
*  TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
*    EXPORTING
*      LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*      R_CONTAINER    =
*      CONTAINER_NAME =
  IMPORTING
    R_SALV_TABLE = LR_ALV
  CHANGING
    T_TABLE      = IT_MARA.
*   CATCH CX_SALV_MSG .
*  ENDTRY.

**get ALV columns
DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS  "get all columns
  RECEIVING
    VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
*   Get MATNR column
  TRY.
      LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to insert hotspot
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

* Set the HotSpot for MATNR Column
  TRY.
      CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
        EXPORTING
          VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
      .
    CATCH CX_SALV_DATA_ERROR .
  ENDTRY.
ENDIF.

*Event handler method for ALV
DATA: GR_EVENTS TYPE REF TO LCL_HANDLE_EVENTS.
***handle hotspot click
CLASS LCL_HANDLE_EVENTS DEFINITION.
  PUBLIC SECTION.
    METHODS:
      ON_LINE_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE
        IMPORTING ROW COLUMN. "event handler method, importing row and column of clicked value
ENDCLASS.                    "lcl_events DEFINITION

*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
CREATE OBJECT GR_EVENTS.
SET HANDLER GR_EVENTS->ON_LINE_CLICK FOR LR_EVENTS. "register event handler method
**set standard ALV functions visible
LR_ALV->SET_SCREEN_STATUS(
  PFSTATUS      =  'SALV_STANDARD'
  REPORT        =  'SALV_TEST_FUNCTIONS'
  SET_FUNCTIONS = LR_ALV->C_FUNCTIONS_ALL ).

LR_ALV->DISPLAY( ). "display grid
CLASS LCL_HANDLE_EVENTS IMPLEMENTATION.
  METHOD ON_LINE_CLICK. "double click implementation
**handle double click here
    READ TABLE IT_MARA INTO WA_MARA INDEX ROW. "here row is index no
    IF SY-SUBRC EQ 0.
      SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR.
      CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
    ENDIF.
  ENDMETHOD.                    "on_link_click
ENDCLASS.                    "lcl_events IMPLEMENTATION

Output

Click on any material no and test

Events in ALV factory methods

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 29 People out of 30 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Chapter →