Have
you ever asked how do we create an interactive checkbox using ALV
Object Model? The sample program below will give you that flavor.
*&---------------------------------------------------------------------*
*& Report ZIF_CUSTOMER_MASTER
*&
*&---------------------------------------------------------------------*
*& This program is to create file based on customer master data
*&---------------------------------------------------------------------*
REPORT zif_customer_master.
*----------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* class Definition of handle event
*----------------------------------------------------------------------*
* event handler class
CLASS lcl_event_handler DEFINITION.
*
PUBLIC SECTION.
METHODS:
on_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
*
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
* Class Definition of report
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
* types declaration
TYPES: BEGIN OF gty_kna1.
TYPES: check TYPE as4flag,
kunnr TYPE kunnr,
land1 TYPE land1_gp,
name1 TYPE name1_gp,
name2 TYPE name2_gp,
ort01 TYPE ort01_gp,
pstlz TYPE pstlz,
regio TYPE regio,
sortl TYPE sortl,
stras TYPE stras_gp,
telf1 TYPE telf1.
END OF gty_kna1.
* variables declaration
CLASS-DATA: kna1 TYPE kna1.
DATA: gv_string TYPE string.
DATA: gr_exception TYPE REF TO cx_salv_msg.
DATA: gr_settings TYPE REF TO cl_salv_display_settings.
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.
DATA: gr_alv TYPE REF TO cl_salv_table.
DATA: gr_events TYPE REF TO cl_salv_events_table.
DATA: gr_event_handler TYPE REF TO lcl_event_handler.
DATA: gv_title TYPE lvc_title.
DATA: gt_kna1 TYPE TABLE OF gty_kna1.
DATA: gw_kna1 LIKE LINE OF gt_kna1.
DATA: gw_cell_types LIKE LINE OF gw_kna1-cell_types.
* method declaration
METHODS get_data.
METHODS instantiate_alv_om.
METHODS alv_om_setting.
METHODS set_checkbox.
METHODS display_data.
ENDCLASS. "lcl_report DEFINITION
*----------------------------------------------------------------------*
* MAIN PROGRAM
*----------------------------------------------------------------------*
* Main Program
*----------------------------------------------------------------------*
* selection screen
SELECT-OPTIONS: s_kunnr FOR lcl_report=>kna1-kunnr.
* Processing logic
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
CREATE OBJECT lo_report.
lo_report->get_data( ).
lo_report->instantiate_alv_om( ).
lo_report->alv_om_setting( ).
lo_report->set_checkbox( ).
lo_report->display_data( ).
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* Implementation class of local report
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*&---------------------------------------------------------------------*
*& Method GET_DATA
*&---------------------------------------------------------------------*
* Select data from SAP customer master data
*----------------------------------------------------------------------*
METHOD get_data .
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_kna1
FROM kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc <> 0.
gv_string = 'No Customer Master Data Found!'(ncd).
MESSAGE gv_string TYPE 'E'.
ENDIF.
ENDMETHOD. " GET_DATA
*&---------------------------------------------------------------------*
*& Method DISPLAY_DATA
*&---------------------------------------------------------------------*
* Display data using ALV OM
*----------------------------------------------------------------------*
METHOD display_data .
gr_functions->set_all( ).
gr_columns->set_optimize( ).
CALL METHOD gr_alv->display.
ENDMETHOD. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Method INSTANTIATE_ALV_OM
*&---------------------------------------------------------------------*
* Instantiate ALV_OM
*----------------------------------------------------------------------*
METHOD instantiate_alv_om .
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = if_salv_c_bool_sap=>false
* r_container =
* container_name =
IMPORTING
r_salv_table = gr_alv
CHANGING
t_table = gt_kna1.
CATCH cx_salv_msg INTO gr_exception.
gv_string = gr_exception->get_text( ).
MESSAGE gv_string TYPE 'E'.
ENDTRY.
ENDMETHOD. " INSTANTIATE_ALV_OM
*&---------------------------------------------------------------------*
*& Method SET_CHECKBOX
*&---------------------------------------------------------------------*
* Set checkbox so that user can click on it
*----------------------------------------------------------------------*
METHOD set_checkbox .
* Change the properties of the Columns CHECK
TRY.
gr_column ?= gr_columns->get_column( 'CHECK' ).
gr_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
gr_column->set_output_length( 1 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
ENDMETHOD. " SET_CHECKBOX
*&---------------------------------------------------------------------*
*& Method ALV_OM_SETTING
*&---------------------------------------------------------------------*
* Set ALV control
*&---------------------------------------------------------------------*
METHOD alv_om_setting .
* Get the event object
gr_events = gr_alv->get_event( ).
* Instantiate the event handler object
CREATE OBJECT gr_event_handler.
*
* event handler
SET HANDLER gr_event_handler->on_link_click FOR gr_events.
gr_functions = gr_alv->get_functions( ).
gr_columns = gr_alv->get_columns( ).
gr_settings = gr_alv->get_display_settings( ).
gv_title = text-tit.
gr_settings->set_list_header( value = gv_title ).
gr_settings->set_striped_pattern( value = 'X' ).
ENDMETHOD. " ALV_OM_SETTING
ENDCLASS. "lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
* Implementation class of event handler
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
*
METHOD on_link_click.
*
* Get the value of the checkbox and set the value accordingly
* Refersh the table
FIELD-SYMBOLS: <lfs_data> LIKE LINE OF lo_report->gt_kna1.
READ TABLE lo_report->gt_kna1 ASSIGNING <lfs_data> INDEX row.
CHECK sy-subrc IS INITIAL.
IF <lfs_data>-check IS INITIAL.
<lfs_data>-check = 'X'.
ELSE.
CLEAR <lfs_data>-check.
ENDIF.
lo_report->gr_alv->refresh( ).
ENDMETHOD. "on_link_click
*
ENDCLASS. "lcl_event_handler IMPLEMENTATION
*& Report ZIF_CUSTOMER_MASTER
*&
*&---------------------------------------------------------------------*
*& This program is to create file based on customer master data
*&---------------------------------------------------------------------*
REPORT zif_customer_master.
*----------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* class Definition of handle event
*----------------------------------------------------------------------*
* event handler class
CLASS lcl_event_handler DEFINITION.
*
PUBLIC SECTION.
METHODS:
on_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
*
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
* Class Definition of report
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
* types declaration
TYPES: BEGIN OF gty_kna1.
TYPES: check TYPE as4flag,
kunnr TYPE kunnr,
land1 TYPE land1_gp,
name1 TYPE name1_gp,
name2 TYPE name2_gp,
ort01 TYPE ort01_gp,
pstlz TYPE pstlz,
regio TYPE regio,
sortl TYPE sortl,
stras TYPE stras_gp,
telf1 TYPE telf1.
END OF gty_kna1.
* variables declaration
CLASS-DATA: kna1 TYPE kna1.
DATA: gv_string TYPE string.
DATA: gr_exception TYPE REF TO cx_salv_msg.
DATA: gr_settings TYPE REF TO cl_salv_display_settings.
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.
DATA: gr_alv TYPE REF TO cl_salv_table.
DATA: gr_events TYPE REF TO cl_salv_events_table.
DATA: gr_event_handler TYPE REF TO lcl_event_handler.
DATA: gv_title TYPE lvc_title.
DATA: gt_kna1 TYPE TABLE OF gty_kna1.
DATA: gw_kna1 LIKE LINE OF gt_kna1.
DATA: gw_cell_types LIKE LINE OF gw_kna1-cell_types.
* method declaration
METHODS get_data.
METHODS instantiate_alv_om.
METHODS alv_om_setting.
METHODS set_checkbox.
METHODS display_data.
ENDCLASS. "lcl_report DEFINITION
*----------------------------------------------------------------------*
* MAIN PROGRAM
*----------------------------------------------------------------------*
* Main Program
*----------------------------------------------------------------------*
* selection screen
SELECT-OPTIONS: s_kunnr FOR lcl_report=>kna1-kunnr.
* Processing logic
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
CREATE OBJECT lo_report.
lo_report->get_data( ).
lo_report->instantiate_alv_om( ).
lo_report->alv_om_setting( ).
lo_report->set_checkbox( ).
lo_report->display_data( ).
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* Implementation class of local report
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*&---------------------------------------------------------------------*
*& Method GET_DATA
*&---------------------------------------------------------------------*
* Select data from SAP customer master data
*----------------------------------------------------------------------*
METHOD get_data .
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_kna1
FROM kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc <> 0.
gv_string = 'No Customer Master Data Found!'(ncd).
MESSAGE gv_string TYPE 'E'.
ENDIF.
ENDMETHOD. " GET_DATA
*&---------------------------------------------------------------------*
*& Method DISPLAY_DATA
*&---------------------------------------------------------------------*
* Display data using ALV OM
*----------------------------------------------------------------------*
METHOD display_data .
gr_functions->set_all( ).
gr_columns->set_optimize( ).
CALL METHOD gr_alv->display.
ENDMETHOD. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Method INSTANTIATE_ALV_OM
*&---------------------------------------------------------------------*
* Instantiate ALV_OM
*----------------------------------------------------------------------*
METHOD instantiate_alv_om .
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = if_salv_c_bool_sap=>false
* r_container =
* container_name =
IMPORTING
r_salv_table = gr_alv
CHANGING
t_table = gt_kna1.
CATCH cx_salv_msg INTO gr_exception.
gv_string = gr_exception->get_text( ).
MESSAGE gv_string TYPE 'E'.
ENDTRY.
ENDMETHOD. " INSTANTIATE_ALV_OM
*&---------------------------------------------------------------------*
*& Method SET_CHECKBOX
*&---------------------------------------------------------------------*
* Set checkbox so that user can click on it
*----------------------------------------------------------------------*
METHOD set_checkbox .
* Change the properties of the Columns CHECK
TRY.
gr_column ?= gr_columns->get_column( 'CHECK' ).
gr_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
gr_column->set_output_length( 1 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
ENDMETHOD. " SET_CHECKBOX
*&---------------------------------------------------------------------*
*& Method ALV_OM_SETTING
*&---------------------------------------------------------------------*
* Set ALV control
*&---------------------------------------------------------------------*
METHOD alv_om_setting .
* Get the event object
gr_events = gr_alv->get_event( ).
* Instantiate the event handler object
CREATE OBJECT gr_event_handler.
*
* event handler
SET HANDLER gr_event_handler->on_link_click FOR gr_events.
gr_functions = gr_alv->get_functions( ).
gr_columns = gr_alv->get_columns( ).
gr_settings = gr_alv->get_display_settings( ).
gv_title = text-tit.
gr_settings->set_list_header( value = gv_title ).
gr_settings->set_striped_pattern( value = 'X' ).
ENDMETHOD. " ALV_OM_SETTING
ENDCLASS. "lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
* Implementation class of event handler
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
*
METHOD on_link_click.
*
* Get the value of the checkbox and set the value accordingly
* Refersh the table
FIELD-SYMBOLS: <lfs_data> LIKE LINE OF lo_report->gt_kna1.
READ TABLE lo_report->gt_kna1 ASSIGNING <lfs_data> INDEX row.
CHECK sy-subrc IS INITIAL.
IF <lfs_data>-check IS INITIAL.
<lfs_data>-check = 'X'.
ELSE.
CLEAR <lfs_data>-check.
ENDIF.
lo_report->gr_alv->refresh( ).
ENDMETHOD. "on_link_click
*
ENDCLASS. "lcl_event_handler IMPLEMENTATION
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com
Social Plugin