OO ALV with hotspot and call transaction
OO ALV report with call transaction on hotspot click using CL_SALV_TABLE
There was a requirement to display list of materials in an ALV control and put hyperlink(hotspot) to material no, when ever we click on material number, it should call MM03 and directly display material.
In this kind of requirement, we need to use below CALL TRANSACTION with parameter id for calling a transaction with parameters.
In the below example, we use object oriented techniques to display ALV table and will use events to handle hyperlink click.
CLASS | CL_SALV_TABLE |
Step1: Display ALV using class CL_SALV_TABLE and method DISPLAY()
Refer working with ALV Factory methods for more details on factory methods
TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, END OF ty_mara. DATA: it_mara TYPE TABLE OF ty_mara, wa_mara TYPE ty_mara. DATA: o_alv TYPE REF TO cl_salv_table. START-OF-SELECTION. SELECT matnr mtart matkl FROM mara INTO TABLE it_mara UP TO 10 ROWS. DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = it_mara ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. o_alv->display( ).
Step2: Create a local class to create hyperlink and event handler
CLASS lcl_alv DEFINITION. PUBLIC SECTION. METHODS: set_hotspot_matnr CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: on_matnr_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row column . ENDCLASS.
CLASS lcl_alv IMPLEMENTATION. METHOD set_hotspot_matnr. * Set Hotspot DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table, lo_col_tab TYPE REF TO cl_salv_column_table. * Get columns object lo_cols_tab = co_alv->get_columns( ). * Get MATNR column TRY. lo_col_tab ?= lo_cols_tab->get_column( 'MATNR' ). CATCH cx_salv_not_found. ENDTRY. * Set the hotspot for MATNR column TRY. CALL METHOD lo_col_tab->set_cell_type EXPORTING value = if_salv_c_cell_type=>hotspot. CATCH cx_salv_data_error . ENDTRY. *...Events DATA: lo_events TYPE REF TO cl_salv_events_table. * All events lo_events = o_alv->get_event( ). * Event handler SET HANDLER me->on_matnr_click FOR lo_events. * ENDMETHOD. METHOD on_matnr_click. * Get the material number from the table READ TABLE it_mara INTO wa_mara INDEX row. IF wa_mara-matnr IS NOT INITIAL. ** Call Transaction SET PARAMETER ID 'MAT' FIELD wa_mara-matnr . "Set Material no to MM03 matnr parameter id SET PARAMETER ID 'MXX' FIELD 'K' . "Directly Display Basic Data CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN . ENDIF. ENDMETHOD. ENDCLASS.
Step3: Create Object set hotspot .
CREATE OBJECT lo_alv. lo_alv->set_hotspot_matnr( CHANGING co_alv = o_alv ). o_alv->display( ).
Final and complete code
REPORT zalv_hyperlink. TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, END OF ty_mara. DATA: it_mara TYPE TABLE OF ty_mara, wa_mara TYPE ty_mara. DATA: o_alv TYPE REF TO cl_salv_table. CLASS lcl_alv DEFINITION DEFERRED. DATA: lo_alv TYPE REF TO lcl_alv. CLASS lcl_alv DEFINITION. PUBLIC SECTION. METHODS: set_hotspot_matnr CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: on_matnr_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row column . ENDCLASS. START-OF-SELECTION. SELECT matnr mtart matkl FROM mara INTO TABLE it_mara UP TO 10 ROWS. DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = it_mara ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. CREATE OBJECT lo_alv. lo_alv->set_hotspot_matnr( CHANGING co_alv = o_alv ). o_alv->display( ). CLASS lcl_alv IMPLEMENTATION. METHOD set_hotspot_matnr. * Set Hotspot DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table, lo_col_tab TYPE REF TO cl_salv_column_table. * Get columns object lo_cols_tab = co_alv->get_columns( ). * Get MATNR column TRY. lo_col_tab ?= lo_cols_tab->get_column( 'MATNR' ). CATCH cx_salv_not_found. ENDTRY. * Set the hotspot for MATNR column TRY. CALL METHOD lo_col_tab->set_cell_type EXPORTING value = if_salv_c_cell_type=>hotspot. CATCH cx_salv_data_error . ENDTRY. *...Events DATA: lo_events TYPE REF TO cl_salv_events_table. * All events lo_events = o_alv->get_event( ). * Event handler SET HANDLER me->on_matnr_click FOR lo_events. * ENDMETHOD. METHOD on_matnr_click. * Get the material number from the table READ TABLE it_mara INTO wa_mara INDEX row. IF wa_mara-matnr IS NOT INITIAL. ** Call Transaction SET PARAMETER ID 'MAT' FIELD wa_mara-matnr . "Set Material no to MM03 matnr parameter id SET PARAMETER ID 'MXX' FIELD 'K' . "Directly Display Basic Data CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN . ENDIF. ENDMETHOD. ENDCLASS.
Fuente:
Comentarios
Publicar un comentario