✒️ABAP El ALV Grid Control
ABAP El ALV Grid Control
ALV Grid Control
In SAP, you can create Z controls using ABAP objects. One of these controls is the ALV GRID CONTROL, which allows you to create ALV reports using objects. With this control, you have access to all standard menu bar functions and can configure all ALV properties to suit your needs. To create the ALV, we use a Container, represented by the class CL_GUI_CUSTOMER_CONTAINER, which provides the physical area for object visualization. This container can hold other controls besides the ALV GRID CONTROL, such as Tree Control, SAP Picture Control, SAP Splitter Control, and more.
The container provides a physical area for object visualization. All controls exist within a container.
Two objects are used to manage Z controls and the ALV GRID CONTROL:
- CL_GUI_CUSTOM_CONTAINER.
- CL_GUI_ALV_GRID.
Note: You can view all attributes, methods, and other configurations of objects through the "Class Builder" transaction SE24.
Steps for Creating ALV Grid Control
- Report Creation: Create a report (ZPRUEBA_ALV_GRID_OBJETOS) and declare:
TYPE-POOLS: slis.
* Output table of ALV
DATA: ti_usuarios TYPE STANDARD TABLE OF ztabla_usuarios,
st_usuarios LIKE LINE OF ti_usuarios.
* Catalog table and structure
DATA: ti_catalogo TYPE lvc_t_fcat,
st_catalogo LIKE LINE OF ti_catalogo,
ok_code TYPE sy-ucomm,
o_grilla TYPE REF TO cl_gui_alv_grid,
o_contenedor TYPE REF TO cl_gui_custom_container.
- Data Retrieval and ALV Display: Perform data selection in the ZTABLA_USUARIOS table in the START-OF-SELECTION event. In the END-OF-SELECTION event, call dynpro 0100, which stores the ALV.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
PERFORM seleccion_datos.
*----------------------------------------------------------------------*
END-OF-SELECTION.
*----------------------------------------------------------------------*
CALL SCREEN 0100.
- Dynpro and GUI Status Creation: Create Dynpro 0100 and GUI Status 0100 with standard navigation buttons. Assign OK_CODE to the dynpro. Use the Screen Painter through transaction SE80 to create a Custom Control, which serves as the container for our ALV. Then, in the dynpro's element list, assign the name CONTENEDOR to the Custom control. In the dynpro processing logic, create modules:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
MODULE crear_objetos_0100.
PROCESS AFTER INPUT.
MODULE salir_0100.
MODULE user_command_0100.
- Creating Objects: In the CREAR_OBJETOS_0100 module, create the objects O_CONTENEDOR and O_GRILLA:
MODULE crear_objetos_0100 OUTPUT.
IF o_contenedor IS INITIAL.
CREATE OBJECT o_contenedor
EXPORTING
container_name = 'CONTENEDOR'.
CREATE OBJECT o_grilla
EXPORTING
i_parent = o_contenedor.
PERFORM armar_catalogo.
CALL METHOD o_grilla->set_table_for_first_display
CHANGING
it_outtab = ti_usuarios
it_fieldcatalog = ti_catalogo[].
ELSE.
CALL METHOD o_grilla->refresh_table_display
EXPORTING
i_soft_refresh = 'X'
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDIF.
ENDMODULE. " CREAR_OBJETOS_0100 OUTPUT
- Exiting the Program: In the SALIR_0100 module, release objects O_CONTENEDOR and O_GRILLA, and return to the initial screen.
MODULE salir_0100 INPUT.
CALL METHOD o_grilla->free.
CALL METHOD o_contenedor->free.
LEAVE PROGRAM.
ENDMODULE. " SALIR_0100 INPUT
Note: When working with ALV_GRID_CONTROL, it may be convenient to create a selection screen, and based on user parameters, call different dynpros using CALL_SCREEN statement. This program would be a hybrid between a dialog program and a classical report. In this situation, for example, a CALL_SCREEN is made to dynpro 0100, and when the BACK button is pressed, we want to return to the selection screen. Within the BACK button code, execute the statement LEAVE TO SCREEN 0 instead of LEAVE PROGRAM.
E.g
*&---------------------------------------------------------------------*
*& Report ZPRUEBA_ALV_GRID_OBJETOS
*&---------------------------------------------------------------------*
REPORT zprueba_alv_grid_objetos.
TYPE-POOLS: slis.
* Tabla de salida del ALV
DATA: ti_usuarios TYPE STANDARD TABLE OF ztabla_usuarios,
st_usuarios LIKE LINE OF ti_usuarios.
* Tabla y estructura del catalogo
DATA: ti_catalogo TYPE lvc_t_fcat,
st_catalogo LIKE LINE OF ti_catalogo,
ok_code TYPE sy-ucomm,
o_grilla TYPE REF TO cl_gui_alv_grid,
o_contenedor TYPE REF TO cl_gui_custom_container.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
PERFORM seleccion_datos.
*----------------------------------------------------------------------*
END-OF-SELECTION.
*----------------------------------------------------------------------*
CALL SCREEN 0100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS '0100'.
SET TITLEBAR '0100'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module CREAR_OBJETOS_0100 OUTPUT
*----------------------------------------------------------------------*
MODULE crear_objetos_0100 OUTPUT.
IF o_contenedor IS INITIAL.
CREATE OBJECT o_contenedor
EXPORTING
container_name = 'CONTENEDOR'.
CREATE OBJECT o_grilla
EXPORTING
i_parent = o_contenedor.
PERFORM armar_catalogo.
CALL METHOD o_grilla->set_table_for_first_display
CHANGING
it_outtab = ti_usuarios
it_fieldcatalog = ti_catalogo[].
ELSE.
CALL METHOD o_grilla->refresh_table_display
EXPORTING
i_soft_refresh = 'X'
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDIF.
ENDMODULE. " CREAR_OBJETOS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form ARMAR_CATALOGO
*&---------------------------------------------------------------------*
FORM armar_catalogo .
CLEAR ti_catalogo.
REFRESH ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'DNI'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'DNI'.
st_catalogo-scrtext_m = 'DNI'.
st_catalogo-scrtext_l = 'DNI'.
APPEND st_catalogo TO ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'NOMBRE_APE'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'Nombre y Apellido'.
st_catalogo-scrtext_m = 'Nombre y Apellido'.
st_catalogo-scrtext_l = 'Nombre y Apellido'.
st_catalogo-outputlen = 25.
st_catalogo-just = 'L'.
APPEND st_catalogo TO ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'ESTADO_USU'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'Estado usuario'.
st_catalogo-scrtext_m = 'Estado usuario'.
st_catalogo-scrtext_l = 'Estado usuario'.
st_catalogo-outputlen = 14.
st_catalogo-just = 'L'.
APPEND st_catalogo TO ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'DIRECCION'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'Dirección'.
st_catalogo-scrtext_m = 'Dirección'.
st_catalogo-scrtext_l = 'Dirección'.
st_catalogo-outputlen = 25.
st_catalogo-just = 'L'.
APPEND st_catalogo TO ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'F_NACIMIENTO'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'Fecha Nac.'.
st_catalogo-scrtext_m = 'Fecha Nac.'.
st_catalogo-scrtext_l = 'Fecha Nac.'.
st_catalogo-outputlen = 12.
st_catalogo-just = 'L'.
APPEND st_catalogo TO ti_catalogo.
CLEAR st_catalogo.
st_catalogo-fieldname = 'ESTADO_CIVIL'.
st_catalogo-tabname = 'TI_USUARIOS'.
st_catalogo-scrtext_s = 'Estado civil'.
st_catalogo-scrtext_m = 'Estado civil'.
st_catalogo-scrtext_l = 'Estado civil'.
st_catalogo-outputlen = 12.
st_catalogo-just = 'L'.
APPEND st_catalogo TO ti_catalogo.
ENDFORM. " ARMAR_CATALOGO
*&---------------------------------------------------------------------*
*& Module SALIR_0100 INPUT
*----------------------------------------------------------------------*
MODULE salir_0100 INPUT.
CALL METHOD o_grilla->free.
CALL METHOD o_contenedor->free.
LEAVE PROGRAM.
ENDMODULE. " SALIR_0100 INPUT
*&---------------------------------------------------------------------*
*& Form SELECCION_DATOS
*&---------------------------------------------------------------------*
FORM seleccion_datos .
CLEAR ti_usuarios.
REFRESH ti_usuarios.
SELECT dni nombre_ape estado_usu direccion codigo_postal
f_nacimiento estado_civil
FROM ztabla_usuarios
INTO CORRESPONDING FIELDS OF TABLE ti_usuarios.
ENDFORM. " SELECCION_DATOS
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 118 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez