0% found this document useful (0 votes)
6K views30 pages

Steps To Create Tree Control

This document provides steps to create an ALV tree control in ABAP. It begins with an overview of creating the basic ALV tree using objects. It then discusses adding user defined buttons to the ALV tree toolbar and processing other user interactions like double clicks. The main steps are outlined as creating the main program code, data declarations and screen call, includes to store the ALV tree code, creating the screen with PBO and PAI modules, and defining an OK code variable. Once this framework is set up, the document goes into details of creating the custom control, container, building the report header, setting the initial ALV tree table, and creating the ALV tree hierarchy within the PBO module. Sample code is

Uploaded by

api-3823432
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
6K views30 pages

Steps To Create Tree Control

This document provides steps to create an ALV tree control in ABAP. It begins with an overview of creating the basic ALV tree using objects. It then discusses adding user defined buttons to the ALV tree toolbar and processing other user interactions like double clicks. The main steps are outlined as creating the main program code, data declarations and screen call, includes to store the ALV tree code, creating the screen with PBO and PAI modules, and defining an OK code variable. Once this framework is set up, the document goes into details of creating the custom control, container, building the report header, setting the initial ALV tree table, and creating the ALV tree hierarchy within the PBO module. Sample code is

Uploaded by

api-3823432
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 30

9171748.

doc Page 1 of 30

Steps to create Tree control

1. Creation of basic ALV tree (using objects)

2. Add user defined buttons to ALV tree toolbar

3. Process other user interaction (i.e. double click)

Main Steps to create ALV Tree (Using Objects)

The ALV tree report produces uses OBJECT METHOD functionality in-order to produce
a Tree structured ALV output. The creation of an ALV tree report first requires the
creation of a simple program to build the ALV Details such as the field catalog and to call
a screen which will be used to display the ALV Tree. The screen should be created with a
'custom control' where you wish the ALV tree report to appear.
For the following example it will have the name 'SCREEN_CONTAINER'.
Creation of Main Program code, Data declaration and screen call
Creation of 'INCLUDES' to store ALV tree code
Create Screen along with PBO and PAI modules for screen
Define OK CODE (SY-UCOMM) variable
Add screen control to PAI module (INCLUDE Z......I01)
Create pf-status

Once you have the main program code in place to call the screen which will display the
ALV tree, you now need to setup the actual ALV tree and populate it. As this is screen
Based (dialog) the display coding will be performed within the PBO screen module.
Therefore you need to add the following processes to the PBO (STATUS_0100) module
Of the screen.
Create Custom control
Create Container
Create Object in Container
Build report header
Set ALV tree table for first display
Create ALV Tree Hierarchy
ABAP code listing of PBO (...O01) and FORM (...F01) includes should now look like
this
9171748.doc Page 2 of 30

Example Output from ALV tree display

Creation of Main Program code, Data declaration and screen call

*&---------------------------------------------------------------------*
*& Report ZDEMO_ALVTREE *
*& *
*&---------------------------------------------------------------------*
*& *
*& Example of a simple ALV Grid Report *
*& ................................... *
*& *
*& The basic requirement for this demo is to display a number of *
9171748.doc Page 3 of 30

*& fields from the EKPO and EKKO table in a tree structure. *
*& *
*&---------------------------------------------------------------------*
* Amendment History *
*----------------------------------------------------------------------*
REPORT zdemo_alvgrid .

*Data Declaration
*----------------
TABLES: ekko.
TYPE-POOLS: slis. "ALV Declarations

TYPES: BEGIN OF t_ekko,


ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
statu TYPE ekpo-statu,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
peinh TYPE ekpo-peinh,
END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,


it_ekpo TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
it_emptytab TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_ekko TYPE t_ekko,
wa_ekpo TYPE t_ekko.

DATA: ok_code like sy-ucomm, "OK-Code


save_ok like sy-ucomm.

*ALV data declarations


DATA: fieldcatalog TYPE lvc_t_fcat WITH HEADER LINE.

DATA: gd_fieldcat TYPE lvc_t_fcat,


gd_tab_group TYPE slis_t_sp_group_alv,
gd_layout TYPE slis_layout_alv.

*ALVtree data declarations


CLASS cl_gui_column_tree DEFINITION LOAD.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: gd_tree TYPE REF TO cl_gui_alv_tree,


gd_hierarchy_header TYPE treev_hhdr,
gd_report_title TYPE slis_t_listheader,
gd_logo TYPE sdydo_value,
gd_variant TYPE disvariant.

*Create container for alv-tree


DATA: gd_tree_container_name(30) TYPE c,
gd_custom_container TYPE REF TO cl_gui_custom_container.
9171748.doc Page 4 of 30

************************************************************************
*Includes
*INCLUDE ZDEMO_ALVTREEO01. "Screen PBO Modules
*INCLUDE ZDEMO_ALVTREEI01. "Screen PAI Modules
*INCLUDE ZDEMO_ALVTREEF01. "ABAP Subroutines(FORMS)

