Example Usage of CL_GUI_ALV_GRID:
The CL_GUI_ALV_GRID class is used to create and manage an ALV Grid Control in SAP ABAP. This allows for more interactive and flexible ALV reports. Below is an example demonstrating how to use the CL_GUI_ALV_GRID class to display an ALV grid on a custom container.
This example demonstrates how to create and display an interactive ALV grid using the CL_GUI_ALV_GRID class, showcasing the display of data in a flexible and user-friendly manner.
Code Example:
REPORT z_alv_grid_example.
TABLES: spfli.
TYPE-POOLS: slis.
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_alv_grid TYPE REF TO cl_gui_alv_grid,
gt_fieldcat TYPE slis_t_fieldcat_alv,
gs_fieldcat TYPE slis_fieldcat_alv,
gt_spfli TYPE TABLE OF spfli,
gv_repid TYPE sy-repid,
gs_layout TYPE lvc_s_layo.
* Fetch data from the SPFLI table
SELECT * FROM spfli INTO TABLE gt_spfli.
* Define field catalog
gs_fieldcat-fieldname = 'CARRID'.
gs_fieldcat-seltext_m = 'Carrier ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CONNID'.
gs_fieldcat-seltext_m = 'Connection ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CITYFROM'.
gs_fieldcat-seltext_m = 'Departure City'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'CITYTO'.
gs_fieldcat-seltext_m = 'Arrival City'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'FLTIME'.
gs_fieldcat-seltext_m = 'Flight Time'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'DISTANCE'.
gs_fieldcat-seltext_m = 'Distance'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'DISTID'.
gs_fieldcat-seltext_m = 'Distance ID'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_fieldcat-fieldname = 'PLANETYPE'.
gs_fieldcat-seltext_m = 'Plane Type'.
APPEND gs_fieldcat TO gt_fieldcat.
* Define screen and container
gv_repid = sy-repid.
CALL SCREEN 100.
MODULE status_0100 OUTPUT.
SET PF-STATUS 'MAIN100'.
ENDMODULE.
MODULE user_command_0100 INPUT.
PERFORM handle_user_command USING sy-ucomm.
ENDMODULE.
INITIALIZATION.
PERFORM create_container_and_grid.
PERFORM set_alv_display.
ENDINITIALIZATION.
* Screen 100 layout definition
SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.
SELECTION-SCREEN END OF SCREEN 100.
AT SELECTION-SCREEN OUTPUT.
PERFORM set_alv_display.
* Form to create container and ALV grid
FORM create_container_and_grid.
CREATE OBJECT go_container
EXPORTING
container_name = 'CUSTOM_CONTAINER'.
CREATE OBJECT go_alv_grid
EXPORTING
i_parent = go_container.
ENDFORM.
* Form to set ALV display
FORM set_alv_display.
CALL METHOD go_alv_grid->set_table_for_first_display
EXPORTING
is_layout = gs_layout
CHANGING
it_outtab = gt_spfli
it_fieldcatalog = gt_fieldcat.
ENDFORM.
* Form to handle user commands
FORM handle_user_command USING r_ucomm TYPE sy-ucomm.
CASE r_ucomm.
WHEN 'BACK'.
LEAVE PROGRAM.
WHEN OTHERS.
ENDCASE.
ENDFORM.
Explanation:
1. Data Retrieval:
- The SELECT statement retrieves data from the SPFLI table into the internal table gt_spfli.
2. Field Catalog Definition:
- The field catalog (gt_fieldcat) is defined to specify the columns and their properties for the ALV grid. Each field's metadata is appended to the field catalog.
3. Screen Definition:
- Screen 100 is defined as a subscreen. The ALV grid will be displayed within this subscreen.
4. Module for Status:
- The status_0100 module sets the PF-STATUS for the screen.
5. Module for User Command:
- The user_command_0100 module handles user commands. In this example, the command 'BACK' exits the program.
6. Initialization:
- In the INITIALIZATION event, the custom container (go_container) and the ALV grid control (go_alv_grid) are created.
- The set_alv_display form is called to set the initial display of the ALV grid, passing the internal table gt_spfli and the field catalog gt_fieldcat.
7. AT SELECTION-SCREEN OUTPUT:
- The set_alv_display form is called to display the ALV grid.
8. Form for Creating Container and ALV Grid:
- The create_container_and_grid form creates the custom container and the ALV grid control.
9. Form for Setting ALV Display:
- The set_alv_display form sets the initial display of the ALV grid, calling the set_table_for_first_display method with the internal table gt_spfli and the field catalog gt_fieldcat.
10. Form for Handling User Commands:
- The handle_user_command form handles user commands, such as the 'BACK' command to exit the program.
Screen 100 Layout:
To use this code, you need to create a screen (100) with a custom control named CUSTOM_CONTAINER.
- Go to the Screen Painter (SE51) and create screen 100.
- Add a custom control element and name it CUSTOM_CONTAINER.
- Set the layout as required.