Sunteți pe pagina 1din 8

Code listing for: ZPR_ALV_OOPS_EVENTS

Description: Programa ZPR_ALV_OOPS_EVENTS


*&-----------------------------------------------------------------
----*
*& Report ZPR_ALV_OOPS_EVENTS
*&-----------------------------------------------------------------
----*
*&
*&-----------------------------------------------------------------
----*
REPORT zpr_alv_oops_events.
TYPE-POOLS : cndp.
CLASS :lcl_event_receiver DEFINITION DEFERRED.
*&-----------------------------------------------------------------
----*
* DECLARATIONS
*&-----------------------------------------------------------------
----*
*TYPE DECLARATION
TYPES : BEGIN OF t_material,
matnr TYPE matnr,
mtart TYPE mtart,
mbrsh TYPE mbrsh,
matkl TYPE matkl,
meins TYPE meins,
END OF t_material.
TYPES : BEGIN OF t_mat_detail,
matnr TYPE matnr,
werks TYPE werks_d,
lgort TYPE lgort_d,
END OF t_mat_detail.
*INTERNAL TABLE/WORK AREA DECLARATION
DATA : it_material TYPE STANDARD TABLE OF t_material,
wa_material TYPE t_material.
DATA : it_mat_detail TYPE STANDARD TABLE OF t_mat_detail,
wa_mat_detail TYPE t_mat_detail.
*VARIABLE DECLARATION
DATA : v_repid TYPE sy-repid,
v_okcode TYPE sy-ucomm.
*CONSTANT DECLARATION
CONSTANTS : c_check(1) TYPE c VALUE 'X'.
*NAME OF CUSTOM CONTAINER ADDED ON SCREEN
DATA : z_container TYPE scrfname VALUE 'MAT_CONTAINER'.
DATA : z_custom_container TYPE REF TO cl_gui_custom_container.
DATA : v_event_receiver TYPE REF TO lcl_event_receiver.
*ALV GRID CONTAINER
DATA : z_grid TYPE REF TO cl_gui_alv_grid.
*FIELD CATALOG
DATA : it_fieldcat TYPE lvc_t_fcat,
wa_fieldcat TYPE lvc_s_fcat.
*LAYOUT
DATA : wa_layout TYPE lvc_s_layo.
*&-----------------------------------------------------------------
----*
* INITIALIZATION
*&-----------------------------------------------------------------
----*
INITIALIZATION.
v_repid = sy-repid.
*------------------------------------------------------------------
----*
* CLASS lcl_oo_alv DEFINITION
*------------------------------------------------------------------
----*
*
*------------------------------------------------------------------
----*
CLASS lcl_oo_alv DEFINITION.
PUBLIC SECTION.
METHODS populate_itab.
METHODS create_field_catalog.
METHODS create_layout.
METHODS display_alv.
ENDCLASS. "lcl_oo_alv DEFINITION
*------------------------------------------------------------------
----*
* CLASS lcl_oo_alv IMPLEMENTATION
*------------------------------------------------------------------
----*
*
*------------------------------------------------------------------
----*
CLASS lcl_oo_alv IMPLEMENTATION.
*-->populate the internal table with data
METHOD populate_itab.
SELECT matnr
mtart
mbrsh
matkl
meins
FROM mara
INTO TABLE it_material.
ENDMETHOD. "populate_itab
*-->create the field catalog to map the data
METHOD create_field_catalog.
wa_fieldcat-fieldname = 'MATNR'.
wa_fieldcat-coltext = 'Material #'.
wa_fieldcat-outputlen = '20'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MTART'.
wa_fieldcat-coltext = 'Material Type'.
wa_fieldcat-outputlen = '20'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MBRSH'.
wa_fieldcat-coltext = 'Industry'.
wa_fieldcat-outputlen = '20'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MATKL'.
wa_fieldcat-coltext = 'Material Group'.
wa_fieldcat-outputlen = '20'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MEINS'.
wa_fieldcat-coltext = 'UOM'.
wa_fieldcat-outputlen = '20'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
ENDMETHOD. "create_field_catalog
*-->set the layout for the ALV display
METHOD create_layout.
wa_layout-grid_title = v_repid.
wa_layout-zebra = c_check.
wa_layout-grid_title = 'OOPs ALV'.
* wa_layout-stylefname = 'CELL_STYLE'.
ENDMETHOD. "create_layout
*-->display data in ALV Grid
METHOD display_alv.
CALL METHOD z_grid->set_table_for_first_display
EXPORTING
is_layout = wa_layout
CHANGING
it_outtab = it_material
it_fieldcatalog = it_fieldcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDMETHOD. "display_alv
ENDCLASS. "lcl_oo_alv IMPLEMENTATION
*------------------------------------------------------------------
---*
* CLASS LCL_EVENT_RECEIVER DEFINITION
*------------------------------------------------------------------
---*
*
*------------------------------------------------------------------
---*
CLASS lcl_event_receiver DEFINITION.
*------------------------------------------------------------------
---*
*
*
*------------------------------------------------------------------
---*
PUBLIC SECTION.
*-->Method for User command
METHODS :
handle_toolbar FOR EVENT toolbar OF
cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command FOR EVENT user_command OF
cl_gui_alv_grid
IMPORTING e_ucomm,
handle_double_click FOR EVENT double_click OF
cl_gui_alv_grid
IMPORTING e_row e_column.
ENDCLASS. "lcl_event_receiver DEFINITION
*------------------------------------------------------------------
---*
* CLASS lcl_event_receiver IMPLEMENTATION
*------------------------------------------------------------------
---*
*
*
*------------------------------------------------------------------
---*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_toolbar.
DATA: ls_toolbar TYPE stb_button.
* append a separator to normal toolbar
CLEAR ls_toolbar.
MOVE 3 TO ls_toolbar-butn_type.
APPEND ls_toolbar TO e_object->mt_toolbar.
* append an icon to show booking table
CLEAR ls_toolbar.
MOVE 'DETAILS' TO ls_toolbar-function.
MOVE icon_employee TO ls_toolbar-icon.
MOVE 'Show Details'(111) TO ls_toolbar-quickinfo.
MOVE 'Detail'(112) TO ls_toolbar-text.
MOVE ' ' TO ls_toolbar-disabled.
APPEND ls_toolbar TO e_object->mt_toolbar.
ENDMETHOD. "handle_toolbar
*-->Handle user command
METHOD handle_user_command.
CASE e_ucomm.
WHEN 'DETAILS'.
DATA: lt_rows TYPE lvc_t_row,
wa_rows LIKE LINE OF lt_rows.
REFRESH it_mat_detail.
CLEAR : wa_mat_detail, wa_material.
CALL METHOD z_grid->get_selected_rows
IMPORTING
et_index_rows = lt_rows.
CALL METHOD cl_gui_cfw=>flush.
READ TABLE lt_rows INTO wa_rows INDEX 1 TRANSPORTING index.
IF sy-subrc = 0.
READ TABLE it_material INTO wa_material INDEX wa_rows-
index
TRANSPORTING matnr.
IF sy-subrc = 0.
SELECT matnr werks lgort
FROM mard
INTO TABLE it_mat_detail
WHERE matnr = wa_material-matnr.
IF sy-subrc = 0.
CALL FUNCTION 'POPUP_WITH_TABLE_DISPLAY'
EXPORTING
endpos_col = 30
endpos_row = 10
startpos_col = 1
startpos_row = 1
titletext = 'Material Details'
TABLES
valuetab = it_mat_detail
EXCEPTIONS
break_off = 1
OTHERS = 2.
IF sy-subrc = 1.
MESSAGE 'Action Cancelled..!!' TYPE 'S'.
ELSE.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDCASE.
ENDMETHOD. "handle_user_command
METHOD handle_double_click.
READ TABLE it_material INTO wa_material INDEX e_row-index
TRANSPORTING matnr.
SET PARAMETER ID 'MAT' FIELD wa_material-matnr.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDMETHOD. "handle_double_click
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
*&-----------------------------------------------------------------
----*
* CLASS INSTANCE VARIABLE
*&-----------------------------------------------------------------
----*
DATA : obj1 TYPE REF TO lcl_oo_alv.
*&-----------------------------------------------------------------
----*
* START OF SELECTION
*&-----------------------------------------------------------------
----*
START-OF-SELECTION.
*CREATE OBJECT
CREATE OBJECT : obj1,
v_event_receiver.
*-->call method to populate the internal table
CALL METHOD obj1->populate_itab.
*&-----------------------------------------------------------------
----*
* CALL SCREEN
*&-----------------------------------------------------------------
----*
CALL SCREEN 1011.
*&-----------------------------------------------------------------
----*
*& Module STATUS_1011 OUTPUT
*&-----------------------------------------------------------------
----*
* text
*------------------------------------------------------------------
----*
MODULE status_1011 OUTPUT.
SET PF-STATUS 'ZALV_STATUS'.
SET TITLEBAR 'ZALV_OOPS'.
ENDMODULE. " STATUS_1011 OUTPUT
*&-----------------------------------------------------------------
----*
*& Module CREATE_INSTANCE OUTPUT
*&-----------------------------------------------------------------
----*
* text
*------------------------------------------------------------------
----*
MODULE create_instance OUTPUT.
*CUSTOM CONTAINER INSTANCE
CREATE OBJECT z_custom_container
EXPORTING
container_name = z_container
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*ALV GRID INSTANCE
CREATE OBJECT z_grid
EXPORTING
i_parent = z_custom_container
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
SET HANDLER v_event_receiver->handle_toolbar FOR z_grid.
SET HANDLER v_event_receiver->handle_user_command FOR z_grid.
SET HANDLER v_event_receiver->handle_double_click FOR z_grid.
ENDMODULE. " CREATE_INSTANCE OUTPUT
*&-----------------------------------------------------------------
----*
*& Module DISPLAY_DATA OUTPUT
*&-----------------------------------------------------------------
----*
* text
*------------------------------------------------------------------
----*
MODULE display_data OUTPUT.
*-->call method to create field catalog
CALL METHOD obj1->create_field_catalog.
*-->call method to create the layout
CALL METHOD obj1->create_layout.
*-->call method to display data in ALV Grid
CALL METHOD obj1->display_alv.
ENDMODULE. " DISPLAY_DATA OUTPUT
*&-----------------------------------------------------------------
----*
*& Module USER_COMMAND_1011 INPUT
*&-----------------------------------------------------------------
----*
* text
*------------------------------------------------------------------
----*
MODULE user_command_1011 INPUT.
v_okcode = sy-ucomm.
CASE v_okcode.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'EXIT'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. " USER_COMMAND_1011 INPUT

*GUI Texts
*----------------------------------------------------------
* ZALV_OOPS --> ALV OOPS EVENTS
*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
* Action Cancelled..!!
Extracted by Mass Download version 1.5.5 - E.G.Mellodew. 1998-2018. Sap Release 750