‎2008 Aug 29 8:31 AM
Hi,
Can you please tell me, how to maintain a tree structure in ALV?
Thanks..
‎2008 Aug 29 8:42 AM
Refer to this program...run it and try to understand it
*&----
*
*& Report Z9_KHUSH_ALVTREE
*&----
*
*& Date Name
*& 15-05-07 Khushboo Gupta
*&----
*
*& This Report Prints the Purchase Orders and its Items in the ALV TREE
*& format with the various option like Select All, Deselect All,
*& Insert First Child, Next Child, Last Child,
*& Insert First Sibling Next Sibling, Last Sibling,
*& Delete Node, Context Menu, Parent-Child Click for Checkboxes,
*& Toolbar Scrolldown
*&----
*
report z9_khush_alvtree.
type-pools : slis.
*----
*
*DATA DECLARATION
*----
*
*--> Internal Table
data : it_ekpo type standard table of z9gv_ekpo with header line,
gts_ekpo type standard table of z9gv_ekpo with header line,
gt_ekpo type standard table of z9gv_ekpo with header line,
it_fieldcatalog type lvc_t_fcat,
lt_nodes type lvc_t_nkey,
lt_items type lvc_t_layi,
l_t_key type lvc_t_nkey,
l_hierarchy_header type treev_hhdr.
*--> Work Area Declaration
data : wa_ekpo type z9gv_ekpo,
wa_fieldcatalog type lvc_s_fcat,
wa_list_commentary type slis_t_listheader,
ls_line type slis_listheader,
la_ekpo type z9gv_ekpo,
ls_item_u type lvc_s_laci,
ls_item type lvc_s_layi,
ls_item_layout type lvc_s_layi.
Refrence Variable for the ALV TREE -
*--> Tree,Container and Toolbar.
data : tree type ref to cl_gui_alv_tree,
cc type ref to cl_gui_custom_container,
mr_toolbar type ref to cl_gui_toolbar.
*--> Variables Declaration.
data : ok_code type sy-ucomm,
save_ok type sy-ucomm,
ld_nkey type lvc_nkey,
ld_ntext type lvc_value,
l_tree_container_name(30) type c,
l_logo type sdydo_value,
l_ebeln_key type lvc_nkey,
l_ebelp_key type lvc_nkey,
l_top_key type lvc_nkey,
node_text type lvc_value,
ls_node_text type lvc_value,
lt_events type cntl_simple_events,
l_event type cntl_simple_event.
For Classes lcl_tree_event_receiver and lcl_toolbar_event_receiver
data : l_rc type c,
lt_children type lvc_t_nkey,
ls_children type lvc_s_nkey,
ls_lvc_s_laci type lvc_s_laci,
lt_lvc_t_laci type lvc_t_laci,
ls_node_layout type lvc_s_lacn,
lt_item_ly type lvc_t_layi,
wa_item_ly type lvc_s_layi,
ls_ekpo type z9gv_ekpo,
lt_item_layout type lvc_t_layi,
lt_selected_node type lvc_t_nkey,
l_menu type ref to cl_ctmenu,
l_fc_handled type as4flag,
l_selected_node type lvc_nkey.
*&----
*
*& Selection Screen
*&----
*
selection-screen : begin of block blk1.
select-options : s_ebeln for wa_ekpo-ebeln.
selection-screen : end of block blk1.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_tree_event_receiver definition.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public section.
Define an event handler method for each event you want to
react to the events
*--> For Node Double Click
methods handle_node_double_click
for event node_double_click of cl_gui_alv_tree
importing node_key sender.
*--> For Parent-Child Checkbox Check
methods handle_checkbox_changed
for event checkbox_change of cl_gui_alv_tree
importing node_key sender.
*--> For Node Context Menu Request
methods: handle_node_cm_req
for event node_context_menu_request of cl_gui_alv_tree
importing node_key menu.
*--> For Node Context Menu Selected
methods: handle_node_cm_sel
for event node_context_menu_selected of cl_gui_alv_tree
importing node_key fcode sender.
endclass. "lcl_tree_event_receiver DEFINITION
**********************************************************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_tree_event_receiver implementation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implement your event handler methods.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_double_click.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 not lt_children is initial.
Expand the nodes which has child nodes or leaf
call method sender->expand_node
exporting
i_node_key = node_key
i_level_count = 2.
endif.
refresh lt_children.
endmethod. "handle_node_double_click
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_cm_req.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Event parameter 'menu' holds a reference to the standard context
menu of ALV Tree (functions 'Expand'/'Collapse' on nodes).
You may either append your own functions (separated by a line)
or clear the menu if you do not want to allow standard functions.
In this case the standard menu is cleared.
call method menu->clear.
The next line defines one line of the context menu.
call method menu->add_function
exporting
fcode = 'DEL_SUBTREE'
text = text-011. "Delete Subtree
endmethod. "handle_node_cm_req
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_cm_sel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At this point of execution, the user selected a menu entry of the
menu build up in event handler method handle_node_cm_req.
Query your own function codes and react accordingly.
case fcode.
If Delete Subtree is selected.
when 'DEL_SUBTREE'.
call function 'POPUP_TO_CONFIRM_STEP'
exporting
textline1 = text-022
textline2 = text-023
titel = text-024
cancel_display = ' '
importing
answer = l_rc.
if l_rc eq 'J'.
Deleting Subtree if User confirms.
call method sender->delete_subtree
exporting
i_node_key = node_key.
Do not forget to refresh the display when you change the contents
of your list.
call method sender->frontend_update.
endif.
endcase.
endmethod. "handle_node_cm_sel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_checkbox_changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
data: lt_childrent like lt_children,
lt_childrenl like lt_children,
lt_childrenr like lt_children.
First Check if the node has child nodes or leaf or not
call method sender->get_children
exporting
i_node_key = node_key
importing
et_children = lt_children.
loop at lt_children into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrent.
endloop.
if lt_childrent is not initial.
loop at lt_childrent into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
loop at lt_childrent into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrenl.
endloop.
clear ls_children.
if lt_childrenl is not initial.
loop at lt_childrenl into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
loop at lt_childrenl into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrenr.
endloop.
clear ls_children.
if lt_childrenr is not initial.
loop at lt_childrenr into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
endif.
endif.
endif.
loop at lt_children into ls_children.
call method tree->get_outtab_line
exporting
i_node_key = ls_children-node_key
importing
e_outtab_line = gt_ekpo
et_item_layout = lt_item_ly
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
if gt_ekpo is not initial.
loop at lt_item_ly into wa_item_ly where fieldname = '&Hierarchy'.
if wa_item_ly-chosen is initial.
clear: ls_lvc_s_laci, ls_node_layout.
Setting the layout of the checkboxes (checked)
ls_lvc_s_laci-fieldname = tree->c_hierarchy_column_name.
ls_lvc_s_laci-u_chosen = 'X'.
ls_lvc_s_laci-chosen = 'X'.
append ls_lvc_s_laci to lt_lvc_t_laci.
clear: ls_lvc_s_laci, ls_node_layout.
else.
clear: ls_lvc_s_laci, ls_node_layout.
Setting the layout of the checkboxes (checked)
ls_lvc_s_laci-fieldname = tree->c_hierarchy_column_name.
ls_lvc_s_laci-u_chosen = 'X'.
ls_lvc_s_laci-chosen = ' '.
append ls_lvc_s_laci to lt_lvc_t_laci.
clear: ls_lvc_s_laci, ls_node_layout.
endif.
endloop.
Changing the ALV Tree Checkboxes Layout
call method tree->change_node
exporting
i_node_key = ls_children-node_key
i_outtab_line = gt_ekpo
is_node_layout = ls_node_layout
it_item_layout = lt_lvc_t_laci.
endif.
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method sender->frontend_update.
refresh: lt_children,
lt_lvc_t_laci,
lt_item_ly.
endmethod. "handle_checkbox_changed
endclass. "lcl_tree_event_receiver
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_toolbar_event_receiver definition.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public section.
For Each Function Selected in the Toolbar.
methods: on_function_selected
for event function_selected of cl_gui_toolbar
importing fcode sender,
For the Toolbar Dropdown
on_toolbar_dropdown
for event dropdown_clicked of cl_gui_toolbar
importing fcode
posx
posy
sender.
endclass. "lcl_toolbar_event_receiver DEFINITION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_toolbar_event_receiver implementation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method on_function_selected.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case fcode.
If Collapse Button is cliked on the TOOLBAR
when '&COLLAPSE'.
call method tree->collapse_all_nodes
exceptions
failed = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
If Expand Button is cliked on the TOOLBAR
when '&EXPAND'.
call method tree->expand_nodes
exporting
it_node_key = l_t_key
exceptions
failed = 1
cntl_system_error = 2
error_in_node_key_table = 3
dp_error = 4
node_not_found = 5
others = 6.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
refresh lt_nodes.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
If Select All Option of the Toolbar is selected.
when 'SELECT_ALL'.
loop at l_t_key into ld_nkey.
Getting the Subtrees for each node
call method tree->get_subtree
exporting
i_node_key = ld_nkey
importing
et_subtree_nodes = lt_nodes.
loop at lt_nodes into ld_nkey.
Getting the Each Line of the Each node of sub trees
call method tree->get_outtab_line
exporting
i_node_key = ld_nkey
importing
e_node_text = ld_ntext
et_item_layout = lt_items
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Looping at those items which has got checkbox property as editable
loop at lt_items into ls_item
where ( class = cl_gui_column_tree=>item_class_checkbox
and editable = 'X' ).
clear: ls_item_u.
Changing layout for the Checkbox Status to checked.
move-corresponding ls_item to ls_item_u.
ls_item_u-chosen = 'X'. " mark checkbox
ls_item_u-u_chosen = 'X'. " do update of checkbox
Changing each item in the tree with the new layout
call method tree->change_item
exporting
i_node_key = ld_nkey
i_fieldname = tree->c_hierarchy_column_name
i_data = ' '
i_u_data = ' ' " do not update i_data = node text
is_item_layout = ls_item_u
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear ls_item_u.
endloop. " items of a single node
endloop. " nodes
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'DESELECT_ALL'.
If DESelect All Option of the Toolbar is selected.
loop at l_t_key into ld_nkey.
Getting the Subtrees for each node
call method tree->get_subtree
exporting
i_node_key = ld_nkey
importing
et_subtree_nodes = lt_nodes.
loop at lt_nodes into ld_nkey.
Getting the Each Line of the Each node of sub trees
call method tree->get_outtab_line
exporting
i_node_key = ld_nkey
importing
e_node_text = ld_ntext
et_item_layout = lt_items
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Looping at those items which has got checkbox property as editable
loop at lt_items into ls_item
where ( class = cl_gui_column_tree=>item_class_checkbox
and editable = 'X' ).
clear: ls_item_u.
Changing layout for the Checkbox Status to uncheck.
move-corresponding ls_item to ls_item_u.
ls_item_u-chosen = ' '. " mark checkbox
ls_item_u-u_chosen = 'X'. " do update of checkbox
Changing each item in the tree with the new layout
call method tree->change_item
exporting
i_node_key = ld_nkey
i_fieldname = tree->c_hierarchy_column_name
i_data = ' '
i_u_data = ' ' " do not update i_data = node text
is_item_layout = ls_item_u
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear ls_item_u.
endloop. " items of a single node
endloop. " nodes
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'DELETE'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
delete subtree
if not l_selected_node is initial.
call method tree->delete_subtree
exporting
i_node_key = l_selected_node
i_update_parents_expander = ''
i_update_parents_folder = 'X'.
else.
message i227(0h).
endif.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_LC'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_last_child
is_outtab_line = ls_ekpo
i_node_text = 'Last Child'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_FC'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_first_child
is_outtab_line = ls_ekpo
i_node_text = 'First Child'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_FS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_first_sibling
is_outtab_line = ls_ekpo
i_node_text = 'First Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_LS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_last_sibling
is_outtab_line = ls_ekpo
i_node_text = 'Last Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_NS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_next_sibling
is_outtab_line = ls_ekpo
i_node_text = 'Next Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
endcase.
endmethod. "on_function_selected
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method on_toolbar_dropdown.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create contextmenu
create object l_menu.
clear l_fc_handled.
case fcode.
To Insert Child
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'.
Attaching the Context Menu to the Toolbar
call method mr_toolbar->track_context_menu
exporting
context_menu = l_menu
posx = posx
posy = posy.
endif.
endmethod. "on_toolbar_dropdown
*&----
*
START OF SELECTION
*&----
*
start-of-selection.
*&----
*
END OF SELECTION
*&----
*
end-of-selection.
Calling Screen 111 which has the Custom Container for the Tree Control
call screen 111.
*&----
*
*& S U B - R O U T I N E S
*&----
*
*&----
*
*& Module alv_tree_PBO OUTPUT
*&----
*
module alv_tree_pbo output.
Setting the PF STATUS
set pf-status 'MAIN100'.
If Tree Control is empty
if tree is initial.
Preparing the Tree for displaying the Required Format.
perform init_tree.
Calling Function FLUSH for Sending the Buffered Automation Queue
to Frontend
call method cl_gui_cfw=>flush
exceptions
cntl_system_error = 1
cntl_error = 2.
if sy-subrc ne 0.
Displaying the Error message if sy-subrc <> 0.
call function 'POPUP_TO_INFORM'
exporting
titel = text-013
txt1 = text-014
txt2 = text-015
txt3 = text-016 .
endif.
endif.
endmodule. " alv_tree_PBO OUTPUT
*&----
*
*& Form init_tree
*&----
*
form init_tree .
§1b. Create ALV Tree Control and corresponding Container.
create container for alv-tree
Name of the Custom Container on the Screen 111
l_tree_container_name = 'CUSTOM_CTRL'.
create object cc
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
others = 6
.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
Create ALV Tree Control Object within the Container
create object tree
exporting
parent = cc
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
hide_selection = 'X'
item_selection = '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
others = 8.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
§2. Create Hierarchy-header
Populating the HIERARCHY HEADER table
perform build_hierarchy_header changing l_hierarchy_header.
Populating the FIELDCATALOG
perform build_fieldcatllog.
Create info-table for html-header
perform get_htmlheader using wa_list_commentary
l_logo.
§3. Create empty Tree Control
IMPORTANT: Table 'gts_ekpo' 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.
refresh gts_ekpo.
call method tree->set_table_for_first_display
exporting
i_structure_name = 'Z9GV_EKPO'
is_hierarchy_header = l_hierarchy_header
it_list_commentary = wa_list_commentary
i_logo = l_logo
changing
it_outtab = gts_ekpo[]
it_fieldcatalog = it_fieldcatalog.
§4. Create hierarchy (nodes and leaves)
perform create_hierarchy.
Change Toolbar
perform change_toolbar.
Events Register
perform register_events.
§5. Send data to frontend.
call method tree->frontend_update.
Adjust column_width
CALL METHOD TREE->COLUMN_OPTIMIZE.
endform. " init_tree
*&----
*
*& Form build_hierarchy_header
*&----
*
<--P_HIERARCHY_HEADER text
*----
*
form build_hierarchy_header changing p_hierarchy_header type treev_hhdr.
Populating the Hierarchy Header Table for Defining the Header Node in
the Tree Heirarchy
p_hierarchy_header-heading = text-020.
p_hierarchy_header-t_image = it_ekpo-ebeln.
p_hierarchy_header-tooltip = text-021.
p_hierarchy_header-width = 40.
endform. " build_hierarchy_header
*&----
*
*& Form build_fieldcatllog
*&----
*
form build_fieldcatllog .
Selecting the Records from the Ekpo Table for the Selections made on
the Selection Screen.
select ebeln
ebelp
aedat
matnr
menge
meins
netpr
from ekpo into table it_ekpo
where ebeln in s_ebeln.
Calling the FM LVC_FIELDCATALOG_MERGE to Populate the Fieldcatalog
table for the ALV
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'Z9GV_EKPO'
changing
ct_fieldcat = it_fieldcatalog
exceptions
inconsistent_interface = 1
program_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " build_fieldcatllog
*&----
*
*& Form create_hierarchy
*&----
*
form create_hierarchy .
§4a. Select data
§4b. Sort output table according to your conceived hierarchy
sort it_ekpo by ebeln ebelp.
Adding the First Header Node in the Tree Heirarchy.
Providing no key means that the node is added on top level:
call method tree->add_node
exporting
i_relat_node_key = ''
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = text-012
importing
e_new_node_key = l_top_key.
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 it_ekpo.
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.
at new ebeln.
read table it_ekpo into la_ekpo with key ebeln = it_ekpo-ebeln.
Top level nodes:
set item-layout
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
node_text = la_ekpo-ebeln.
Adding the First Node based on the Data From the Output table
call method tree->add_node
exporting
i_relat_node_key = l_top_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = node_text
it_item_layout = lt_item_layout
importing
e_new_node_key = l_ebeln_key
exceptions
relat_node_not_found = 1
node_not_found = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
append l_ebeln_key to l_t_key .
endat.
at new ebelp.
clear la_ekpo.
read table it_ekpo into la_ekpo with key ebeln = it_ekpo-ebeln
ebelp = it_ekpo-ebelp.
ls_node_text = la_ekpo-ebelp.
set item-layout
clear ls_item_layout.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
clear ls_item_layout.
ls_item_layout-fieldname = 'EBELP'.
ls_item_layout-alignment = cl_gui_column_tree=>align_right.
append ls_item_layout to lt_item_layout.
Adding Second level of node
call method tree->add_node
exporting
i_relat_node_key = l_ebeln_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = ls_node_text
is_outtab_line = la_ekpo
it_item_layout = lt_item_layout
importing
e_new_node_key = l_ebelp_key
exceptions
relat_node_not_found = 1
node_not_found = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear la_ekpo.
endat.
clear la_ekpo.
clear it_ekpo.
endloop.
endform. " create_hierarchy
*&----
*
*& Module ALV_TREE_PAI INPUT
*&----
*
module alv_tree_pai input.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'EXIT' or 'BACK' or 'CANC'.
perform exit_program.
when others.
§6. Call dispatch to process toolbar functions
call method cl_gui_cfw=>dispatch.
endcase.
call method cl_gui_cfw=>flush.
endmodule. " ALV_TREE_PAI INPUT
*&----
*
*& Form exit_program
*&----
*
form exit_program .
Exiting the Program and Empting the ALV Tree
call method cc->free.
leave program.
endform. " exit_program
*&----
*
*& Form get_htmlheader
*----
*
-->P_WA_LIST_COMMENTARY text
*----
*
form get_htmlheader using p_list_commentary type slis_t_listheader
p_logo type sdydo_value.
Populating the LIST_COMMENTARY Table for the HTML Header in the ALV.
clear ls_line.
ls_line-typ = 'H'.
ls_line-info = text-017.
append ls_line to p_list_commentary.
clear ls_line.
ls_line-typ = 'S'.
ls_line-info = text-018.
append ls_line to p_list_commentary.
ls_line-key = text-019.
ls_line-info = sy-datum.
append ls_line to p_list_commentary.
p_logo = 'ENJOYSAP_LOGO'.
endform. " get_htmlheader
*&----
*
*& Form register_events
*&----
*
form register_events .
Declaring the Refrence variable to the CLASS Created for handling the
Tree Events
data : l_event_receiver type ref to lcl_tree_event_receiver.
Fetching all the Registered events of the tree.
call method tree->get_registered_events
importing
events = lt_events.
Attachign the Event ID To the Required Events in the Events Table
l_event-eventid = cl_gui_column_tree=>eventid_node_double_click.
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_node_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_checkbox_change.
append l_event to lt_events.
Registering the Event Id Attached to the Events in the Table
call method 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.
Creating the Object of Tree Event Receiver CLass
create object l_event_receiver.
Setting the Handler for the Events Used.
set handler l_event_receiver->handle_node_double_click for tree.
set handler l_event_receiver->handle_node_cm_req for tree.
set handler l_event_receiver->handle_node_cm_sel for tree.
set handler l_event_receiver->handle_checkbox_changed for tree.
endform. " register_events
*&----
*
*& Form Change_Toolbar
*&----
*
form change_toolbar .
Declaring the Refrence Variable to the Toolbar Event Receiver Class.
data : toolbar_event_receiver type ref to lcl_toolbar_event_receiver.
get toolbar control
call method tree->get_toolbar_object
importing
er_toolbar = mr_toolbar.
check not mr_toolbar is initial.
add seperator to toolbar
call method mr_toolbar->add_button
exporting
fcode = ''
icon = ''
butn_type = cntb_btype_sep
text = ''
quickinfo = 'This is a Seperator'. "#EC NOTEXT
add Standard Button to toolbar (for Delete Subtree)
call method mr_toolbar->add_button
exporting
fcode = 'DELETE'
icon = '@18@'
butn_type = cntb_btype_button
text = ''
quickinfo = 'Delete subtree'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'INSERT_LC'
icon = '@17@'
butn_type = cntb_btype_dropdown
text = ''
quickinfo = 'Insert Line'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'SELECT_ALL'
icon = '@4B@'
butn_type = '' "cntb_btype_dropdown
text = ''
quickinfo = 'SELECT ALL'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'DESELECT_ALL'
icon = '@4D@'
butn_type = '' "CNTB_BTYPE_CHECK
text = ''
quickinfo = 'DESELECT ALL'. "#EC NOTEXT
Creating the Object of the Toolbar Events Receiver CLass.
create object toolbar_event_receiver.
set event-handler for toolbar-control
set handler toolbar_event_receiver->on_function_selected
for mr_toolbar.
set handler toolbar_event_receiver->on_toolbar_dropdown
for mr_toolbar.
endform. " Change_Toolbar
Thanks & Regards,
Vivek Gaur
‎2008 Aug 29 8:35 AM
‎2008 Aug 29 8:39 AM
‎2008 Aug 29 8:42 AM
Refer to this program...run it and try to understand it
*&----
*
*& Report Z9_KHUSH_ALVTREE
*&----
*
*& Date Name
*& 15-05-07 Khushboo Gupta
*&----
*
*& This Report Prints the Purchase Orders and its Items in the ALV TREE
*& format with the various option like Select All, Deselect All,
*& Insert First Child, Next Child, Last Child,
*& Insert First Sibling Next Sibling, Last Sibling,
*& Delete Node, Context Menu, Parent-Child Click for Checkboxes,
*& Toolbar Scrolldown
*&----
*
report z9_khush_alvtree.
type-pools : slis.
*----
*
*DATA DECLARATION
*----
*
*--> Internal Table
data : it_ekpo type standard table of z9gv_ekpo with header line,
gts_ekpo type standard table of z9gv_ekpo with header line,
gt_ekpo type standard table of z9gv_ekpo with header line,
it_fieldcatalog type lvc_t_fcat,
lt_nodes type lvc_t_nkey,
lt_items type lvc_t_layi,
l_t_key type lvc_t_nkey,
l_hierarchy_header type treev_hhdr.
*--> Work Area Declaration
data : wa_ekpo type z9gv_ekpo,
wa_fieldcatalog type lvc_s_fcat,
wa_list_commentary type slis_t_listheader,
ls_line type slis_listheader,
la_ekpo type z9gv_ekpo,
ls_item_u type lvc_s_laci,
ls_item type lvc_s_layi,
ls_item_layout type lvc_s_layi.
Refrence Variable for the ALV TREE -
*--> Tree,Container and Toolbar.
data : tree type ref to cl_gui_alv_tree,
cc type ref to cl_gui_custom_container,
mr_toolbar type ref to cl_gui_toolbar.
*--> Variables Declaration.
data : ok_code type sy-ucomm,
save_ok type sy-ucomm,
ld_nkey type lvc_nkey,
ld_ntext type lvc_value,
l_tree_container_name(30) type c,
l_logo type sdydo_value,
l_ebeln_key type lvc_nkey,
l_ebelp_key type lvc_nkey,
l_top_key type lvc_nkey,
node_text type lvc_value,
ls_node_text type lvc_value,
lt_events type cntl_simple_events,
l_event type cntl_simple_event.
For Classes lcl_tree_event_receiver and lcl_toolbar_event_receiver
data : l_rc type c,
lt_children type lvc_t_nkey,
ls_children type lvc_s_nkey,
ls_lvc_s_laci type lvc_s_laci,
lt_lvc_t_laci type lvc_t_laci,
ls_node_layout type lvc_s_lacn,
lt_item_ly type lvc_t_layi,
wa_item_ly type lvc_s_layi,
ls_ekpo type z9gv_ekpo,
lt_item_layout type lvc_t_layi,
lt_selected_node type lvc_t_nkey,
l_menu type ref to cl_ctmenu,
l_fc_handled type as4flag,
l_selected_node type lvc_nkey.
*&----
*
*& Selection Screen
*&----
*
selection-screen : begin of block blk1.
select-options : s_ebeln for wa_ekpo-ebeln.
selection-screen : end of block blk1.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_tree_event_receiver definition.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public section.
Define an event handler method for each event you want to
react to the events
*--> For Node Double Click
methods handle_node_double_click
for event node_double_click of cl_gui_alv_tree
importing node_key sender.
*--> For Parent-Child Checkbox Check
methods handle_checkbox_changed
for event checkbox_change of cl_gui_alv_tree
importing node_key sender.
*--> For Node Context Menu Request
methods: handle_node_cm_req
for event node_context_menu_request of cl_gui_alv_tree
importing node_key menu.
*--> For Node Context Menu Selected
methods: handle_node_cm_sel
for event node_context_menu_selected of cl_gui_alv_tree
importing node_key fcode sender.
endclass. "lcl_tree_event_receiver DEFINITION
**********************************************************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_tree_event_receiver implementation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implement your event handler methods.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_double_click.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 not lt_children is initial.
Expand the nodes which has child nodes or leaf
call method sender->expand_node
exporting
i_node_key = node_key
i_level_count = 2.
endif.
refresh lt_children.
endmethod. "handle_node_double_click
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_cm_req.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Event parameter 'menu' holds a reference to the standard context
menu of ALV Tree (functions 'Expand'/'Collapse' on nodes).
You may either append your own functions (separated by a line)
or clear the menu if you do not want to allow standard functions.
In this case the standard menu is cleared.
call method menu->clear.
The next line defines one line of the context menu.
call method menu->add_function
exporting
fcode = 'DEL_SUBTREE'
text = text-011. "Delete Subtree
endmethod. "handle_node_cm_req
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_node_cm_sel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At this point of execution, the user selected a menu entry of the
menu build up in event handler method handle_node_cm_req.
Query your own function codes and react accordingly.
case fcode.
If Delete Subtree is selected.
when 'DEL_SUBTREE'.
call function 'POPUP_TO_CONFIRM_STEP'
exporting
textline1 = text-022
textline2 = text-023
titel = text-024
cancel_display = ' '
importing
answer = l_rc.
if l_rc eq 'J'.
Deleting Subtree if User confirms.
call method sender->delete_subtree
exporting
i_node_key = node_key.
Do not forget to refresh the display when you change the contents
of your list.
call method sender->frontend_update.
endif.
endcase.
endmethod. "handle_node_cm_sel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method handle_checkbox_changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
data: lt_childrent like lt_children,
lt_childrenl like lt_children,
lt_childrenr like lt_children.
First Check if the node has child nodes or leaf or not
call method sender->get_children
exporting
i_node_key = node_key
importing
et_children = lt_children.
loop at lt_children into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrent.
endloop.
if lt_childrent is not initial.
loop at lt_childrent into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
loop at lt_childrent into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrenl.
endloop.
clear ls_children.
if lt_childrenl is not initial.
loop at lt_childrenl into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
loop at lt_childrenl into ls_children.
Get the Each Selected output line in the table gt_ekpo
for each child node
call method sender->get_children
exporting
i_node_key = ls_children-node_key
importing
et_children = lt_childrenr.
endloop.
clear ls_children.
if lt_childrenr is not initial.
loop at lt_childrenr into ls_children.
append ls_children to lt_children.
endloop.
clear ls_children.
endif.
endif.
endif.
loop at lt_children into ls_children.
call method tree->get_outtab_line
exporting
i_node_key = ls_children-node_key
importing
e_outtab_line = gt_ekpo
et_item_layout = lt_item_ly
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
if gt_ekpo is not initial.
loop at lt_item_ly into wa_item_ly where fieldname = '&Hierarchy'.
if wa_item_ly-chosen is initial.
clear: ls_lvc_s_laci, ls_node_layout.
Setting the layout of the checkboxes (checked)
ls_lvc_s_laci-fieldname = tree->c_hierarchy_column_name.
ls_lvc_s_laci-u_chosen = 'X'.
ls_lvc_s_laci-chosen = 'X'.
append ls_lvc_s_laci to lt_lvc_t_laci.
clear: ls_lvc_s_laci, ls_node_layout.
else.
clear: ls_lvc_s_laci, ls_node_layout.
Setting the layout of the checkboxes (checked)
ls_lvc_s_laci-fieldname = tree->c_hierarchy_column_name.
ls_lvc_s_laci-u_chosen = 'X'.
ls_lvc_s_laci-chosen = ' '.
append ls_lvc_s_laci to lt_lvc_t_laci.
clear: ls_lvc_s_laci, ls_node_layout.
endif.
endloop.
Changing the ALV Tree Checkboxes Layout
call method tree->change_node
exporting
i_node_key = ls_children-node_key
i_outtab_line = gt_ekpo
is_node_layout = ls_node_layout
it_item_layout = lt_lvc_t_laci.
endif.
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method sender->frontend_update.
refresh: lt_children,
lt_lvc_t_laci,
lt_item_ly.
endmethod. "handle_checkbox_changed
endclass. "lcl_tree_event_receiver
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_toolbar_event_receiver definition.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public section.
For Each Function Selected in the Toolbar.
methods: on_function_selected
for event function_selected of cl_gui_toolbar
importing fcode sender,
For the Toolbar Dropdown
on_toolbar_dropdown
for event dropdown_clicked of cl_gui_toolbar
importing fcode
posx
posy
sender.
endclass. "lcl_toolbar_event_receiver DEFINITION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class lcl_toolbar_event_receiver implementation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method on_function_selected.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case fcode.
If Collapse Button is cliked on the TOOLBAR
when '&COLLAPSE'.
call method tree->collapse_all_nodes
exceptions
failed = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
If Expand Button is cliked on the TOOLBAR
when '&EXPAND'.
call method tree->expand_nodes
exporting
it_node_key = l_t_key
exceptions
failed = 1
cntl_system_error = 2
error_in_node_key_table = 3
dp_error = 4
node_not_found = 5
others = 6.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
refresh lt_nodes.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
If Select All Option of the Toolbar is selected.
when 'SELECT_ALL'.
loop at l_t_key into ld_nkey.
Getting the Subtrees for each node
call method tree->get_subtree
exporting
i_node_key = ld_nkey
importing
et_subtree_nodes = lt_nodes.
loop at lt_nodes into ld_nkey.
Getting the Each Line of the Each node of sub trees
call method tree->get_outtab_line
exporting
i_node_key = ld_nkey
importing
e_node_text = ld_ntext
et_item_layout = lt_items
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Looping at those items which has got checkbox property as editable
loop at lt_items into ls_item
where ( class = cl_gui_column_tree=>item_class_checkbox
and editable = 'X' ).
clear: ls_item_u.
Changing layout for the Checkbox Status to checked.
move-corresponding ls_item to ls_item_u.
ls_item_u-chosen = 'X'. " mark checkbox
ls_item_u-u_chosen = 'X'. " do update of checkbox
Changing each item in the tree with the new layout
call method tree->change_item
exporting
i_node_key = ld_nkey
i_fieldname = tree->c_hierarchy_column_name
i_data = ' '
i_u_data = ' ' " do not update i_data = node text
is_item_layout = ls_item_u
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear ls_item_u.
endloop. " items of a single node
endloop. " nodes
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'DESELECT_ALL'.
If DESelect All Option of the Toolbar is selected.
loop at l_t_key into ld_nkey.
Getting the Subtrees for each node
call method tree->get_subtree
exporting
i_node_key = ld_nkey
importing
et_subtree_nodes = lt_nodes.
loop at lt_nodes into ld_nkey.
Getting the Each Line of the Each node of sub trees
call method tree->get_outtab_line
exporting
i_node_key = ld_nkey
importing
e_node_text = ld_ntext
et_item_layout = lt_items
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
Looping at those items which has got checkbox property as editable
loop at lt_items into ls_item
where ( class = cl_gui_column_tree=>item_class_checkbox
and editable = 'X' ).
clear: ls_item_u.
Changing layout for the Checkbox Status to uncheck.
move-corresponding ls_item to ls_item_u.
ls_item_u-chosen = ' '. " mark checkbox
ls_item_u-u_chosen = 'X'. " do update of checkbox
Changing each item in the tree with the new layout
call method tree->change_item
exporting
i_node_key = ld_nkey
i_fieldname = tree->c_hierarchy_column_name
i_data = ' '
i_u_data = ' ' " do not update i_data = node text
is_item_layout = ls_item_u
exceptions
node_not_found = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear ls_item_u.
endloop. " items of a single node
endloop. " nodes
endloop.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'DELETE'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
delete subtree
if not l_selected_node is initial.
call method tree->delete_subtree
exporting
i_node_key = l_selected_node
i_update_parents_expander = ''
i_update_parents_folder = 'X'.
else.
message i227(0h).
endif.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_LC'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_last_child
is_outtab_line = ls_ekpo
i_node_text = 'Last Child'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_FC'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_first_child
is_outtab_line = ls_ekpo
i_node_text = 'First Child'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_FS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_first_sibling
is_outtab_line = ls_ekpo
i_node_text = 'First Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_LS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_last_sibling
is_outtab_line = ls_ekpo
i_node_text = 'Last Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
when 'INSERT_NS'.
get selected node
call method tree->get_selected_nodes
changing
ct_selected_nodes = lt_selected_node.
call method cl_gui_cfw=>flush.
read table lt_selected_node into l_selected_node index 1.
get current Line
if not l_selected_node is initial.
call method tree->get_outtab_line
exporting
i_node_key = l_selected_node
importing
e_outtab_line = ls_ekpo.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
call method tree->add_node
exporting
i_relat_node_key = l_selected_node
i_relationship = cl_tree_control_base=>relat_next_sibling
is_outtab_line = ls_ekpo
i_node_text = 'Next Sibling'
it_item_layout = lt_item_layout. "#EC NOTEXT
else.
message i227(0h).
endif.
refresh lt_item_layout.
Do not forget to refresh the display when you change the contents
of your list.
call method tree->frontend_update.
endcase.
endmethod. "on_function_selected
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method on_toolbar_dropdown.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create contextmenu
create object l_menu.
clear l_fc_handled.
case fcode.
To Insert Child
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'.
Attaching the Context Menu to the Toolbar
call method mr_toolbar->track_context_menu
exporting
context_menu = l_menu
posx = posx
posy = posy.
endif.
endmethod. "on_toolbar_dropdown
*&----
*
START OF SELECTION
*&----
*
start-of-selection.
*&----
*
END OF SELECTION
*&----
*
end-of-selection.
Calling Screen 111 which has the Custom Container for the Tree Control
call screen 111.
*&----
*
*& S U B - R O U T I N E S
*&----
*
*&----
*
*& Module alv_tree_PBO OUTPUT
*&----
*
module alv_tree_pbo output.
Setting the PF STATUS
set pf-status 'MAIN100'.
If Tree Control is empty
if tree is initial.
Preparing the Tree for displaying the Required Format.
perform init_tree.
Calling Function FLUSH for Sending the Buffered Automation Queue
to Frontend
call method cl_gui_cfw=>flush
exceptions
cntl_system_error = 1
cntl_error = 2.
if sy-subrc ne 0.
Displaying the Error message if sy-subrc <> 0.
call function 'POPUP_TO_INFORM'
exporting
titel = text-013
txt1 = text-014
txt2 = text-015
txt3 = text-016 .
endif.
endif.
endmodule. " alv_tree_PBO OUTPUT
*&----
*
*& Form init_tree
*&----
*
form init_tree .
§1b. Create ALV Tree Control and corresponding Container.
create container for alv-tree
Name of the Custom Container on the Screen 111
l_tree_container_name = 'CUSTOM_CTRL'.
create object cc
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
others = 6
.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
Create ALV Tree Control Object within the Container
create object tree
exporting
parent = cc
node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
hide_selection = 'X'
item_selection = '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
others = 8.
if sy-subrc <> 0.
message x208(00) with 'ERROR'.
endif.
§2. Create Hierarchy-header
Populating the HIERARCHY HEADER table
perform build_hierarchy_header changing l_hierarchy_header.
Populating the FIELDCATALOG
perform build_fieldcatllog.
Create info-table for html-header
perform get_htmlheader using wa_list_commentary
l_logo.
§3. Create empty Tree Control
IMPORTANT: Table 'gts_ekpo' 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.
refresh gts_ekpo.
call method tree->set_table_for_first_display
exporting
i_structure_name = 'Z9GV_EKPO'
is_hierarchy_header = l_hierarchy_header
it_list_commentary = wa_list_commentary
i_logo = l_logo
changing
it_outtab = gts_ekpo[]
it_fieldcatalog = it_fieldcatalog.
§4. Create hierarchy (nodes and leaves)
perform create_hierarchy.
Change Toolbar
perform change_toolbar.
Events Register
perform register_events.
§5. Send data to frontend.
call method tree->frontend_update.
Adjust column_width
CALL METHOD TREE->COLUMN_OPTIMIZE.
endform. " init_tree
*&----
*
*& Form build_hierarchy_header
*&----
*
<--P_HIERARCHY_HEADER text
*----
*
form build_hierarchy_header changing p_hierarchy_header type treev_hhdr.
Populating the Hierarchy Header Table for Defining the Header Node in
the Tree Heirarchy
p_hierarchy_header-heading = text-020.
p_hierarchy_header-t_image = it_ekpo-ebeln.
p_hierarchy_header-tooltip = text-021.
p_hierarchy_header-width = 40.
endform. " build_hierarchy_header
*&----
*
*& Form build_fieldcatllog
*&----
*
form build_fieldcatllog .
Selecting the Records from the Ekpo Table for the Selections made on
the Selection Screen.
select ebeln
ebelp
aedat
matnr
menge
meins
netpr
from ekpo into table it_ekpo
where ebeln in s_ebeln.
Calling the FM LVC_FIELDCATALOG_MERGE to Populate the Fieldcatalog
table for the ALV
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'Z9GV_EKPO'
changing
ct_fieldcat = it_fieldcatalog
exceptions
inconsistent_interface = 1
program_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " build_fieldcatllog
*&----
*
*& Form create_hierarchy
*&----
*
form create_hierarchy .
§4a. Select data
§4b. Sort output table according to your conceived hierarchy
sort it_ekpo by ebeln ebelp.
Adding the First Header Node in the Tree Heirarchy.
Providing no key means that the node is added on top level:
call method tree->add_node
exporting
i_relat_node_key = ''
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = text-012
importing
e_new_node_key = l_top_key.
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 it_ekpo.
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.
at new ebeln.
read table it_ekpo into la_ekpo with key ebeln = it_ekpo-ebeln.
Top level nodes:
set item-layout
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
node_text = la_ekpo-ebeln.
Adding the First Node based on the Data From the Output table
call method tree->add_node
exporting
i_relat_node_key = l_top_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = node_text
it_item_layout = lt_item_layout
importing
e_new_node_key = l_ebeln_key
exceptions
relat_node_not_found = 1
node_not_found = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
append l_ebeln_key to l_t_key .
endat.
at new ebelp.
clear la_ekpo.
read table it_ekpo into la_ekpo with key ebeln = it_ekpo-ebeln
ebelp = it_ekpo-ebelp.
ls_node_text = la_ekpo-ebelp.
set item-layout
clear ls_item_layout.
ls_item_layout-fieldname = tree->c_hierarchy_column_name.
ls_item_layout-class = cl_gui_column_tree=>item_class_checkbox.
ls_item_layout-editable = 'X'.
append ls_item_layout to lt_item_layout.
clear ls_item_layout.
ls_item_layout-fieldname = 'EBELP'.
ls_item_layout-alignment = cl_gui_column_tree=>align_right.
append ls_item_layout to lt_item_layout.
Adding Second level of node
call method tree->add_node
exporting
i_relat_node_key = l_ebeln_key
i_relationship = cl_gui_column_tree=>relat_last_child
i_node_text = ls_node_text
is_outtab_line = la_ekpo
it_item_layout = lt_item_layout
importing
e_new_node_key = l_ebelp_key
exceptions
relat_node_not_found = 1
node_not_found = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear la_ekpo.
endat.
clear la_ekpo.
clear it_ekpo.
endloop.
endform. " create_hierarchy
*&----
*
*& Module ALV_TREE_PAI INPUT
*&----
*
module alv_tree_pai input.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'EXIT' or 'BACK' or 'CANC'.
perform exit_program.
when others.
§6. Call dispatch to process toolbar functions
call method cl_gui_cfw=>dispatch.
endcase.
call method cl_gui_cfw=>flush.
endmodule. " ALV_TREE_PAI INPUT
*&----
*
*& Form exit_program
*&----
*
form exit_program .
Exiting the Program and Empting the ALV Tree
call method cc->free.
leave program.
endform. " exit_program
*&----
*
*& Form get_htmlheader
*----
*
-->P_WA_LIST_COMMENTARY text
*----
*
form get_htmlheader using p_list_commentary type slis_t_listheader
p_logo type sdydo_value.
Populating the LIST_COMMENTARY Table for the HTML Header in the ALV.
clear ls_line.
ls_line-typ = 'H'.
ls_line-info = text-017.
append ls_line to p_list_commentary.
clear ls_line.
ls_line-typ = 'S'.
ls_line-info = text-018.
append ls_line to p_list_commentary.
ls_line-key = text-019.
ls_line-info = sy-datum.
append ls_line to p_list_commentary.
p_logo = 'ENJOYSAP_LOGO'.
endform. " get_htmlheader
*&----
*
*& Form register_events
*&----
*
form register_events .
Declaring the Refrence variable to the CLASS Created for handling the
Tree Events
data : l_event_receiver type ref to lcl_tree_event_receiver.
Fetching all the Registered events of the tree.
call method tree->get_registered_events
importing
events = lt_events.
Attachign the Event ID To the Required Events in the Events Table
l_event-eventid = cl_gui_column_tree=>eventid_node_double_click.
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_node_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_checkbox_change.
append l_event to lt_events.
Registering the Event Id Attached to the Events in the Table
call method 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.
Creating the Object of Tree Event Receiver CLass
create object l_event_receiver.
Setting the Handler for the Events Used.
set handler l_event_receiver->handle_node_double_click for tree.
set handler l_event_receiver->handle_node_cm_req for tree.
set handler l_event_receiver->handle_node_cm_sel for tree.
set handler l_event_receiver->handle_checkbox_changed for tree.
endform. " register_events
*&----
*
*& Form Change_Toolbar
*&----
*
form change_toolbar .
Declaring the Refrence Variable to the Toolbar Event Receiver Class.
data : toolbar_event_receiver type ref to lcl_toolbar_event_receiver.
get toolbar control
call method tree->get_toolbar_object
importing
er_toolbar = mr_toolbar.
check not mr_toolbar is initial.
add seperator to toolbar
call method mr_toolbar->add_button
exporting
fcode = ''
icon = ''
butn_type = cntb_btype_sep
text = ''
quickinfo = 'This is a Seperator'. "#EC NOTEXT
add Standard Button to toolbar (for Delete Subtree)
call method mr_toolbar->add_button
exporting
fcode = 'DELETE'
icon = '@18@'
butn_type = cntb_btype_button
text = ''
quickinfo = 'Delete subtree'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'INSERT_LC'
icon = '@17@'
butn_type = cntb_btype_dropdown
text = ''
quickinfo = 'Insert Line'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'SELECT_ALL'
icon = '@4B@'
butn_type = '' "cntb_btype_dropdown
text = ''
quickinfo = 'SELECT ALL'. "#EC NOTEXT
add Dropdown Button to toolbar (for Insert Line)
call method mr_toolbar->add_button
exporting
fcode = 'DESELECT_ALL'
icon = '@4D@'
butn_type = '' "CNTB_BTYPE_CHECK
text = ''
quickinfo = 'DESELECT ALL'. "#EC NOTEXT
Creating the Object of the Toolbar Events Receiver CLass.
create object toolbar_event_receiver.
set event-handler for toolbar-control
set handler toolbar_event_receiver->on_function_selected
for mr_toolbar.
set handler toolbar_event_receiver->on_toolbar_dropdown
for mr_toolbar.
endform. " Change_Toolbar
Thanks & Regards,
Vivek Gaur
‎2008 Aug 29 1:09 PM