************************************************************************
*Start-of-selection.
START-OF-SELECTION.

* ALVtree setup data


PERFORM data_retrieval.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM build_hierarchy_header CHANGING gd_hierarchy_header.
PERFORM build_report_title USING gd_report_title gd_logo.
PERFORM build_variant.

* Display ALVtree report


call screen 100.

*&---------------------------------------------------------------------*
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data into Internal tables
*----------------------------------------------------------------------*
FORM data_retrieval.
SELECT ebeln
UP TO 10 ROWS
FROM ekko
INTO corresponding fields of TABLE it_ekko.

loop at it_ekko into wa_ekko.


SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
FROM ekpo
appending TABLE it_ekpo
where ebeln eq wa_ekko-ebeln.
endloop.
ENDFORM. " DATA_RETRIEVAL

*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.
* Please not there are a number of differences between the structure of
* ALVtree fieldcatalogs and ALVgrid fieldcatalogs.
* For example the field seltext_m is replace by scrtext_m in ALVtree.

fieldcatalog-fieldname = 'EBELN'. "Field name in itab


9171748.doc Page 5 of 30

fieldcatalog-scrtext_m = 'Purchase Order'. "Column text


fieldcatalog-col_pos = 0. "Column position
fieldcatalog-outputlen = 15. "Column width
fieldcatalog-emphasize = 'X'. "Emphasize (X or SPACE)
fieldcatalog-key = 'X'. "Key Field? (X or SPACE)
* fieldcatalog-do_sum = 'X'. "Sum Column?
* fieldcatalog-no_zero = 'X'. "Don't display if zero
APPEND fieldcatalog TO gd_fieldcat.
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'EBELP'.
fieldcatalog-scrtext_m = 'PO Iten'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 1.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'STATU'.
fieldcatalog-scrtext_m = 'Status'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 2.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'AEDAT'.
fieldcatalog-scrtext_m = 'Item change date'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 3.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'MATNR'.
fieldcatalog-scrtext_m = 'Material Number'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 4.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'MENGE'.
fieldcatalog-scrtext_m = 'PO quantity'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 5.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'MEINS'.
fieldcatalog-scrtext_m = 'Order Unit'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 6.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'NETPR'.
fieldcatalog-scrtext_m = 'Net Price'.
9171748.doc Page 6 of 30

fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 7.
fieldcatalog-datatype = 'CURR'.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.

fieldcatalog-fieldname = 'PEINH'.
fieldcatalog-scrtext_m = 'Price Unit'.
fieldcatalog-outputlen = 15.
fieldcatalog-col_pos = 8.
APPEND fieldcatalog TO gd_fieldcat..
CLEAR fieldcatalog.
ENDFORM. " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*& Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
* Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-totals_text = 'Totals'(201).
* gd_layout-totals_only = 'X'.
* gd_layout-f2code = 'DISP'. "Sets fcode for when double
* "click(press f2)
* gd_layout-zebra = 'X'.
* gd_layout-group_change_edit = 'X'.
* gd_layout-header_text = 'helllllo'.
ENDFORM. " BUILD_LAYOUT

*&---------------------------------------------------------------------*
*& Form build_hierarchy_header
*&---------------------------------------------------------------------*
* build hierarchy-header-information
*----------------------------------------------------------------------*
* -->P_L_HIERARCHY_HEADER structure for hierarchy-header
*----------------------------------------------------------------------*
FORM build_hierarchy_header CHANGING
p_hierarchy_header TYPE treev_hhdr.

p_hierarchy_header-heading = 'Hierarchy Header'(013).


p_hierarchy_header-tooltip = 'This is the Hierarchy Header !'(014).
p_hierarchy_header-width = 30.
p_hierarchy_header-width_pix = ''.
ENDFORM. " build_hierarchy_header

*&---------------------------------------------------------------------*
*& Form BUILD_REPORT_TITLE
*&---------------------------------------------------------------------*
* Build table for ALVtree header
*----------------------------------------------------------------------*
9171748.doc Page 7 of 30

* <-> p1 Header details


* <-> p2 Logo value
*----------------------------------------------------------------------*
FORM build_report_title CHANGING
pt_report_title TYPE slis_t_listheader
pa_logo TYPE sdydo_value.

DATA: ls_line TYPE slis_listheader,


ld_date(10) TYPE c.

* List Heading Line(TYPE H)


CLEAR ls_line.
ls_line-typ = 'H'.
* ls_line-key "Not Used For This Type(H)
ls_line-info = 'PO ALVTree Display'.
APPEND ls_line TO pt_report_title.

* Status Line(TYPE S)
ld_date(2) = sy-datum+6(2).
ld_date+2(1) = '/'.
ld_date+3(2) = sy-datum+4(2).
ld_date+5(1) = '/'.
ld_date+6(4) = sy-datum(4).

ls_line-typ = 'S'.
ls_line-key = 'Date'.
ls_line-info = ld_date.
APPEND ls_line TO pt_report_title.

* Action Line(TYPE A)
CLEAR ls_line.
ls_line-typ = 'A'.
CONCATENATE 'Report: ' sy-repid INTO ls_line-info SEPARATED BY space.
APPEND ls_line TO pt_report_title.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form BUILD_VARIANT
*&---------------------------------------------------------------------*
* Build variant
*----------------------------------------------------------------------*
form build_variant.
* Set repid for storing variants
gd_variant-report = sy-repid.
endform. " BUILD_VARIANT

Creation of 'INCLUDES' to store ALV tree code

Three includes need to be created in-order to store the ABAP code required for the
ALVtree report.
9171748.doc Page 8 of 30

Typically these will be one for the PBO modules, one for PAI modules and one for the
subroutines(FORMs):

*Includes
include zdemo_alvtreeo01. "Screen PBO Modules
include zdemo_alvtreei01. "Screen PAI Modules
include zdemo_alvtreef01. "ABAP Subroutines (FORMS)

If you are using the code provide within the ALV tree section of this web site simply
create the includes by un-commenting the 'Includes' section within the code (see below)
and double clicking on the name
i.e. 'zdemo_alvtreeo01'. Obviously these can be renamed.

************************************************************************
*Includes
*include zdemo_alvtreeo01. "Screen PBO Modules
*include zdemo_alvtreei01. "Screen PAI Modules
*include zdemo_alvtreef01. "ABAP Subroutines(FORMS)

************************************************************************
*Start-of-selection.
start-of-selection.

Create Screen along with PBO and PAI modules for screen
The next step is to create screen 100, to do this double click on the '100' within the call
screen command (Call screen 100.). Enter short description and select 'Normal' as screen
type.

To create the PBO and PAI modules insert that code below into the screen's flow logic.
Much of this code should automatically have been inserted during screen creation but
with the module lines commented out.
Simple remove the comments and double click the module name(STATUS_0100 and
USER_COMMAND_0100) in-order to create them, this will display the
perform/module creation screen.
The MODULES are usually created within two includes one ending in 'O01' for PBO
modules and One ending in 'I01' for PAI modules (See code below).

Please note in order for these includes to be displayed on the creation screen they need to
have be created along with the following lines of code added to the main prog(see
previous step):
INCLUDE ZDEMO_ALVTREEO01. "Screen PBO Modules
INCLUDE ZDEMO_ALVTREEI01. "Screen PAI Modules
Otherwise use the 'New Include' entry and SAP will add the necessary line for you.
* Screen flow logic code
9171748.doc Page 9 of 30

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

*-------------------------------------------------------------------*
***INCLUDE Z......O01 .
*-------------------------------------------------------------------*
*&------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&------------------------------------------------------------------*
* PBO Module
*-------------------------------------------------------------------*
module status_0100 output.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.

endmodule. " STATUS_0100 OUTPUT

*-------------------------------------------------------------------*
***INCLUDE Z......I01 .
*-------------------------------------------------------------------*
*&------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&------------------------------------------------------------------*
* PAI Module
*-------------------------------------------------------------------*
module user_command_0100 input.

endmodule. " USER_COMMAND_0100 INPUT


9171748.doc Page 10 of 30

Module creation screen

Define OK CODE (SY-UCOMM) variable


In order to define the OK CODE you must fist declare a variable of type SY-UCOM and
then insert this variable into the OK code declaration within the element list (see screen
shot below). If you have used the code contained on the website the ok code should
already have been declared as OK_CODE.
i.e. OK_CODE like sy-ucomm.
Note: there is also a variable called SAVE_OK, it is good practice to store the returned ok
code into a work area as soon as you enter the PAI processing.
9171748.doc Page 11 of 30

Add screen control to PAI module (INCLUDE Z......I01)

The following code adds simple screen control to the report and whenever the user
presses the cancel, exit or back icon they will exit from the report. It also processes the
ALV tree user interactions within the 'others' case statement

*----------------------------------------------------------------------*
* INCLUDE Z......I01 *
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0100 input.
DATA return TYPE REF TO cl_gui_event.

save_ok = ok_code.
case ok_code.
when 'EXIT' or 'BACK' or 'CANC'.
* Exit program
leave to screen 0.
* Process ALVtree user actions
when others.
call method cl_gui_cfw=>get_current_event_object
9171748.doc Page 12 of 30

receiving
event_object = return.
call method cl_gui_cfw=>dispatch.

endcase.
endmodule. " USER_COMMAND_0100 INPUT

Create Pf-status (screen menu functionality)

In order to created the pf-status for the screen you need to un-comment '* SET PF-
STATUS ‘xxxxxxxx’ and give it a name.
i.e. SET PF-STATUS 'STATUS1'.
Step 1
Now double click on 'STATUS1' in-order to create the pf-status. Enter short text, select
status type as 'Online status' and click save.
Step2
You should now be presented with the status creation screen. Choose 'Adjust template'
from the Extras menu (4.6 onwards only).
Step 3
Now select 'List status' and click the green tick (see below).

Step 4
9171748.doc Page 13 of 30

All the basic menu bars/buttons should now have been entered. Now click save then
activate. The Pf-status has now been completed.

Create Custom control


Via screen painter insert 'custom control' on to screen and give it the name
'SCREEN_CONTROL'. This is where the ALV tree will appear so align appropriately.
9171748.doc Page 14 of 30

Create Container

* Add following code to 'STATUS_0100'(PBO module)


*-----------------------------------------------*
* Create container for alv-tree
PERFORM create_alvtree_container.

* Add following code to 'Z......F01' INCLUDE


*-------------------------------------------*
*&---------------------------------------------------------------------*
*& Form CREATE_ALVTREE_CONTAINER
*&---------------------------------------------------------------------*
* Create container for alv-tree
*----------------------------------------------------------------------*
FORM create_alvtree_container.
l_tree_container_name = 'TREE1'.

create object l_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.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
ENDFORM.

Create Object in Container

* Add following code to 'STATUS_0100'(PBO module)


*-----------------------------------------------*
* Create tree control
PERFORM create_object_in_container.

* Add following code to 'Z......F01' INCLUDE


*-------------------------------------------*
*&---------------------------------------------------------------------*
*& Form CREATE_OBJECT_IN_CONTAINER
*&---------------------------------------------------------------------*
* Create ALV tree control
*----------------------------------------------------------------------*
FORM create_object_in_container.
create object gd_tree
9171748.doc Page 15 of 30

exporting
parent = gd_custom_container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
no_html_header = ''
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'.
endif.
ENDFORM.

Build Report Header (Title)

----Add to START-OF-SELECTION Event


* Create Report Title
PERFORM build_report_title USING gd_report_title gd_logo.

----Add to main prog after START-OF-SELECTION Event


*&---------------------------------------------------------------------*
*& Form BUILD_REPORT_TITLE
*&---------------------------------------------------------------------*
* Build table for ALV tree header
*----------------------------------------------------------------------*
* <-> p1 Header details
* <-> p2 Logo value
*----------------------------------------------------------------------*
FORM build_report_title CHANGING
pt_report_title TYPE slis_t_listheader
pa_logo TYPE sdydo_value.

DATA: ls_line TYPE slis_listheader,


ld_date(10) TYPE c.

* List Heading Line(TYPE H)


CLEAR ls_line.
ls_line-typ = 'H'.
* ls_line-key "Not Used For This Type(H)
ls_line-info = 'PO ALVTree Display'.
APPEND ls_line TO pt_report_title.

* Status Line(TYPE S)
ld_date(2) = sy-datum+6(2).
ld_date+2(1) = '/'.
ld_date+3(2) = sy-datum+4(2).
ld_date+5(1) = '/'.
9171748.doc Page 16 of 30

ld_date+6(4) = sy-datum(4).

ls_line-typ = 'S'.
ls_line-key = 'Date'.
ls_line-info = ld_date.
APPEND ls_line TO pt_report_title.

* Action Line(TYPE A)
CLEAR ls_line.
ls_line-typ = 'A'.
CONCATENATE 'Report: ' sy-repid INTO ls_line-info SEPARATED BY space.
APPEND ls_line TO pt_report_title.

ENDFORM. "BUILD_REPORT_TITLE

Set ALV tree table for first display

* Add following code to 'STATUS_0100'(PBO module)


*------------------------------------------------*
* Create empty ALV tree control ready for first display
PERFORM create_empty_alvtree_control.

* Add following code to 'Z......F01' INCLUDE


*-------------------------------------------*
*&---------------------------------------------------------------------*
*& Form CREATE_EMPTY_ALVTREE_CONTROL
*&---------------------------------------------------------------------*
* Create empty tree control
*----------------------------------------------------------------------*
FORM create_empty_alvtree_control.
* Create emty tree-control
CLEAR: it_emptytab.
REFRESH: it_emptytab.
CALL METHOD gd_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = gd_hierarchy_header
it_list_commentary = gd_report_title
i_logo = gd_logo
* i_background_id = 'ALV_BACKGROUND'
i_save = 'A'
* is_variant = ls_variant
CHANGING
it_outtab = it_emptytab "Must be empty
it_fieldcatalog = gd_fieldcat.

ENDFORM. " CREATE_EMPTY_ALVTREE_CONTROL


9171748.doc Page 17 of 30

Create ALV tree Hierarchy

* Add following code to 'STATUS_0100'(PBO module)


*-----------------------------------------------*
* Create ALV tree Hierarchy
PERFORM create_alvtree_hierarchy.

* Add following code to 'Z......F01' INCLUDE


