En este post vamos a ver un ejemplo sencillo de como crear un ALV TREE. Es un ejemplo muy sencillo, después se le puede añadir muchisima funcionalidad. Para el ejemplo utilizaremos objetos.
El resultado final al que debemos llegar es el siguiente:
1º) Desde la transacción SE80 creamos el reporte Z_ALV_TREE y añadimos el siguiente código:
*&———————————————————————*
*& 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.
*—————————————————————–*
* SELECTION-SCREEN *
*—————————————————————–*
SELECTION-SCREEN BEGIN OF SCREEN 1001.
SELECTION-SCREEN END OF SCREEN 1001.
*—————————————————————–*
* START-OF-SELECTION *
*—————————————————————–*
START-OF-SELECTION. PERFORM OBTENER_DATOS. IF G_ALV_TREE IS INITIAL.
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’. 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. 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. PERFORM FILL_CATALOG_TREE.
PERFORM BUILD_HIERARCHY_HEADER CHANGING L_HIERARCHY_HEADER. 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.
*—————————————————————–*
* 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.
*———————————————————————*
* 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.
*———————————————————————*
* 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.
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. CALL METHOD G_ALV_TREE->FRONTEND_UPDATE. ENDFORM.
*———————————————————————*
* 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.
*———————————————————————*
* 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.
*———————————————————————*
* 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.
2º) Después de activar el programa debemos crear la dynpro sobre la que vamos a mostrar el ALV. Dentro de esta
Nos paramos sobre la carpeta con mismo nombre, click con el botón derecho del mouse… opción Crear y luego Dynpro:
A continuación se va a abrir el Screen Painter:
Clickeamos en el botón Custom Control ( ) y creamos un cuadrado con el nombre TREE_CONTROL
Finalmente guardamos, activamos, cerramos el Screen Painter. Y por último en la transacción SE80 doble click en la carpeta Z_ALV_TREE y ejecutamos.
Cabe aclarar que este ejemplo trabaja con la tabla SPFLI que se usa comúnmente en los ejemplos.
Recomiendo ordenar todo el código en varios INCLUDE. Bajo mi punto de vista siempre es mejor tener el código ordenado que todo apelotonado.
No hay comentarios:
Publicar un comentario