You are on page 1of 7

NOTE: As part of screen design create two screens with screen numbers 0100 and 0101.

Also create a
menu named MYMENU with Functional Keys BACK, CANCEL, and EXIT with respective function
codes.

To the screen 0100:


Add the menu MYMENU and in the PAI section of the screen provide appropriate code to handle the
functionality for BACK, CANCEL, and EXIT. See MODULE user_command_0100 INPUT in the
code below. To the layout of this screen add a customer control and name it CC_KNA1.

Flow logic of screen 0100:


PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.

PROCESS AFTER INPUT.


MODULE USER_COMMAND_0100.

To the screen 0101:


Add the menu MYMENU and in the PAI section of the screen provide appropriate code to handle the
functionality for BACK, CANCEL, and EXIT. See MODULE user_command_0101 INPUT in the
code below. To the layout of this screen add a customer control and name it CC_VBAK.

Flow logic of screen 0101:


PROCESS BEFORE OUTPUT.
MODULE STATUS_0101.

PROCESS AFTER INPUT.


MODULE USER_COMMAND_0101.

REPORT zoop_alv_ interactive.

CLASS lcl_event_handler DEFINITION DEFERRED.

TYPES: BEGIN OF ty_kna1,


kunnr TYPE kna1-kunnr,
name1 TYPE kna1-name1,
ort01 TYPE kna1-ort01,
land1 TYPE kna1-land1,
pstlz TYPE kna1-pstlz,
END OF ty_kna1,
BEGIN OF ty_vbak,
kunnr TYPE vbak-kunnr,
vbeln TYPE vbak-vbeln,
netwr TYPE vbak-netwr,
END OF ty_vbak.

DATA: t_kna1 TYPE TABLE OF ty_kna1,


r_ccontainer_kna1 TYPE REF TO cl_gui_custom_container,
r_grid_kna1 TYPE REF TO cl_gui_alv_grid,
s_layo_kna1 TYPE lvc_s_layo,
t_fcat_kna1 TYPE lvc_t_fcat,
t_vbak TYPE TABLE OF ty_vbak,
r_ccontainer_vbak TYPE REF TO cl_gui_custom_container,
r_grid_vbak TYPE REF TO cl_gui_alv_grid,
s_layo_vbak TYPE lvc_s_layo,
t_fcat_vbak TYPE lvc_t_fcat,
r_ev_handler TYPE REF TO lcl_event_handler.

DATA: g_kunnr TYPE kna1-kunnr.

*&-- Event Handling Class


CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING es_row_no.
ENDCLASS. "lcl_event_handler DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
METHOD handle_double_click.
DATA: l_kunnr TYPE kna1-kunnr.
READ TABLE t_kna1 INDEX es_row_no-row_id INTO l_kunnr.
SELECT kunnr
vbeln
netwr
INTO TABLE t_vbak
FROM vbak
WHERE kunnr = l_kunnr.
IF sy-subrc <> 0.
MESSAGE 'No order for the customer.' TYPE 'I'.
ELSE.
CALL SCREEN '0101'.
ENDIF.
ENDMETHOD. "handle_double_click
ENDCLASS. "lcl_event_handler IMPLEMENTATION

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.


SELECT-OPTIONS: s_kunnr FOR g_kunnr DEFAULT 1000 TO 2000.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
PERFORM populate_t_kna1.
CALL SCREEN '100'.

*----------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'MYMENU'.
PERFORM display_kna1_alv.
ENDMODULE. " STATUS_0100 OUTPUT

*----------------------------------------------------------------------*
* MODULE user_command_0100 INPUT
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
IF sy-ucomm = 'BACK'.
LEAVE TO SCREEN 0.
ELSEIF sy-ucomm = 'CANCEL' OR sy-ucomm = 'EXIT'.
LEAVE PROGRAM.
ENDIF.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form display_kna1_alv
*&---------------------------------------------------------------------*
FORM display_kna1_alv.
CHECK t_kna1 IS NOT INITIAL.

IF r_grid_kna1 IS INITIAL. See Note-1


PERFORM populate_kna1_layo.

PERFORM populate_kna1_fcat.

CREATE OBJECT r_ccontainer_kna1


EXPORTING
container_name = 'CC_KNA1'.

CREATE OBJECT r_grid_kna1


EXPORTING
i_parent = r_ccontainer_kna1.

CREATE OBJECT r_ev_handler.


SET HANDLER r_ev_handler->handle_double_click FOR r_grid_kna1.

CALL METHOD r_grid_kna1->set_table_for_first_display See Note-2


EXPORTING
is_layout = s_layo_kna1
CHANGING
it_outtab = t_kna1
it_fieldcatalog = t_fcat_kna1.
ELSE.
CALL METHOD r_grid_kna1->refresh_table_display. See Note-2
ENDIF.
ENDFORM. " display_kna1_alv

*&---------------------------------------------------------------------*
*& Form populate_kna1_layo
*&---------------------------------------------------------------------*
FORM populate_kna1_layo.
CLEAR s_layo_kna1.
s_layo_kna1-zebra = 'X'.
s_layo_kna1-cwidth_opt = 'X'.
ENDFORM. " populate_layout

*&---------------------------------------------------------------------*
*& Form populate_kna1_fcat
*&---------------------------------------------------------------------*
FORM populate_kna1_fcat .
DATA: ls_fcat TYPE lvc_s_fcat.

REFRESH t_fcat_kna1.
ls_fcat-fieldname = 'KUNNR'.
ls_fcat-coltext = 'Customer'.
APPEND ls_fcat TO t_fcat_kna1.
CLEAR ls_fcat.

ls_fcat-fieldname = 'NAME1'.
ls_fcat-coltext = 'Customer Name'.
APPEND ls_fcat TO t_fcat_kna1.
CLEAR ls_fcat.

ls_fcat-fieldname = 'ORT01'.
ls_fcat-coltext = 'City'.
APPEND ls_fcat TO t_fcat_kna1.
CLEAR ls_fcat.

ls_fcat-fieldname = 'LAND1'.
ls_fcat-coltext = 'Country'.
APPEND ls_fcat TO t_fcat_kna1.
CLEAR ls_fcat.

ls_fcat-fieldname = 'PSTLZ'.
ls_fcat-coltext = 'P Code'.
APPEND ls_fcat TO t_fcat_kna1.
CLEAR ls_fcat.
ENDFORM. " populate_fcat

*&---------------------------------------------------------------------*
*& Form populate_t_kna1
*&---------------------------------------------------------------------*
FORM populate_t_kna1 .
SELECT kunnr
name1
ort01
land1
pstlz
FROM kna1 INTO TABLE t_kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc <> 0.
MESSAGE 'No data found for given range.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " populate_t_kna1

*----------------------------------------------------------------------*
* MODULE status_0101 OUTPUT
*----------------------------------------------------------------------*
MODULE status_0101 OUTPUT.
SET PF-STATUS 'MYMENU'.
PERFORM display_vbak_alv.
ENDMODULE. " STATUS_0101 OUTPUT

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0101 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0101 INPUT.
IF sy-ucomm = 'BACK'.
* SET SCREEN '0100'.
LEAVE TO SCREEN 0.
ELSEIF sy-ucomm = 'CANCEL' OR sy-ucomm = 'EXIT'.
LEAVE PROGRAM.
ENDIF.
ENDMODULE. " USER_COMMAND_0101 INPUT
*&---------------------------------------------------------------------*
*& Form display_vbak_alv
*&---------------------------------------------------------------------*
FORM display_vbak_alv .
CHECK t_vbak IS NOT INITIAL.

IF r_grid_vbak IS INITIAL. See Note-1


PERFORM populate_vbak_layo.

PERFORM populate_vbak_fcat.

CREATE OBJECT r_ccontainer_vbak


EXPORTING
container_name = 'CC_VBAK'.

CREATE OBJECT r_grid_vbak


