ALV Tree Controller!

This example will show you how to build a simple tree using class CL_GUI_ALV_TREE.

Tree will show flight detail as below.

I use two global classes, one for application logic (get data for the report) and other one to create ALV tree. Icon field create is a hot spot.

Application logic class:  ZCL_FLIGHT
View controller: ZCL_FLIGHT_TREE_VIEW

REPORT  zr_flight_detail_tree.

DATA: go_data TYPE REF TO zcl_flight,
go_view TYPE REF TO zcl_flight_tree_view.

START-OF-SELECTION.

FREE go_data.

CREATE OBJECT go_data.

END-OF-SELECTION.

FREE go_view.

CALL SCREEN 100.

MODULE pbo OUTPUT.
CREATE OBJECT go_view
EXPORTING
iv_dynnr = sy-dynnr
iv_repid = sy-repid
iv_data  = go_data.

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.
ENDMODULE.                 ” PBO  OUTPUT

MODULE status_0100 OUTPUT.
SET PF-STATUS ‘100_STATUS’.
SET TITLEBAR ‘100_TITLE’.
ENDMODULE.                 ” STATUS_0100  OUTPUT

MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN ‘BACK’.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.                 ” USER_COMMAND_0100  INPUT

Coding of application logic class:  ZCL_FLIGHT

Attributes

Methods

METHOD constructor.
CALL METHOD me->get_data.
ENDMETHOD.

method GET_DATA.
FIELD-SYMBOLS: <fs_flights> like LINE OF gi_flights.

SELECT *
FROM sflight
into TABLE gi_flights.

LOOP AT gi_flights ASSIGNING <fs_flights>.
<fs_flights>-create = ‘@59@’.
ENDLOOP.
endmethod.

Coding of view class: ZCL_FLIGHT_TREE_VIEW

Attributes

Methods

METHOD CONSTRUCTOR.
DATA: li_flights LIKE gr_data->gi_flights.

gr_data = iv_data.                    “Reference from object with application logic
gv_dynnr = iv_dynnr.                  “Screen number
gv_repid = iv_repid.                  “Report Name

*create container
CREATE OBJECT gr_custom_container
EXPORTING
container_name              = gc_container_name
repid                       = gv_repid
dynnr                       = gv_dynnr
EXCEPTIONS
cntl_error                  = 1
cntl_system_error           = 2
create_error                = 3
lifetime_error              = 4
lifetime_dynpro_dynpro_link = 5.

* create tree control
CREATE OBJECT gr_tree
EXPORTING
parent              = gr_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.

CALL METHOD me->create_field_cat.         “Field catalog of the tree
CALL METHOD me->create_header.            “Create tree header

CALL METHOD gr_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = gwa_header
CHANGING
it_fieldcatalog     = gt_fieldcat
it_outtab           = gi_flights.     “This table should be empty, it will be filled when adding nodes

CALL METHOD me->register_events.          “register events of the tree
CALL METHOD me->create_nodes.             “Create hierarchy
CALL METHOD gr_tree->frontend_update.
ENDMETHOD.

METHOD CREATE_NODES.
DATA: lwa_flights LIKE LINE OF gr_data->gi_flights.
DATA: lwa_flights_tmp LIKE LINE OF gr_data->gi_flights.

DATA: lv_carrid_key TYPE lvc_nkey,
lv_connid_key TYPE lvc_nkey,
lv_node_text TYPE lvc_value.

*sort the data table according to the node hierarchy
SORT gr_data->gi_flights BY carrid connid fldate.

LOOP AT gr_data->gi_flights INTO lwa_flights.

*folder for every air line code
AT NEW carrid.
lv_node_text = lwa_flights-carrid.
CALL METHOD gr_tree->add_node
EXPORTING
i_relat_node_key = ”                                   “no parent node
i_relationship   = cl_gui_column_tree=>relat_last_child
i_node_text      = lv_node_text
is_outtab_line   = lwa_flights_tmp
IMPORTING
e_new_node_key   = lv_carrid_key.
ENDAT.

*leaves of the tree which is connection no
lv_node_text = lwa_flights-connid.

CALL METHOD gr_tree->add_node
EXPORTING
i_relat_node_key = lv_carrid_key                        “parent node is the airline code
i_relationship   = cl_gui_column_tree=>relat_last_child
i_node_text      = lv_node_text
is_outtab_line   = lwa_flights
IMPORTING
e_new_node_key   = lv_connid_key.

ENDLOOP.
ENDMETHOD.

METHOD CREATE_FIELD_CAT.

FIELD-SYMBOLS: <fs_fieldcat> LIKE LINE OF gt_fieldcat.
*get the field catalog
CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’
EXPORTING
i_structure_name = ‘ZSFLIGHT_ISURU’
CHANGING
ct_fieldcat      = gt_fieldcat.

LOOP AT gt_fieldcat ASSIGNING <fs_fieldcat>.
CASE <fs_fieldcat>-fieldname.
WHEN ‘CARRID’.
<fs_fieldcat>-no_out = ‘X’.           “Node field should be hidden
WHEN ‘CONNID’.
<fs_fieldcat>-no_out = ‘X’.
WHEN ‘CREATE’.
<fs_fieldcat>-icon = ‘X’.             “Icon field and hot spot
<fs_fieldcat>-hotspot = ‘X’.
ENDCASE.
ENDLOOP.
ENDMETHOD.

method REGISTER_EVENTS.

DATA: li_events TYPE cntl_simple_events,
lwa_events like LINE OF li_events.

*get the standard registerd events
call method gr_tree->get_registered_events
importing events = li_events.

*append the hot-spot click (link click) event
lwa_events-eventid = cl_gui_column_tree=>eventid_link_click.
APPEND lwa_events TO li_events.

*Set the events
CALL METHOD gr_tree->set_registered_events
EXPORTING
events = li_events
EXCEPTIONS
cntl_error                = 1
cntl_system_error         = 2
illegal_event_combination = 3.

*set the handler for the event link click
set HANDLER me->link_click FOR gr_tree.
endmethod.

method CREATE_HEADER.
gwa_header-heading = ‘Airline Code/Connection Number'(300).      “Header of the nodes: Folder structure
gwa_header-width = 40.                              “Width of the header
gwa_header-width_pix = ”.
endmethod.

method LINK_CLICK.
BREAK-POINT.
endmethod.

You can write your own logic at hot spot click.

Leave a Reply

Your email address will not be published. Required fields are marked *