2011/12/30

OO ALV Reports


ALV Reports with Using ABAP Objects.


1. Using Classes.
2. Using functional Module.

ALV Reporting Using Classes:

ALV - Advance List Viewer or ABAP List Viewer:-

The common features of  reports are column alignment, sorting, filtering, subtotals, totals etc. To implement these, a lot of coding and logic is to be put. To avoid that we can use a concept called ABAP List Viewer (ALV).

Module Pool Screen - SE51-

Custom Control: We have to take the custom Control in SE51 Layout.  (Physical Control)

Container :   CL_GUI_CUSTOM_CONTAINER
ALV Grid:   CL_GUI_ALV_GRID 
                   This screen cannot sit directly on the custom control so we take a container.



Method of Class CL_GUI_ALV_GRID.

                    1. SET_TABLE_FOR_FIRST_DISPLAY  ( Display Internal table data in ALV Grid.)


Field Catalog : Provides the Information of the Internal table fields.
                          Manually generating field catalog by using the table type .
                          LVC_T_FCAT and Line Type Or Structure LVC_S_FCAT.


Coloring Rows:  1. Add non-database field (Clolor) in the Final Internal table.
                            2. Before displaying ALV Grid loop in final internal table set color coding as a value for
                                this column depending on a condition.
                            3. As a part of Layout set the field info_fname the value of this field should be name of
                                non-database column (Color).

Displaying Traffic Lights:

Displaying ALV Cell Value on the Button:-



FORM fldcat.
  CLEAR ls_fcat.
  ls_fcat-fieldname = 'KUNNR'.
  ls_fcat-col_pos = 1.
  ls_fcat-coltext = 'Customer No'.
  ls_fcat-outputlen = 12.
*  ls_fcat-style = cl_gui_alv_grid=>MC_STYLE_BUTTON.
  ls_fcat-style = o_grid->mc_style_button.


Displaying ALV Cell value As Push Button Based On Condition:-


1. declare a additional non-database column in the internal table.

TYPES  : BEGIN OF ty_kna1,
            lt_styl TYPE lvc_t_styl,

           end of ty_kna1.
before the alv grid display  loop the final internal table.


FORM styles .
  DATA : ls_styl TYPE lvc_s_styl.
  LOOP AT lt_kna1 INTO ls_kna1.
    IF ls_kna1-land1 = 'AR' OR
       ls_kna1-land1 = 'IN'.

     ls_styl-fieldname = 'KUNNR'.
     ls_styl-style = cl_gui_alv_grid=>mc_style_button.
     APPEND ls_styl TO ls_kna1-lt_styl.
     MODIFY lt_kna1 FROM ls_kna1
            TRANSPORTING lt_styl.





































Steps:-

1. Create Module pool program with Normal Screen 0100 or SE51.

2. Place the Custom Container Control on the screen with Name (CONTAINR).

3. Declare Reference Variable for Custom Container.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONATINER.

Create Instance for Container Class.
“ PBO (Process before Output)
Create object o_container
Exporting
Container_name = . “Container

4. Declare Reference Variable Variable for ALV Grid.

Data: o_grid type ref to CL_GUI_ALV_GRID.

Create Instance for ALV Grid.

PBO(Process Before Output)
Create object o_grid
Exporting
I_parent = o_container.

For ALV Grid Custom Container object (o_container ) as parent.

5. ALV Grid Instance are ready now. We can call the method for display the data and enjoy the ALV functionality.















6. Displaying the list in the ALV Grid Control.

Required data Collect into It_T001.
Call method o_grid->set_table_for_first_display
Exporting
I_structure_name = ‘T001’ “ (Acts as a field catalog)
Changing
It_outtab = it_T001.

7. Create Transaction Code for the Module Pool Program.
==============================================================================
Program 1 :
To display the first 10 company code detail in ALV Grid using ABAP Object.
*&---------------------------------------------------------------------*
*& Module Pool ZGDEMO_ALV_GRID_OO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

PROGRAM ZGDEMO_ALV_GRID_OO.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID,
IT_T001 LIKE TABLE OF T001.

module STATUS_0100 output.
SET PF-STATUS 'ZSTATUS'.

* CREATE CONTAINER OBJ
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'T001'
CHANGING
IT_OUTTAB = IT_T001. “ Act As a Field Catalog

ENDIF.
ENDIF.

SET TITLEBAR 'ZTITLE'.

endmodule. " STATUS_0100 OUTPUT

module READ_DATA output.

SELECT * INTO TABLE IT_T001
FROM T001 UP TO 10 ROWS.

endmodule. " READ_DATA OUTPUT

module USER_COMMAND_0100 input.
CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
endmodule. " USER_COMMAND_0100 INPUT

Create Transaction Code.

Frequently Used Methods from Class Cl_GUI_ALV_GRID
1. Constructor:-
It’s create Automatically by the system when we use the create object of class cl_gui_alv_grid.
2. Get_selected_rows:-
Get index of selected Rows.
Call method o_grid->get_selected_rows
Importing
Et_index_rows = lvc_t_row
Et_row_no = lvc_t_roid. “ table of index selected rows.
3. Set_filter_criteria:-
Call method o_grid->set_filter_criteria
Exporting
It_filter = lvc_t_filter
4. Set_table_for_first_display
Call method o_grid-> Set_table_for_first_display

5. Refresh_table_display
Call method o_grid-> Refresh_table_display
exporting
is_stable = lvc_s_stbl
i_soft_refresh = 
6. Set_toolbar_interactive
Call method o_grid-> Set_toolbar_interactive
7. Set_user_command
Call method o_grid-> Set_user_command
exporting
i_ucomm = sy-ucomm



Frequently Used Events from Class Cl_GUI_ALV_GRID

1. Print_top_of_list
2. Print_end_of_list
3. Print_top_of_page
4. Print_end_of_page
(Note: to allow out put for end_of_page, you must reserve the several lines for these pages. Field reservelns of structure type lvc_s_prnt and pass this structure with method
Set_table_for_first_display.)

5. Double_click
Get index of selected Rows.
Call method o_grid->get_selected_rows
Importing
Question: How to handled the events and print the events?
Ans: Steps-
1. Define the Local Class (Event Handler Class) to handle the events.
(Define and implement methods to handle the event.)
Class called Event Handler Class.
2. Define a method (Event Handler Methods) for each event to handle.
3. Implement the Event Handler Methods , in write statement provide the output for all the print event.
4. Register the event handler Methods. Line the events and event Handler Methods.


Program 2 :
To display Purchasing Document and the detail in ALV Grid using ABAP Objects and handle the print events and respond accordingly.

PROGRAM ZGDEMO_PRINT_ALV_EVENTS.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.

DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO,
IT_EKPO LIKE TABLE OF EKPO,
WA_PRINT TYPE LVC_S_PRNT.

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
*DEFINE A METHOD(EVENT HANDLER METHOD) FOR EACH PRINT EVENT
METHODS:
HANDLE_TOP_OF_PAGE
FOR EVENT PRINT_TOP_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_END_OF_PAGE
FOR EVENT PRINT_END_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_TOP_OF_LIST
FOR EVENT PRINT_TOP_OF_LIST OF CL_GUI_ALV_GRID,

HANDLE_END_OF_LIST
FOR EVENT PRINT_END_OF_LIST OF CL_GUI_ALV_GRID.

ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

CLASS LCL_EVENT_HANDLER IMPLEMENTATION.

METHOD HANDLE_TOP_OF_PAGE.
WRITE: / 'I''M TOP OF THE PAGE'.
ENDMETHOD. "HANDLE_TOP_OF_PAGE

METHOD HANDLE_END_OF_PAGE.
WRITE: / 'I''M END OF PAGE'.
ENDMETHOD. "HANDLE_END_OF_PAGE

METHOD HANDLE_TOP_OF_LIST.
WRITE: 'I''M TOP OF LIST'.
ENDMETHOD. "HANDLE_TOP_OF_LIST

METHOD HANDLE_END_OF_LIST.
WRITE: ' I''M END OF LIST'.
ENDMETHOD. "HANLE_END_OF_LIST

ENDCLASS. "lcl_event_handler IMPLEMENTATION

MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ZSTATUS'.

CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.

CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
* REVERCE THE LINES FOR THE PRINT_END_OF_PAGE.
WA_PRINT-RESERVELNS = 3.

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING
I_STRUCTURE_NAME = 'EKKO'
CHANGING
IT_OUTTAB = IT_EKKO.

DATA O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.

CREATE OBJECT O_HANDLER.

*REGISTRAING THE EVENTS

SET HANDLER : O_HANDLER->HANDLE_TOP_OF_LIST FOR O_GRID,
O_HANDLER->HANDLE_TOP_OF_PAGE FOR O_GRID,
O_HANDLER->HANDLE_END_OF_PAGE FOR O_GRID,
O_HANDLER->HANDLE_END_OF_LIST FOR O_GRID.
ENDIF.
ENDIF.
* SET TITLEBAR 'xxx'.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM.
WHEN 'BANCK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Now create transaction code for the Module Pool program.

(Now Execute the program click on Print ---> enter-> note down the spool request number (ex: 10900) ---> Run Transaction code- [sp01] for spool request selction screen and give the number (10900) and click on print and click the ABAP list.)

Question: How the User Can interact with ALV Grid?
Ans: User can interact with ALV Grid in two ways.
1. Line / Field selection.
Line selection event triggered is Double_click which return the row index and the column name.
2. Function Key.
Interaction through standard / custom user command (button on the ALV toolbar) event triggered is user_command.


Working With Double_Click
Program 3: Display the purchasing documents as the Basic ALV grid and display the Items of the Selected (Double_clicked) purchasing document in Another ALV.

PROGRAM ZGDEMO_OOALV_DOUBLE_CLICK.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.

DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO.

DATA: IT_EKPO LIKE TABLE OF EKPO.
DATA: WA_LAYOUT TYPE LVC_S_LAYO. " DETERMINE THE PROPERTIES OF THE GRID CONTROL. THE LAYOUT STRUCTURE HAS NOTHING
" TO DO WITH THE LAYOUT FOR SAVING FILTER, SORT AND COLOUMN PROPERTIES.
" LVC_S_LAYO STRUCTURE OF TYPE.
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
METHODS: HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW.
ENDCLASS.


CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
METHOD HANDLE_DOUBLE_CLICK.
READ TABLE IT_EKKO INTO WA_EKKO INDEX E_ROW-INDEX.
SET PARAMETER ID 'BES' FIELD WA_EKKO-EBELN.

CALL TRANSACTION 'ME23N'.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ZSTATUS'.
* SET TITLEBAR 'xxx'.
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
WA_LAYOUT-SEL_MODE = 'D'. "MULTIPLE ROWS & COLOUMN

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKKO'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = IT_EKKO.

DATA: O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
CREATE OBJECT O_HANDLER.
*REGISTER THE EVENT

SET HANDLER: O_HANDLER->HANDLE_DOUBLE_CLICK FOR O_GRID.

ENDIF.
ENDIF.


ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module READ_DATA OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Create Transaction Code.

Working With interactive event User_Command
User_command: User command is the event triggered for user interaction through function key.

Program 4:
Display the purchasing documents as the Basic ALV grid and Add a new Button DETAILS , When user Interacts With the button Details It should display the Line Items of all the Purchasing Document from the Basic Alv As secondary ALV.

PROGRAM ZGDEMO_OOALV_USER_COMMAND.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO.

DATA: IT_EKPO LIKE TABLE OF EKPO,
WA_EKPO LIKE EKPO.

DATA: WA_LAYOUT TYPE LVC_S_LAYO.

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
* DEFINE A METHOD FOR EACH EVENT TO BE HANDLED.
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.
ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
METHOD HANDLE_TOOLBAR.

DATA: WA_TOOLBAR TYPE STB_BUTTON.
CLEAR WA_TOOLBAR.

WA_TOOLBAR-FUNCTION = 'DETAIL'.
WA_TOOLBAR-ICON = 'ICON-DETAIL'.
WA_TOOLBAR-TEXT = 'DETAILED DATA'.
APPEND WA_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD. "LCL_EVENT_HANDLER

