![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP La creación de un ALV TREE orientado a objetos
ABAP La creación de un ALV TREE orientado a objetos
![ABAP La creación de un ALV TREE orientado a objetos ABAP La creación de un ALV TREE orientado a objetos](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/60.jpg)
Creating an ALV Tree Using Object Orientation
In this tutorial, we'll walk through the creation of a program, Z_LLENAR_SPFLI, which populates data into the standard table SPFLI containing flight information. Then, using transaction SE38, we'll create the program Z_ALV_TREE.
Key Points:
- Internal Table Declaration: We start by declaring an internal table with 7 fields, which will be displayed on the screen.
- Instance Variables: We declare variables to instantiate two classes, CL_GUI_ALV_TREE and CL_GUI_CUSTOM_CONTAINER.
- Data Retrieval: Inside the START-OF-SELECTION event, we construct the obtener_datos subroutine, which retrieves all data from the SPFLI table.
- Instance Check: We check if the tree has not been instantiated yet, then we instantiate both the tree and the container.
- Catalog Filling: The fill_catalog_tree subroutine fills the catalog.
- Hierarchy Building: We build the data hierarchy of the tree using the build_hierarchy_header and create_hierarchy subroutines.
- Displaying ALV: Finally, we call the method to display the ALV on the screen.
- Dynpro Creation: We also need to create a dynpro where we place a custom control containing the ALV Tree and a GUI status for screen navigation.
- Execution: Execute the program to see the output on the screen.
E.g
*&---------------------------------------------------------------------*
*& Report Z_ALV_TREE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_alv_tree.
*----------------------------------------------------------------------*
* DECLARACION DE TYPES
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_spfli,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
countryto TYPE spfli-countryto,
cityto TYPE spfli-cityto,
END OF ty_spfli.
TYPES: BEGIN OF ty_header,
carrid TYPE spfli-carrid,
END OF ty_header.
*----------------------------------------------------------------------*
* DECLARACION DE TABLAS INTERNAS
*----------------------------------------------------------------------*
DATA: t_spfli TYPE STANDARD TABLE OF ty_spfli WITH HEADER LINE,
tab_tree TYPE STANDARD TABLE OF ty_spfli WITH HEADER LINE,
t_tree TYPE STANDARD TABLE OF ty_spfli WITH HEADER LINE,
t_header TYPE STANDARD TABLE OF ty_header WITH HEADER LINE.
*----------------------------------------------------------------------*
* DECLARACION DE VARIABLES
*----------------------------------------------------------------------*
DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
l_hierarchy_header TYPE treev_hhdr,
gs_fieldcat TYPE lvc_s_fcat,
gt_fieldcat_tree TYPE lvc_t_fcat,
g_custom_container TYPE REF TO cl_gui_custom_container.
*----------------------------------------------------------------------*
* DECLARACION DE FIELD-SYMBOLS
*----------------------------------------------------------------------*
FIELD-SYMBOLS: <header> LIKE LINE OF t_header,
<spfli> LIKE LINE OF t_spfli.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
* Obtenemos los datos que se mostrarán en el ALV
PERFORM obtener_datos.
IF g_alv_tree IS INITIAL.
* Se crearán los objetos correspondientes al CONTAINER y el ALV TREE
PERFORM init_tree.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2.
ENDIF.
CALL SCREEN 100.
*----------------------------------------------------------------------*
* FORM init_tree *
*----------------------------------------------------------------------*
FORM init_tree.
DATA: l_tree_container_name(30) TYPE c.
l_tree_container_name = 'TREE_CONTROL'.
* Creamos el objeto CONTAINER
CREATE OBJECT g_custom_container
EXPORTING
container_name = l_tree_container_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
* Creamos el objeto del ALV TREE
CREATE OBJECT g_alv_tree
EXPORTING
parent = g_custom_container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = ''
no_html_header = 'X'
no_toolbar = ''
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
illegal_node_selection_mode = 5
failed = 6
illegal_column_name = 7.
* Completamos el catálogo
PERFORM fill_catalog_tree.
* Construimos la jerarquia del ALV TREE
PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
* Ejecutamos el método que muestra el ALV
CALL METHOD g_alv_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = l_hierarchy_header
CHANGING
it_outtab = t_tree[]
it_fieldcatalog = gt_fieldcat_tree.
PERFORM create_hierarchy.
ENDFORM. "INIT_TREE
*----------------------------------------------------------------------*
* FORM FILL_CATALOG_TREE
*----------------------------------------------------------------------*
FORM fill_catalog_tree.
DATA: gs_fieldcat_add TYPE lvc_s_fcat.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 1.
gs_fieldcat-reptext = 'Flight'.
gs_fieldcat-scrtext_s = 'Flight'.
gs_fieldcat-fieldname = 'CONNID'.
gs_fieldcat-tabname = 'T_SPFLI'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 2.
gs_fieldcat-reptext = 'Country'.
gs_fieldcat-scrtext_s = 'Country'.
gs_fieldcat-fieldname = 'COUNTRYFR'.
gs_fieldcat-tabname = 'T_SPFLI'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 3.
gs_fieldcat-reptext = 'Dep.City'.
gs_fieldcat-scrtext_s = 'Dep.City'.
gs_fieldcat-fieldname = 'CITYFROM'.
gs_fieldcat-tabname = 'T_SPFLI'.
gs_fieldcat-outputlen = '15'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 4.
gs_fieldcat-reptext = 'Dep.Airp'.
gs_fieldcat-scrtext_s = 'Dep.Airp'.
gs_fieldcat-fieldname = 'AIRPFROM'.
gs_fieldcat-tabname = 'T_SPFLI'.
gs_fieldcat-outputlen = '20'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 5.
gs_fieldcat-reptext = 'Country'.
gs_fieldcat-scrtext_s = 'Country'.
gs_fieldcat-fieldname = 'COUNTRYTO'.
gs_fieldcat-tabname = 'T_SPFLI'.
gs_fieldcat-outputlen = '15'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 6.
gs_fieldcat-reptext = 'Arr.City'.
gs_fieldcat-scrtext_s = 'Arr.City'.
gs_fieldcat-fieldname = 'CITYTO'.
gs_fieldcat-tabname = 'T_SPFLI'.
gs_fieldcat-outputlen = '20'.
APPEND gs_fieldcat TO gt_fieldcat_tree.
ENDFORM. "FILL_CATALOG_TREE
*----------------------------------------------------------------------*
* FORM build_hierarchy_header
*----------------------------------------------------------------------*
FORM build_hierarchy_header CHANGING p_hierarchy_header TYPE treev_hhdr.
CLEAR p_hierarchy_header.
p_hierarchy_header-heading = 'Airline carrier'(300).
p_hierarchy_header-width = 18.
p_hierarchy_header-width_pix = ' '.
ENDFORM. "BUILD_HIERARCHY_HEADER
*----------------------------------------------------------------------*
* FORM create_hierarchy *
*----------------------------------------------------------------------*
FORM create_hierarchy.
DATA: l_root_key TYPE lvc_nkey,
l_next_key TYPE lvc_nkey,
l_last_key TYPE lvc_nkey,
header TYPE string,
w_menge_text(13) TYPE c.
CLEAR l_root_key.
LOOP AT t_header ASSIGNING <header>.
CLEAR l_root_key.
CLEAR l_next_key.
* Agregamos un nuevo nodo en el ALV TREE
PERFORM add_node USING <header>-carrid l_root_key
CHANGING l_next_key.
LOOP AT t_spfli ASSIGNING <spfli> WHERE carrid EQ <header>-carrid.
MOVE-CORRESPONDING <spfli> TO tab_tree.
PERFORM add_leaf USING tab_tree l_next_key
CHANGING l_last_key.
ENDLOOP.
ENDLOOP.
* Actualizamos el ALV TREE
CALL METHOD g_alv_tree->frontend_update.
ENDFORM. "CREATE_HIERARCHY
*----------------------------------------------------------------------*
* FORM ADD_NODE
*----------------------------------------------------------------------*
FORM add_node USING l_name l_root_key CHANGING l_next_key.
DATA: l_node_text TYPE lvc_value,
ls_tree TYPE ty_spfli.
l_node_text = l_name.
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = l_root_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_outtab_line = ls_tree
IMPORTING
e_new_node_key = l_next_key.
ENDFORM. "ADD_NODE
*----------------------------------------------------------------------*
* FORM ADD_LEAF *
*----------------------------------------------------------------------*
FORM add_leaf USING l_tree TYPE ty_spfli
l_next_key
CHANGING l_last_key.
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = l_next_key
i_relationship = cl_gui_column_tree=>relat_last_child
is_outtab_line = tab_tree
IMPORTING
e_new_node_key = l_last_key.
ENDFORM. "ADD_LEAF
*----------------------------------------------------------------------*
* FORM OBTENER_DATOS
*----------------------------------------------------------------------*
FORM obtener_datos.
SELECT carrid connid countryfr cityfrom
airpfrom countryto cityto
INTO TABLE t_spfli
FROM spfli.
IF sy-subrc EQ 0.
LOOP AT t_spfli ASSIGNING <spfli>.
MOVE <spfli>-carrid TO t_header-carrid.
APPEND t_header.
ENDLOOP.
SORT t_header.
DELETE ADJACENT DUPLICATES FROM t_header.
ENDIF.
ENDFORM. "OBTENER_DATOS
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS '0100'.
SET TITLEBAR '0100'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module EXIT_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE exit_command_0100 INPUT.
LEAVE PROGRAM.
ENDMODULE. " EXIT_COMMAND_0100 INPUT
 
 
 
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: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
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