EXPORTING
i_parent = r_ccontainer_vbak.

CALL METHOD r_grid_vbak->set_table_for_first_display See Note-2


EXPORTING
is_layout = s_layo_vbak
CHANGING
it_outtab = t_vbak
it_fieldcatalog = t_fcat_vbak.
ELSE.
CALL METHOD r_grid_vbak->refresh_table_display. See Note-2
ENDIF.
ENDFORM. " display_vbak_alv

*&---------------------------------------------------------------------*
*& Form populate_vbak_layo
*&---------------------------------------------------------------------*
FORM populate_vbak_layo .
CLEAR s_layo_vbak.
s_layo_vbak-zebra = 'X'.
s_layo_vbak-cwidth_opt = 'X'.
ENDFORM. " populate_vbak_layo

*&---------------------------------------------------------------------*
*& Form populate_vbak_fcat
*&---------------------------------------------------------------------*
FORM populate_vbak_fcat .
DATA: ls_fcat TYPE lvc_s_fcat.
REFRESH t_fcat_vbak.

ls_fcat-fieldname = 'KUNNR'.
ls_fcat-coltext = 'Customer'.
APPEND ls_fcat TO t_fcat_vbak.
CLEAR ls_fcat.

ls_fcat-fieldname = 'VBELN'.
ls_fcat-coltext = 'Order Num.'.
APPEND ls_fcat TO t_fcat_vbak.
CLEAR ls_fcat.

ls_fcat-fieldname = 'NETWR'.
ls_fcat-coltext = 'Net Value'.
APPEND ls_fcat TO t_fcat_vbak.
CLEAR ls_fcat.
ENDFORM. " populate_vbak_fcat

NOTES:
Note-1: As the user of the report keeps moving back and forth in the interactive report the PBO modules
of the respective screen get invoked repeatedly. The objects of CL_GUI_CUSTOM_CONTAINER and
that of CL_GUI_ALV_GRID should be created only when the report is displayed for the first time and not
every time the user moves back and forth. For this reason always verify if the grid is initial or not, and
create the objects only when it has not yet been created. Also, populate the field catalog table, layout
structure; sort table etc only the first time and not every time PBO gets executed.

Note-2: If you want to refresh the data displayed in the output table, use method refresh_table_display.
Method set_table_for_first_display must only be called a second time if the structure of the output table
changes.

Note-3: After a screen transition, when you come back to the screen with the ALV, the scroll information
is lost. Say, you scroll down an ALV report and double click on the 70th row to navigated to the next
report; when you again navigate back the former ALV report doesnt retain the scroll position. To retain
the scroll position on navigating back to the former ALV report add the below snippets of code to the
above program.

Snippet 1: Add these variable declarations.

*&--- Declarations for retaining scroll position


DATA: s_rowinfo TYPE lvc_s_row,
s_colinfo TYPE lvc_s_col.

Snippet 2: In the event handler method, just before the READ statement add the below snippet of code.

*&--- Code to capture scroll position - Start


IF r_grid_kna1 IS NOT INITIAL.
CALL METHOD r_grid_kna1->get_scroll_info_via_id
IMPORTING
es_row_info = s_rowinfo
es_col_info = s_colinfo.
ENDIF.
*&--- Code to capture scroll position End

Snippet 3: In the MODULE user_command_0101 INPUT pertaining to PAI of screen 101, while handling
the function code BACK, add the below snippet of code to set the scroll position and then have the code
to navigate back to the former ALV report.

IF sy-ucomm = 'BACK'.
* SET SCREEN '0100'.
*&--- Code to retain scroll position - Start
CALL METHOD r_grid_kna1->set_scroll_info_via_id
EXPORTING
is_row_info = s_rowinfo
is_col_info = s_colinfo.
*&--- Code to retain scroll position - End

LEAVE TO SCREEN 0.
ELSEIF sy-ucomm = 'CANCEL' OR sy-ucomm = 'EXIT'.
LEAVE PROGRAM.
ENDIF.

You might also like