METHOD HANDLE_USER_COMMAND.
CASE E_UCOMM.
WHEN 'DETAIL'.
SELECT * INTO TABLE IT_EKPO FROM EKPO
FOR ALL ENTRIES IN IT_EKKO
WHERE EBELN = IT_EKKO-EBELN.

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKPO'
CHANGING
IT_OUTTAB = IT_EKPO.

ENDCASE.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'ZSTATUS'.
* SET TITLEBAR 'xxx'.
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
WA_LAYOUT-SEL_MODE = 'D'.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKKO'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = IT_EKKO.

* CREATE OBJECT FOR EVENT HANDLER CLASS

DATA: O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
CREATE OBJECT O_HANDLER.
* REGISTRING THE EVENT

SET HANDLER : O_HANDLER->HANDLE_TOOLBAR FOR O_GRID,
O_HANDLER->HANDLE_USER_COMMAND FOR O_GRID.

* TO TRIGGERED THE EVENT
CALL METHOD O_GRID->SET_TOOLBAR_INTERACTIVE.
ENDIF.
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
*& Module READ_DATA OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE SY-UCOMM.

WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Create Transaction Code.

92 PAGE TO CONT………….





ALV Reports with Using ABAP Objects.
Steps:-
1. Create Module pool program with Normal Screen 0100.

2. Place the Custom Container Control on the screen with Name (CONTAINR).

3. Declare Reference Variable for Custom Container.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONATINER.

Create Instance for Container Class.
“ PBO (Process before Output)
Create object o_container
Exporting
Container_name = . “Container

4. Declare Reference Variable Variable for ALV Grid.

Data: o_grid type ref to CL_GUI_ALV_GRID.

Create Instance for ALV Grid.

PBO(Process Before Output)
Create object o_grid
Exporting
I_parent = o_container.

For ALV Grid Custom Container object (o_container ) as parent.

5. ALV Grid Instance are ready now. We can call the method for display the data and enjoy the ALV functionality.















6. Displaying the list in the ALV Grid Control.

Required data Collect into It_T001.
Call method o_grid->set_table_for_first_display
Exporting
I_structure_name = ‘T001’ “ (Acts as a field catalog)
Changing
It_outtab = it_T001.

7. Create Transaction Code for the Module Pool Program.
==============================================================================
Program 1 :
To display the first 10 company code detail in ALV Grid using ABAP Object.
*&---------------------------------------------------------------------*
*& Module Pool ZGDEMO_ALV_GRID_OO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

PROGRAM ZGDEMO_ALV_GRID_OO.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID,
IT_T001 LIKE TABLE OF T001.

module STATUS_0100 output.
SET PF-STATUS 'ZSTATUS'.

* CREATE CONTAINER OBJ
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'T001'
CHANGING
IT_OUTTAB = IT_T001. “ Act As a Field Catalog

ENDIF.
ENDIF.

SET TITLEBAR 'ZTITLE'.

endmodule. " STATUS_0100 OUTPUT

module READ_DATA output.

SELECT * INTO TABLE IT_T001
FROM T001 UP TO 10 ROWS.

endmodule. " READ_DATA OUTPUT

module USER_COMMAND_0100 input.
CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
endmodule. " USER_COMMAND_0100 INPUT

Create Transaction Code.

Frequently Used Methods from Class Cl_GUI_ALV_GRID
1. Constructor:-
It’s create Automatically by the system when we use the create object of class cl_gui_alv_grid.
2. Get_selected_rows:-
Get index of selected Rows.
Call method o_grid->get_selected_rows
Importing
Et_index_rows = lvc_t_row
Et_row_no = lvc_t_roid. “ table of index selected rows.
3. Set_filter_criteria:-
Call method o_grid->set_filter_criteria
Exporting
It_filter = lvc_t_filter
4. Set_table_for_first_display
Call method o_grid-> Set_table_for_first_display

5. Refresh_table_display
Call method o_grid-> Refresh_table_display
exporting
is_stable = lvc_s_stbl
i_soft_refresh = 
6. Set_toolbar_interactive
Call method o_grid-> Set_toolbar_interactive
7. Set_user_command
Call method o_grid-> Set_user_command
exporting
i_ucomm = sy-ucomm



Frequently Used Events from Class Cl_GUI_ALV_GRID

