ALV TREE
posted under
REPORTES - ALV
by Antonio Lopez
Este Programa permite crear un ALV TREE en un customcontrol, debemos tener customcontrol en nuestra dynproo llamado "CCONTAINER".
En este ejemplo utilizo una tabla llamada zrf_t0000, cuya estructura es la siguiente.
MANDT-----------MANDT
ID--------------NUMC
DEPARTAMENTOS---CHAR
PANTALLAS-------CHAR
TRANSACCIONES---CHAR
CODIGO:
********************************
Screen Flow Logic:
********************************
PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.
--------------------------------
**********************
*INCLUDE TOP
**********************
*&---------------------------------------------------------------------*
*& Include ZALVTREETOP *
*& *
*&---------------------------------------------------------------------*
PROGRAM ZALVTREE.
tables:
ZRF_T0000.
include
DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
g_custom_container TYPE REF TO cl_gui_custom_container.
DATA: gt_TONO TYPE ZRF_T0000 OCCURS 0, "Output-Table
ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm, "OK-Code
g_max type i value 255,
lztono like line of GT_tono.
DATA:
CAT_CAMPOS TYPE LVC_T_FCAT,
LCAT_CAMPOS LIKE LINE OF CAT_CAMPOS.
*---------------------------------------------------------------
* LOCAL CLASSES
*---------------------------------------------------------------
*§1. Define a (local) class for event handling
CLASS lcl_tree_event_receiver DEFINITION.
PUBLIC SECTION.
*§2. Define an event handler method for each event you want to react to.
METHODS handle_node_double_click
FOR EVENT node_double_click OF cl_gui_alv_tree
IMPORTING node_key sender.
* 'sender' is an implicit event parameter that is provided by
* ABAP Objects runtime system. It contains a reference to the
* object that fired the event. You may directly use it to
* call methods of this instance.
ENDCLASS.
******************************************************************
CLASS lcl_tree_event_receiver IMPLEMENTATION.
*§3. Implement your event handler methods.
METHOD handle_node_double_click.
data: lt_children type lvc_t_nkey.
data temp type p.
DATA: e_node_text type LVC_VALUE.
DATA: TRANSACCION(10) TYPE C.
*first check if the node is a leaf, i.e. can not be expanded
call method sender->get_children
exporting i_node_key = node_key
importing et_children = lt_children.
if lt_children is initial.
****************************************************************
CALL METHOD sender->get_outtab_line
EXPORTING i_node_key = node_key
IMPORTING e_node_text = e_node_text.
SELECT TRANSACCIONES INTO TRANSACCION
FROM ZRF_T0000
WHERE PANTALLAS = e_node_text.
ENDSELECT.
CALL TRANSACTION TRANSACCION.
****************************************************************
endif.
ENDMETHOD.
ENDCLASS.
**********************
* INCLUDE OUTPUT
**********************
*----------------------------------------------------------------------*
* INCLUDE ZALVTREEO01 *
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'GUI'.
SET TITLEBAR 'MAINTITLE'.
IF g_alv_tree IS INITIAL.
PERFORM init_tree.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS cntl_system_error = 1
cntl_error = 2.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = 'Automation Queue failure'(801)
txt1 = 'Internal error:'(802)
txt2 = 'A method in the automation queue'(803)
txt3 = 'caused a failure.'(804).
ENDIF.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
******************
*INCLUDE IN
******************
*----------------------------------------------------------------------*
* INCLUDE ZALVTREEI01 *
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module USER_COMMAND_0100 input.
CASE SY-UCOMM.
WHEN 'CANC' OR 'EXIT' OR 'BACK'.
LEAVE PROGRAM.
WHEN OTHERS.
* §6. Call dispatch to process toolbar functions
CALL METHOD cl_gui_cfw=>dispatch.
ENDCASE.
CALL METHOD cl_gui_cfw=>flush.
endmodule. " USER_COMMAND_0100 INPUT
***********************
*INCLUDE FORMS
***********************
*----------------------------------------------------------------------*
* INCLUDE ZALVTREEF01 *
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form init_tree
*&---------------------------------------------------------------------*
* INICIALIZACION DEL TREE
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form init_tree.
* create container for alv-tree
* DATA: l_tree_container_name(30) TYPE c.
CREATE OBJECT g_custom_container
EXPORTING
container_name = 'CCONTAINER'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
IF sy-subrc <> 0.
MESSAGE x208(00) WITH 'ERROR'(100).
ENDIF.
* create tree control
CREATE OBJECT g_alv_tree
EXPORTING
parent = g_custom_container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
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.
IF sy-subrc <> 0.
MESSAGE x208(00) WITH 'ERROR'. "#EC NOTEXT
ENDIF.
* §2. Create Hierarchy-header
* The simple ALV Tree uses the text of the fields which were used
* for sorting to define this header. When you use
* the 'normal' ALV Tree the hierarchy is build up freely
* by the programmer this is not possible, so he has to define it
* himself.
DATA l_hierarchy_header TYPE treev_hhdr.
PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
* §3. Create empty Tree Control
* IMPORTANT: Table 'gt_sflight' must be empty. Do not change this table
* (even after this method call). You can change data of your table
* by calling methods of CL_GUI_ALV_TREE.
* Furthermore, the output table 'gt_outtab' must be global and can
* only be used for one ALV Tree Control.
PERFORM CATCAMPOS.
* create info-table for html-header
data: lt_list_commentary type slis_t_listheader,
l_logo type sdydo_value.
perform build_comment using
lt_list_commentary
l_logo.
data: ls_variant type disvariant.
ls_variant-report = sy-repid.
CALL METHOD g_alv_tree->set_table_for_first_display
EXPORTING
i_structure_name = 'ZRF_T0000'
is_hierarchy_header = l_hierarchy_header
it_list_commentary = lt_list_commentary
i_logo = l_logo
i_background_id = 'ALV_BACKGROUND'
is_variant = ls_variant
i_save = 'A'
CHANGING
it_outtab = gt_TONO "table must be empty !
it_fieldcatalog = CAT_CAMPOS.
* §4. Create hierarchy (nodes and leaves)
PERFORM create_hierarchy.
PERFORM register_events.
* §5. Send data to frontend.
CALL METHOD g_alv_tree->frontend_update.
* wait for automatic flush at end of pbo
ENDFORM. " init_tree
FORM CATCAMPOS.
REFRESH CAT_CAMPOS.
CLEAR LCAT_CAMPOS.
LCAT_CAMPOS-FIELDNAME = 'DEPARTAMENTOS'.
LCAT_CAMPOS-REF_TABLE = 'ZRF_T0000'.
LCAT_CAMPOS-COLTEXT = 'DEPARTAMENTO'.
LCAT_CAMPOS-OUTPUTLEN = 10.
LCAT_CAMPOS-EMPHASIZE = 'C200'.
*LCAT_CAMPOS-NO_OUT = 'X'.
APPEND LCAT_CAMPOS TO CAT_CAMPOS.
CLEAR LCAT_CAMPOS.
LCAT_CAMPOS-FIELDNAME = 'PANTALLAS'.
LCAT_CAMPOS-REF_TABLE = 'ZRF_T0000'.
LCAT_CAMPOS-COLTEXT = 'PROCESO'.
LCAT_CAMPOS-OUTPUTLEN = 10.
LCAT_CAMPOS-EMPHASIZE = 'C200'.
*LCAT_CAMPOS-NO_OUT = 'X'.
APPEND LCAT_CAMPOS TO CAT_CAMPOS.
CLEAR LCAT_CAMPOS.
LCAT_CAMPOS-FIELDNAME = 'ID'.
LCAT_CAMPOS-REF_TABLE = 'ZRF_T0000'.
LCAT_CAMPOS-COLTEXT = 'ID'.
LCAT_CAMPOS-OUTPUTLEN = 10.
LCAT_CAMPOS-EMPHASIZE = 'C200'.
*LCAT_CAMPOS-NO_OUT = 'X'.
APPEND LCAT_CAMPOS TO CAT_CAMPOS.
CLEAR LCAT_CAMPOS.
LCAT_CAMPOS-FIELDNAME = 'TRANSACCIONES'.
LCAT_CAMPOS-REF_TABLE = 'ZRF_T0000'.
LCAT_CAMPOS-COLTEXT = 'TRANSACCION'.
LCAT_CAMPOS-OUTPUTLEN = 10.
LCAT_CAMPOS-EMPHASIZE = 'C200'.
*LCAT_CAMPOS-NO_OUT = 'X'.
APPEND LCAT_CAMPOS TO CAT_CAMPOS.
endform. " init_tree
**&---------------------------------------------------------------------
**
**& Form build_hierarchy_header
**&---------------------------------------------------------------------
**
** build hierarchy-header-information
**----------------------------------------------------------------------
**
** -->P_L_HIERARCHY_HEADER strucxture for hierarchy-header
**----------------------------------------------------------------------
**
FORM build_hierarchy_header CHANGING
p_hierarchy_header TYPE treev_hhdr.
p_hierarchy_header-heading = 'DEPARTAMENTOS'(300).
p_hierarchy_header-tooltip = 'Escoga un departamento...'(400).
p_hierarchy_header-width = 10.
p_hierarchy_header-width_pix = ' '.
ENDFORM. " build_hierarchy_header
*&---------------------------------------------------------------------*
*& Form build_comment
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LT_LIST_COMMENTARY text
* -->P_L_LOGO text
*----------------------------------------------------------------------*
form build_comment using
pt_list_commentary type slis_t_listheader
p_logo type sdydo_value.
data: ls_line type slis_listheader.
GET TIME.
* LIST HEADING LINE: TYPE H
clear ls_line.
ls_line-typ = 'H'.
* LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-info = 'Rodocletas Feliz S.A. de C.V.'. "#EC NOTEXT
append ls_line to pt_list_commentary.
* STATUS LINE: TYPE S
clear ls_line.
ls_line-typ = 'S'.
ls_line-key = 'valid until'. "#EC NOTEXT
ls_line-info = SY-DATUM. "#EC NOTEXT
append ls_line to pt_list_commentary.
ls_line-key = 'time'.
ls_line-info = SY-UZEIT. "#EC NOTEXT
append ls_line to pt_list_commentary.
* ACTION LINE: TYPE A
clear ls_line.
ls_line-typ = 'A'.
* LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-info = 'Seleccione un departamento y su transaccion'."NOTEXT
append ls_line to pt_list_commentary.
p_logo = 'ENJOYSAP_LOGO'.
endform. " build_comment
*&---------------------------------------------------------------------*
*& Form create_hierarchy
*&---------------------------------------------------------------------*
* CREACION DE JERARQUIA
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form create_hierarchy.
DATA: ls_TONO TYPE ZRF_T0000,
lt_TONO TYPE ZRF_T0000 OCCURS 0,
ldepartamento type ZRF_T0000-departamentos,
ldepartamento_anterior type ZRF_T0000-departamentos,
lpantalla type ZRF_T0000-pantallas,
lpantalla_anterior type ZRF_T0000-pantallas.
DATA: l_VISTA TYPE lvc_nkey,
l_DEPARTAMENTO_key TYPE lvc_nkey,
l_PANTALLA_key TYPE lvc_nkey,
l_last_key TYPE lvc_nkey.
* §4a. Select data
SELECT * FROM ZRF_T0000 INTO TABLE lt_TONO up to g_max rows.
* §4b. Sort output table according to your conceived hierarchy
* We sort in this order:
* departamento and pantalla (top level nodes)
* carrier id (next level)
SORT lt_TONO BY DEPARTAMENTOS pantallas.
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.
* §4c. Add data to tree
LOOP AT lt_TONO INTO ls_TONO.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
ldepartamento = ls_TONO-departamentos.
lpantalla = ls_tono-pantallas.
* Top level nodes:
IF ldepartamento <> ldepartamento_anterior.
ldepartamento_anterior = ldepartamento.
*Providing no key means that the node is added on top level:
PERFORM add_depto USING ldepartamento
''
CHANGING l_departamento_key.
* The departament changed, thus, there is no predecessor carrier
ENDIF.
* pantalla nodes:
* (always inserted as child of the last depto
* which is identified by 'l_departamento_key')
IF lpantalla <> lpantalla_anterior. "on change of l_carrid
lpantalla_anterior = lpantalla.
PERFORM add_pantalla_line USING ls_tono
l_departamento_key
CHANGING l_pantalla_key.
ENDIF.
*add the related information of each row into each nodo.
PERFORM add_complete_line USING ls_tono
l_pantalla_key
CHANGING l_last_key.
ENDLOOP.
ENDFORM. " create_hierarchy
**&---------------------------------------------------------------------
**& Form add_depto
**&---------------------------------------------------------------------
FORM add_depto USING depto TYPE c
p_relat_key TYPE lvc_nkey
CHANGING p_node_key TYPE lvc_nkey.
DATA: l_node_text TYPE lvc_value,
ls_tono TYPE ZRF_T0000,
l_month(15) TYPE c. "output string for depto
* get depto name for node text
l_node_text = depto.
* add node:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = p_relat_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_outtab_line = ls_tono
IMPORTING
e_new_node_key = p_node_key.
ENDFORM. " add_month
**--------------------------------------------------------------------
FORM add_pantalla_line USING ps_TONO TYPE ZRF_T0000
p_relat_key TYPE lvc_nkey
CHANGING p_node_key TYPE lvc_nkey.
DATA: l_node_text TYPE lvc_value,
ls_TONO TYPE ZRF_T0000.
* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
l_node_text = ps_TONO-pantallas.
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = p_relat_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = l_node_text
is_outtab_line = ls_TONO
IMPORTING
e_new_node_key = p_node_key.
ENDFORM. " add_carrid_line
*&---------------------------------------------------------------------
*
*& Form add_complete_line
*&---------------------------------------------------------------------
*
FORM add_complete_line USING ps_tono TYPE ZRF_T0000
p_relat_key TYPE lvc_nkey
CHANGING p_node_key TYPE lvc_nkey.
DATA: l_node_text TYPE lvc_value.
write PS_TONO-pantallas to l_node_text.
* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
CALL METHOD g_alv_tree->add_node
EXPORTING
i_relat_node_key = p_relat_key
i_relationship = cl_gui_column_tree=>relat_last_child
is_outtab_line = ps_tono
i_node_text = l_node_text
IMPORTING
e_new_node_key = p_node_key.
ENDFORM. " add_complete_line
*&---------------------------------------------------------------------*
*& Form register_events
*&---------------------------------------------------------------------*
* text
form register_events.
DATA: lt_events TYPE cntl_simple_events,
l_event TYPE cntl_simple_event,
l_event_receiver TYPE REF TO lcl_tree_event_receiver.
call method g_alv_tree->get_registered_events
importing events = lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_node_double_click.
APPEND l_event TO lt_events.
CALL METHOD g_alv_tree->set_registered_events
EXPORTING
events = lt_events
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.
IF sy-subrc <> 0.
MESSAGE x208(00) WITH 'ERROR'. "#EC NOTEXT
ENDIF.
CREATE OBJECT l_event_receiver.
SET HANDLER l_event_receiver->handle_node_double_click FOR g_alv_tree.
endform. " register_events
Comment Form under post in blogger/blogspot