You are on page 1of 6

A Simple OOP ALV Report ( with Custom Container component in screen )

Steps to code an OOP ALV:


1) Populate an internal table with data to be displayed in the ALV report.

2) SE51 in the screen paint a Custom Container component.

3) In the report program PBO module:


a) Create an object of CL_GUI_CUSTOM_CONTAINER and associate it with the Custom
Container component in the screen.
b) Create an object of CL_GUI_ALV_GRID by setting the object of
CL_GUI_CUSTOM_CONTAINER as its parent.
c) Populate layout properties using the structure LVC_S_LAYO.
d) Populate field catalog properties using the table type LVC_T_FCAT.
e) Invoke the method SET_TABLE_FOR_FIRST_DISPLAY on the object of
CL_GUI_ALV_GRID and pass the layout, field catalog and the internal table with data.

OOP ALV With Custom Container

As part of screen design:


1) Create a screen with screen-number 0100. Also create a menu named MYMENU with
Functional Keys BACK, CANCEL, and EXIT with respective function codes. See MODULE
user_command_0100 INPUT in the code below. In a PBO module set the menu using the
command SET PF-STATUS 'MYMENU'.
2) To the layout of this screen add a customer control and name it CUST_CONT.
3) In a PBO module set the title in the title bar of the report screen, using the command
SET TITLEBAR <Title Name>.

Flow logic of screen 0100:


PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.

PROCESS AFTER INPUT.


MODULE USER_COMMAND_0100.

Element List of screen 0100:


***********
REPORT zoop_alv_variants.

TYPES: BEGIN OF ty_output,


kunnr TYPE kna1-kunnr,
name1 TYPE kna1-name1,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat,
netwr TYPE vbak-netwr,
END OF ty_output.

DATA: t_output TYPE TABLE OF ty_output,


r_cust_cont TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid,
s_layout TYPE lvc_s_layo,
t_fcat TYPE lvc_t_fcat,
ok_code LIKE sy-ucomm.

DATA: g_kunnr TYPE kna1-kunnr.

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.
SELECT a~kunnr
a~name1
b~vbeln
b~erdat
b~netwr
FROM kna1 AS a INNER JOIN
vbak AS b
ON a~kunnr = b~kunnr
INTO TABLE t_output
WHERE a~kunnr IN s_kunnr.

SORT t_output BY kunnr.


CALL SCREEN '100'.

MODULE status_0100 OUTPUT.


SET PF-STATUS 'MYMENU'.
PERFORM populate_layout.
PERFORM populate_fcat.
PERFORM display_alv.
ENDMODULE. " STATUS_0100 OUTPUT

MODULE user_command_0100 INPUT.


IF ok_code = 'BACK'.
LEAVE TO SCREEN 0.
ELSEIF ok_code = 'CANCEL' OR ok_code = 'EXIT'.
LEAVE PROGRAM.
ENDIF.
ENDMODULE. " USER_COMMAND_0100 INPUT
FORM populate_layout .
s_layout-zebra = 'X'.
s_layout-cwidth_opt = 'X'.
ENDFORM. "populate_layout

FORM populate_fcat .
DATA: ls_fcat TYPE lvc_s_fcat.

ls_fcat-fieldname = 'KUNNR'. ls_fcat-coltext = 'Customer'.


APPEND ls_fcat TO t_fcat. CLEAR ls_fcat.

ls_fcat-fieldname = 'NAME1'. ls_fcat-coltext = 'Customer Name'.


APPEND ls_fcat TO t_fcat. CLEAR ls_fcat.

ls_fcat-fieldname = 'VBELN'. ls_fcat-coltext = 'SO Num.'.


APPEND ls_fcat TO t_fcat. CLEAR ls_fcat.

ls_fcat-fieldname = 'ERDAT'. ls_fcat-coltext = 'Created on'.


APPEND ls_fcat TO t_fcat. CLEAR ls_fcat.

ls_fcat-fieldname = 'NETWR'. ls_fcat-coltext = 'Net Value'.


APPEND ls_fcat TO t_fcat. CLEAR ls_fcat.
ENDFORM. "populate_fcat

FORM display_alv .
CREATE OBJECT r_cust_cont
EXPORTING
container_name = 'CUST_CONT'.

CREATE OBJECT r_grid


EXPORTING
i_parent = r_cust_cont.

CALL METHOD r_grid->set_table_for_first_display


EXPORTING
is_layout = s_layout
CHANGING
it_outtab = t_output
it_fieldcatalog = t_fcat.
ENDFORM. "display_alv

Allowing the user to create report variants:


In other words we are allowing the user to save and reuse the layout.

A button can be shown on the grid toolbar, allowing the user to save and reuse a layout. The button looks

like this: .

See also example in SAP standard program BCALV_GRID_09.


To do this, use the parameters IS_VARIANT and I_SAVE of the set_table_for_first_display method.

The IS_VARIANT parameter must have the structure DISVARIANT.

The I_SAVE parameter indicates the options for saving layouts. This parameter can have the
following values:
U Only user specific layouts can be saved
X Only global layouts can be saved
A Both user specific and global layouts can be saved
Space Layouts cannot be saved

In the above code modify the subroutine display_alv as highlighted below:

FORM display_alv.
DATA: ls_variant TYPE disvariant.

CREATE OBJECT r_cust_cont


EXPORTING
container_name = 'CUST_CONT'.

CREATE OBJECT r_grid


EXPORTING
i_parent = r_cust_cont.

ls_variant-report = sy-repid.
CALL METHOD r_grid->set_table_for_first_display
EXPORTING
is_variant = ls_variant
i_save = 'A'
* I_DEFAULT = 'X'
is_layout = s_layout
CHANGING
it_outtab = t_output
it_fieldcatalog = t_fcat.
ENDFORM.

About the parameter - I_DEFAULT:


The purpose of the parameter I_DEFAULT is to either allow or disallow the end-user of the report to
select a layout (variant) as the default layout, whenever the report is executed. If a value of X is passed
to this parameter (the default value for this parameter is X) the user will be allowed to choose a particular
layout as the default layout. The user can do this in the Manage Layout option. On the other hand, if a
space is passed; then the user will not have the option to select a layout as default.
If the parameter I_DEFAULT is assigned a value of X (or if the parameter is not at all used in the call to
the method set_table_for_first_display, the Manage Layouts screen would look as below, where the
user can choose a default layout: [First create two or more layout variants for the report]

i_default = X, or Do not mention this parameter in the method call. The default value for this
parameter is X.

If the parameter I_DEFAULT is assigned a value of space, the Manage Layouts screen would look as
below, where the user has no option choose a default layout:

i_default = space (or) i_default =

Setting the Grid Title:

Modify the subroutine populate_layout as highlighted below:

FORM populate_layout .
s_layout-zebra = 'X'.
s_layout-cwidth_opt = 'X'.
s_layout-grid_title = 'Grid title text comes here.'.
ENDFORM.

You might also like