*--------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form CREATE_ALVTREE_HIERARCHY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* Builds ALV tree display, (inserts nodes, subnodes etc)
*----------------------------------------------------------------------*
form create_alvtree_hierarchy.
data: ls_sflight type sflight,
lt_sflight type sflight occurs 0.
data: ld_ebeln_key type lvc_nkey,
ld_ebelp_key type lvc_nkey.

loop at it_ekko into wa_ekko.


perform add_ekko_node using wa_ekko
''
changing ld_ebeln_key.

loop at it_ekpo into wa_ekpo where ebeln eq wa_ekko-ebeln.


perform add_ekpo_line using wa_ekpo
ld_ebeln_key
changing ld_ebelp_key.
endloop.
endloop.

* calculate totals
call method gd_tree->update_calculations.

* this method must be called to send the data to the frontend


call method gd_tree->frontend_update.
endform. " CREATE_ALVTREE_HIERARCHY

ABAP code listing of PBO (...O01) and FORM (...F01) includes should now look like
this

The following code allows the easy creation of the example ALVtree report. Simply copy
and past it into the appropriate includes (PBO and FORM).

*----------------------------------------------------------------------*
9171748.doc Page 18 of 30

***INCLUDE ZDEMO_ALVTREEO01.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
* SET TITLEBAR 'xxx'.

* If ALV tree already exists then it mush not be re-created as this


* will cause a runtime error.
IF gd_tree IS INITIAL.

* Create ALV tree (must be performed within screen PBO module)


PERFORM create_alvtree_container.
PERFORM create_object_in_container.
PERFORM build_report_title USING gd_report_title gd_logo.
PERFORM create_empty_alvtree_control.
PERFORM create_alvtree_hierarchy.
ENDIF.
CALL METHOD cl_gui_cfw=>flush.

ENDMODULE. " STATUS_0100 OUTPUT

*----------------------------------------------------------------------*
* INCLUDE ZDEMO_ALVTREEF01 *
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*& Form CREATE_ALVTREE_CONTAINER
*&---------------------------------------------------------------------*
* Create container for ALV tree report
*----------------------------------------------------------------------*

form create_alvtree_container.
* Create container for alv-tree
gd_tree_container_name = 'SCREEN_CONTAINER'.

create object gd_custom_container


exporting
container_name = gd_tree_container_name
exceptions
cntl_error =1
cntl_system_error =2
create_error =3
lifetime_error =4
lifetime_dynpro_dynpro_link = 5.
if sy-subrc <> 0.
9171748.doc Page 19 of 30

message x208(00) with 'ERROR'.


endif.
endform. " CREATE_ALVTREE_CONTAINER

*&---------------------------------------------------------------------*
*& Form CREATE_OBJECT_IN_CONTAINER
*&---------------------------------------------------------------------*
* Create ALV tree object in container
*----------------------------------------------------------------------*
form create_object_in_container.

* Create tree control


create object gd_tree
exporting
parent = gd_custom_container
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
item_selection = 'X'
no_html_header = ''
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'.
endif.
endform. " CREATE_OBJECT_IN_CONTAINER

*&---------------------------------------------------------------------*
*& Form BUILD_REPORT_TITLE
*&---------------------------------------------------------------------*
* Build table for ALVtree header
*----------------------------------------------------------------------*
* <-> p1 Header details
* <-> p2 Logo value
*----------------------------------------------------------------------*
FORM build_report_title CHANGING
pt_report_title TYPE slis_t_listheader
pa_logo TYPE sdydo_value.

DATA: ls_line TYPE slis_listheader,


ld_date(10) TYPE c.

* List Heading Line(TYPE H)


CLEAR ls_line.
ls_line-typ = 'H'.
* ls_line-key "Not Used For This Type(H)
ls_line-info = 'PO ALVTree Display'.
9171748.doc Page 20 of 30

APPEND ls_line TO pt_report_title.

* Status Line(TYPE S)
ld_date(2) = sy-datum+6(2).
ld_date+2(1) = '/'.
ld_date+3(2) = sy-datum+4(2).
ld_date+5(1) = '/'.
ld_date+6(4) = sy-datum(4).

ls_line-typ = 'S'.
ls_line-key = 'Date'.
ls_line-info = ld_date.
APPEND ls_line TO pt_report_title.

* Action Line(TYPE A)
CLEAR ls_line.
ls_line-typ = 'A'.
CONCATENATE 'Report: ' sy-repid INTO ls_line-info SEPARATED BY space.
APPEND ls_line TO pt_report_title.
ENDFORM. " BUILD_REPORT_TITLE

*&---------------------------------------------------------------------*
*& Form CREATE_EMPTY_ALVTREE_CONTROL
*&---------------------------------------------------------------------*
* Create empty tree control
*----------------------------------------------------------------------*
FORM create_empty_alvtree_control.
* Create emty tree-control
CLEAR: it_emptytab.
REFRESH: it_emptytab.
CALL METHOD gd_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = gd_hierarchy_header
it_list_commentary = gd_report_title
i_logo = gd_logo
* i_background_id = 'ALV_BACKGROUND'
i_save = 'A'
is_variant = gd_variant
CHANGING
it_outtab = it_emptytab "Must be empty
it_fieldcatalog = gd_fieldcat.
ENDFORM. " CREATE_EMPTY_ALVTREE_CONTROL

*&---------------------------------------------------------------------*
*& Form CREATE_ALVTREE_HIERARCHY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* Builds ALV tree display, (inserts nodes, subnodes etc)
*----------------------------------------------------------------------*
form create_alvtree_hierarchy.
data: ls_sflight type sflight,
9171748.doc Page 21 of 30

lt_sflight type sflight occurs 0.


data: ld_ebeln_key type lvc_nkey,
ld_ebelp_key type lvc_nkey.

loop at it_ekko into wa_ekko.


perform add_ekko_node using wa_ekko
''
changing ld_ebeln_key.

loop at it_ekpo into wa_ekpo where ebeln eq wa_ekko-ebeln.


perform add_ekpo_line using wa_ekpo
ld_ebeln_key
changing ld_ebelp_key.
endloop.
endloop.

* calculate totals
call method gd_tree->update_calculations.

* this method must be called to send the data to the frontend


call method gd_tree->frontend_update.
endform. " CREATE_ALVTREE_HIERARCHY

*&---------------------------------------------------------------------*
*& Form ADD_EKKO_NODE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_EKPO text
* -->P_0553 text
* <--P_EBELN_KEY text
*----------------------------------------------------------------------*
form add_ekko_node using ps_ekko like wa_ekko
value(p_relate_key)
changing p_node_key.

data: ld_node_text type lvc_value,


ls_sflight type sflight.

* Set item-layout
data: lt_item_layout type lvc_t_layi,
ls_item_layout type lvc_s_layi.
ls_item_layout-t_image = '@3P@'.
ls_item_layout-fieldname = gd_tree->c_hierarchy_column_name.
ls_item_layout-style = cl_gui_column_tree=>style_default.
ld_node_text = ps_ekko-ebeln.
append ls_item_layout to lt_item_layout.

* Add node
call method gd_tree->add_node
exporting
i_relat_node_key = p_relate_key
9171748.doc Page 22 of 30

i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = ld_node_text
is_outtab_line = ps_ekko
it_item_layout = lt_item_layout
importing
e_new_node_key = p_node_key.
endform. " ADD_EKKO_NODE

*&---------------------------------------------------------------------*
*& Form ADD_EKPO_LINE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_EKPO text
* -->P_LD_EBELN_KEY text
* <--P_LD_EBELP_KEY text
*----------------------------------------------------------------------*
form add_ekpo_line using ps_ekpo like wa_ekpo
value(p_relate_key)
changing p_node_key.

data: ld_node_text type lvc_value,


ls_sflight type sflight.

* Set item-layout
data: lt_item_layout type lvc_t_layi,
ls_item_layout type lvc_s_layi.
ls_item_layout-t_image = '@3P@'.
ls_item_layout-fieldname = gd_tree->c_hierarchy_column_name.
ls_item_layout-style = cl_gui_column_tree=>style_default.
ld_node_text = ps_ekpo-ebelp.
append ls_item_layout to lt_item_layout.

* Add node
call method gd_tree->add_node
exporting
i_relat_node_key = p_relate_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = ld_node_text
is_outtab_line = ps_ekpo
it_item_layout = lt_item_layout
importing
e_new_node_key = p_node_key.
endform. " ADD_EKPO_LINE

2. Add user defined buttons to ALV tree toolbar

Implement user defined buttons to ALV tree toolbar


9171748.doc Page 23 of 30

In order to add additional buttons onto the ALV tree report the following sections of code
need implementing:

Creation of toolbar event receiver class

Add statement 'include <icon>.' to data declaration section

Code to insert button(s) onto toolbar

Creation of toolbar event receiver class

Define class to handle user defined ALV tree toolbar buttons. Insert at end of DATA
declaration section but before any ABAP processing.

*----------------------------------------------------------------------*
* INCLUDE ZTEST_TOOLBAR_EVENT_RECEIVER *
*----------------------------------------------------------------------*
data mr_toolbar type ref to cl_gui_toolbar. "Add to top include

CLASS lcl_toolbar_event_receiver DEFINITION.

PUBLIC SECTION.
METHODS: on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,

on_toolbar_dropdown
FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode
posx
posy.
9171748.doc Page 24 of 30

ENDCLASS.

*---------------------------------------------------------------------*
* CLASS lcl_toolbar_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS lcl_toolbar_event_receiver IMPLEMENTATION.

METHOD on_function_selected.
DATA: ls_sflight TYPE sflight.
DATA: lt_list_commentary TYPE slis_t_listheader,
l_logo TYPE sdydo_value.

* Processing for user defined tollbar button goes here


CASE fcode.
WHEN 'NEXT'.
WHEN 'DELETE'.
DATA: lt_selected_node TYPE lvc_t_nkey.
DATA: e_selected_node TYPE lvc_nkey,
e_fieldname TYPE lvc_fname.
DATA: lt_outtab_line(100) TYPE c,
lt_node_text TYPE lvc_value.

CALL METHOD gd_tree->get_selected_nodes


CHANGING
ct_selected_nodes = lt_selected_node.

WHEN 'INSERT_LC'.
* Code for function code INSERT_LC goes here
WHEN 'INSERT_FC'.
* Code for function code INSERT_FC goes here
WHEN 'INSERT_FS'.
* Code for function code INSERT_FS goes here
WHEN 'INSERT_LS'.
* Code for function code INSERT_LS goes here
WHEN 'INSERT_NS'.
* Code for function code INSERT_NS goes here
ENDCASE.

* update frontend
CALL METHOD gd_tree->frontend_update.
ENDMETHOD.

METHOD on_toolbar_dropdown.
* create contextmenu
DATA: l_menu TYPE REF TO cl_ctmenu,
l_fc_handled TYPE as4flag.

CREATE OBJECT l_menu.


CLEAR l_fc_handled.

* Setup Insert button so options are displayed as drop down menu


9171748.doc Page 25 of 30

CASE fcode.
WHEN 'INSERT_LC'.
l_fc_handled = 'X'.
* insert as last child
CALL METHOD l_menu->add_function
EXPORTING fcode = 'INSERT_LC'
text = 'Insert New Line as Last Child'. "#EC NOTEXT
* insert as first child
CALL METHOD l_menu->add_function
EXPORTING fcode = 'INSERT_FC'
text = 'Insert New Line as First Child'. "#EC NOTEXT
* insert as next sibling
CALL METHOD l_menu->add_function
EXPORTING fcode = 'INSERT_NS'
text = 'Insert New Line as Next Sibling'."#EC NOTEXT
* insert as last sibling
CALL METHOD l_menu->add_function
EXPORTING fcode = 'INSERT_LS'
text = 'Insert New Line as Last Sibling'."#EC NOTEXT
* insert as first sibling
CALL METHOD l_menu->add_function
EXPORTING fcode = 'INSERT_FS'
text = 'Insert New Line as First Sibling'. "#EC NOTEXT
ENDCASE.

* show dropdownbox
IF l_fc_handled = 'X'.
CALL METHOD mr_toolbar->track_context_menu
EXPORTING
context_menu = l_menu
posx = posx
posy = posy.
ENDIF.

ENDMETHOD.
ENDCLASS.

Code to insert button(s) onto toolbar


Insert the following code into the PBO of the screen after the ALV tree has been created.
I.e. after 'CALL METHOD gd_tree->set_table_for_first_display' has been executed.

*&---------------------------------------------------------------------*
*& CHANGE_TOOLBAR
*&---------------------------------------------------------------------*

* get toolbar control


call method gd_tree->get_toolbar_object
importing
er_toolbar = mr_toolbar.

check not mr_toolbar is initial.

* add separator to toolbar


9171748.doc Page 26 of 30

call method mr_toolbar->add_button


exporting
fcode = ''
icon = ''
butn_type = cntb_btype_sep
text = ''
quickinfo = 'This is a Separator'. "#EC NOTEXT

* add Standard Button to toolbar (for Delete Subtree)


call method mr_toolbar->add_button
exporting
fcode = 'DELETE' "Function code of button
icon = '@18@' "Icon ID (see )
butn_type = cntb_btype_button "Button type
text = '' "Button text
quickinfo = 'Delete subtree'. "Quick info text

* add Dropdown Button to toolbar (for Insert Line)


call method mr_toolbar->add_button
exporting
fcode = 'INSERT_LC' "Function code of button
icon = '@17@' "Icon ID (see )
butn_type = cntb_btype_dropdown "Button type
text = '' "Button text
quickinfo = 'Insert Line'. "Quick info text

* set event-handler for toolbar-control


data: toolbar_event_receiver type ref to lcl_toolbar_event_receiver.

create object toolbar_event_receiver.


set handler toolbar_event_receiver->on_function_selected
for mr_toolbar.
set handler toolbar_event_receiver->on_toolbar_dropdown
for mr_toolbar.

3. Process other user interaction (i.e. double click)

Implement ALV tree user interaction processing

In order to add additional buttons onto the ALV tree report the following sections of code
need Implementing:

Creation of tree event receiver class

Code to activate User interaction events (i.e. double click)


9171748.doc Page 27 of 30

Creation of tree event receiver class

Define class to handle user interaction other than via toolbar buttons. Insert at end of
DATA declaration section but before any ABAP processing

*----------------------------------------------------------------------*
* INCLUDE BCALV_TREE_EVENT_RECEIVER *
*----------------------------------------------------------------------*
CLASS lcl_tree_event_receiver DEFINITION.

PUBLIC SECTION.

METHODS handle_node_ctmenu_request
FOR EVENT node_context_menu_request OF cl_gui_alv_tree
IMPORTING node_key
menu.
METHODS handle_node_ctmenu_selected
FOR EVENT node_context_menu_selected OF cl_gui_alv_tree
IMPORTING node_key
fcode.
METHODS handle_item_ctmenu_request
FOR EVENT item_context_menu_request OF cl_gui_alv_tree
IMPORTING node_key
fieldname
menu.
METHODS handle_item_ctmenu_selected
FOR EVENT item_context_menu_selected OF cl_gui_alv_tree
IMPORTING node_key
fieldname
fcode.

METHODS handle_item_double_click
FOR EVENT item_double_click OF cl_gui_alv_tree
IMPORTING node_key
fieldname.

METHODS handle_button_click
FOR EVENT button_click OF cl_gui_alv_tree
IMPORTING node_key
fieldname.
9171748.doc Page 28 of 30

METHODS handle_link_click
FOR EVENT link_click OF cl_gui_alv_tree
IMPORTING node_key
fieldname.

METHODS handle_header_click
FOR EVENT header_click OF cl_gui_alv_tree
IMPORTING fieldname.

ENDCLASS.

*---------------------------------------------------------------------*
* CLASS lcl_tree_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS lcl_tree_event_receiver IMPLEMENTATION.

METHOD handle_node_ctmenu_request.
* append own functions
CALL METHOD menu->add_function
EXPORTING fcode = 'USER1'
text = 'Usercmd 1'. "#EC NOTEXT
CALL METHOD menu->add_function
EXPORTING fcode = 'USER2'
text = 'Usercmd 2'. "#EC NOTEXT
CALL METHOD menu->add_function
EXPORTING fcode = 'USER3'
text = 'Usercmd 3'. "#EC NOTEXT
ENDMETHOD.

METHOD handle_node_ctmenu_selected.
CASE fcode.
WHEN 'USER1' OR 'USER2' OR 'USER3'.
MESSAGE i000(0h) WITH 'Node-Context-Menu on Node ' node_key
'fcode : ' fcode. "#EC NOTEXT
ENDCASE.
ENDMETHOD.

METHOD handle_item_ctmenu_request .
* append own functions
CALL METHOD menu->add_function
EXPORTING fcode = 'USER1'
text = 'Usercmd 1'.
CALL METHOD menu->add_function
EXPORTING fcode = 'USER2'
text = 'Usercmd 2'.
CALL METHOD menu->add_function
EXPORTING fcode = 'USER3'
text = 'Usercmd 3'.
ENDMETHOD.

METHOD handle_item_ctmenu_selected.
CASE fcode.
9171748.doc Page 29 of 30

WHEN 'USER1' OR 'USER2' OR 'USER3'.


MESSAGE i000(0h) WITH 'Item-Context-Menu on Node ' node_key
'Fieldname : ' fieldname. "#EC NOTEXT
ENDCASE.
ENDMETHOD.

METHOD handle_item_double_click.
* Processing for when user double clicks on ALVtree
ENDMETHOD.

METHOD handle_button_click.
* Processing when user clicks button
ENDMETHOD.

METHOD handle_link_click.
* ??
ENDMETHOD.

METHOD handle_header_click.
* Processing for when user clicks on ALVtree column headers
ENDMETHOD.

ENDCLASS.

Code to activate User interaction events (i.e. double click)

Insert the following code into the PBO of the screen after the ALV tree has been created.
I.e. after 'CALL METHOD gd_tree->set_table_for_first_display' has been executed.

*&---------------------------------------------------------------------*
*& REGISTER_EVENTS
*&---------------------------------------------------------------------*

* define the events which will be passed to the backend


data: lt_events type cntl_simple_events,
l_event type cntl_simple_event.

* define the events which will be passed to the backend


l_event-eventid = cl_gui_column_tree=>eventid_expand_no_children.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_checkbox_change.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_header_context_men_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_node_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_double_click.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_header_click.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_keypress.
9171748.doc Page 30 of 30

append l_event to lt_events.

call method gd_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.

* set Handler
data: l_event_receiver type ref to lcl_tree_event_receiver.
create object l_event_receiver.
set handler l_event_receiver->handle_node_ctmenu_request
for gd_tree.
set handler l_event_receiver->handle_node_ctmenu_selected
for gd_tree.
set handler l_event_receiver->handle_item_ctmenu_request
for gd_tree.
set handler l_event_receiver->handle_item_ctmenu_selected
for gd_tree.
set handler l_event_receiver->handle_item_double_click
for gd_tree.
set handler l_event_receiver->handle_header_click
for gd_tree.

You might also like