1. Print_top_of_list
2. Print_end_of_list
3. Print_top_of_page
4. Print_end_of_page
(Note: to allow out put for end_of_page, you must reserve the several lines for these pages. Field reservelns of structure type lvc_s_prnt and pass this structure with method
Set_table_for_first_display.)

5. Double_click
Get index of selected Rows.
Call method o_grid->get_selected_rows
Importing
Question: How to handled the events and print the events?
Ans: Steps-
1. Define the Local Class (Event Handler Class) to handle the events.
(Define and implement methods to handle the event.)
Class called Event Handler Class.
2. Define a method (Event Handler Methods) for each event to handle.
3. Implement the Event Handler Methods , in write statement provide the output for all the print event.
4. Register the event handler Methods. Line the events and event Handler Methods.


Program 2 :
To display Purchasing Document and the detail in ALV Grid using ABAP Objects and handle the print events and respond accordingly.

PROGRAM ZGDEMO_PRINT_ALV_EVENTS.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.

DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO,
IT_EKPO LIKE TABLE OF EKPO,
WA_PRINT TYPE LVC_S_PRNT.

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
*DEFINE A METHOD(EVENT HANDLER METHOD) FOR EACH PRINT EVENT
METHODS:
HANDLE_TOP_OF_PAGE
FOR EVENT PRINT_TOP_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_END_OF_PAGE
FOR EVENT PRINT_END_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_TOP_OF_LIST
FOR EVENT PRINT_TOP_OF_LIST OF CL_GUI_ALV_GRID,

HANDLE_END_OF_LIST
FOR EVENT PRINT_END_OF_LIST OF CL_GUI_ALV_GRID.

ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

CLASS LCL_EVENT_HANDLER IMPLEMENTATION.

METHOD HANDLE_TOP_OF_PAGE.
WRITE: / 'I''M TOP OF THE PAGE'.
ENDMETHOD. "HANDLE_TOP_OF_PAGE

METHOD HANDLE_END_OF_PAGE.
WRITE: / 'I''M END OF PAGE'.
ENDMETHOD. "HANDLE_END_OF_PAGE

METHOD HANDLE_TOP_OF_LIST.
WRITE: 'I''M TOP OF LIST'.
ENDMETHOD. "HANDLE_TOP_OF_LIST

METHOD HANDLE_END_OF_LIST.
WRITE: ' I''M END OF LIST'.
ENDMETHOD. "HANLE_END_OF_LIST

ENDCLASS. "lcl_event_handler IMPLEMENTATION

MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ZSTATUS'.

CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.

CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
* REVERCE THE LINES FOR THE PRINT_END_OF_PAGE.
WA_PRINT-RESERVELNS = 3.

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING
I_STRUCTURE_NAME = 'EKKO'
CHANGING
IT_OUTTAB = IT_EKKO.

DATA O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.

CREATE OBJECT O_HANDLER.

*REGISTRAING THE EVENTS

SET HANDLER : O_HANDLER->HANDLE_TOP_OF_LIST FOR O_GRID,
O_HANDLER->HANDLE_TOP_OF_PAGE FOR O_GRID,
O_HANDLER->HANDLE_END_OF_PAGE FOR O_GRID,
O_HANDLER->HANDLE_END_OF_LIST FOR O_GRID.
ENDIF.
ENDIF.
* SET TITLEBAR 'xxx'.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM.
WHEN 'BANCK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Now create transaction code for the Module Pool program.

(Now Execute the program click on Print ---> enter-> note down the spool request number (ex: 10900) ---> Run Transaction code- [sp01] for spool request selction screen and give the number (10900) and click on print and click the ABAP list.)

Question: How the User Can interact with ALV Grid?
Ans: User can interact with ALV Grid in two ways.
1. Line / Field selection.
Line selection event triggered is Double_click which return the row index and the column name.
2. Function Key.
Interaction through standard / custom user command (button on the ALV toolbar) event triggered is user_command.


Working With Double_Click
Program 3: Display the purchasing documents as the Basic ALV grid and display the Items of the Selected (Double_clicked) purchasing document in Another ALV.

PROGRAM ZGDEMO_OOALV_DOUBLE_CLICK.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.

DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO.

DATA: IT_EKPO LIKE TABLE OF EKPO.
DATA: WA_LAYOUT TYPE LVC_S_LAYO. " DETERMINE THE PROPERTIES OF THE GRID CONTROL. THE LAYOUT STRUCTURE HAS NOTHING
" TO DO WITH THE LAYOUT FOR SAVING FILTER, SORT AND COLOUMN PROPERTIES.
" LVC_S_LAYO STRUCTURE OF TYPE.
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
METHODS: HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW.
ENDCLASS.


CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
METHOD HANDLE_DOUBLE_CLICK.
READ TABLE IT_EKKO INTO WA_EKKO INDEX E_ROW-INDEX.
SET PARAMETER ID 'BES' FIELD WA_EKKO-EBELN.

CALL TRANSACTION 'ME23N'.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ZSTATUS'.
* SET TITLEBAR 'xxx'.
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
WA_LAYOUT-SEL_MODE = 'D'. "MULTIPLE ROWS & COLOUMN

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKKO'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = IT_EKKO.

DATA: O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
CREATE OBJECT O_HANDLER.
*REGISTER THE EVENT

SET HANDLER: O_HANDLER->HANDLE_DOUBLE_CLICK FOR O_GRID.

ENDIF.
ENDIF.


ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module READ_DATA OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Create Transaction Code.

Working With interactive event User_Command
User_command: User command is the event triggered for user interaction through function key.

Program 4:
Display the purchasing documents as the Basic ALV grid and Add a new Button DETAILS , When user Interacts With the button Details It should display the Line Items of all the Purchasing Document from the Basic Alv As secondary ALV.

PROGRAM ZGDEMO_OOALV_USER_COMMAND.

DATA: O_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA: IT_EKKO LIKE TABLE OF EKKO,
WA_EKKO LIKE EKKO.

DATA: IT_EKPO LIKE TABLE OF EKPO,
WA_EKPO LIKE EKPO.

DATA: WA_LAYOUT TYPE LVC_S_LAYO.

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION.

PUBLIC SECTION.
* DEFINE A METHOD FOR EACH EVENT TO BE HANDLED.
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.
ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

*----------------------------------------------------------------------*
* CLASS LCL_EVENT_HANDLER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
METHOD HANDLE_TOOLBAR.

DATA: WA_TOOLBAR TYPE STB_BUTTON.
CLEAR WA_TOOLBAR.

WA_TOOLBAR-FUNCTION = 'DETAIL'.
WA_TOOLBAR-ICON = 'ICON-DETAIL'.
WA_TOOLBAR-TEXT = 'DETAILED DATA'.
APPEND WA_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD. "LCL_EVENT_HANDLER

METHOD HANDLE_USER_COMMAND.
CASE E_UCOMM.
WHEN 'DETAIL'.
SELECT * INTO TABLE IT_EKPO FROM EKPO
FOR ALL ENTRIES IN IT_EKKO
WHERE EBELN = IT_EKKO-EBELN.

CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKPO'
CHANGING
IT_OUTTAB = IT_EKPO.

ENDCASE.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'ZSTATUS'.
* SET TITLEBAR 'xxx'.
CREATE OBJECT O_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

IF SY-SUBRC = 0.
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = O_CONTAINER.

IF SY-SUBRC = 0.
WA_LAYOUT-SEL_MODE = 'D'.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'EKKO'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = IT_EKKO.

* CREATE OBJECT FOR EVENT HANDLER CLASS

DATA: O_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
CREATE OBJECT O_HANDLER.
* REGISTRING THE EVENT

SET HANDLER : O_HANDLER->HANDLE_TOOLBAR FOR O_GRID,
O_HANDLER->HANDLE_USER_COMMAND FOR O_GRID.

* TO TRIGGERED THE EVENT
CALL METHOD O_GRID->SET_TOOLBAR_INTERACTIVE.
ENDIF.
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
*& Module READ_DATA OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE READ_DATA OUTPUT.

SELECT * INTO TABLE IT_EKKO FROM EKKO UP TO 100 ROWS.

ENDMODULE. " READ_DATA OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE SY-UCOMM.

WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Create Transaction Code.

No comments: