*&---------------------------------------------------------------------*
*& Include Z_HANDLE_CLASS_FILE
*&---------------------------------------------------------------------*
DATA tree_model TYPE REF TO cl_column_tree_model.
DATA grid TYPE REF TO cl_gui_alv_grid.
DATA BEGIN OF my_wa.
INCLUDE STRUCTURE zstudent.
DATA course LIKE zstucourse-course.
DATA score LIKE zstucourse-score.
DATA END OF my_wa.
DATA my_tab LIKE TABLE OF my_wa WITH KEY stu_no course.
DATA: BEGIN OF stu_wa,
stu_name LIKE my_wa-stu_name,
course LIKE my_wa-course,
score LIKE my_wa-score,
END OF stu_wa.
DATA stu_tab LIKE TABLE OF stu_wa WITH KEY course.
DATA temp_no LIKE zstudent-stu_no.
DATA: fcat_tab TYPE lvc_t_fcat,
fcat_wa LIKE LINE OF fcat_tab.
*----------------------------------------------------------------------*
* CLASS handle_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
****************** define class ***************************************
CLASS handle_class DEFINITION.
PUBLIC SECTION.
CLASS-METHODS handle_node_click FOR EVENT node_double_click OF cl_column_tree_model
IMPORTING
node_key.
CLASS-METHODS handle_menu FOR EVENT node_context_menu_request OF cl_column_tree_model
IMPORTING
node_key
menu.
CLASS-METHODS handle_menu_select FOR EVENT node_context_menu_select OF cl_column_tree_model
IMPORTING
node_key
fcode.
ENDCLASS. "handle_class DEFINITION
*----------------------------------------------------------------------*
* CLASS handle_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS handle_class IMPLEMENTATION.
METHOD handle_node_click.
CLEAR stu_tab.
IF STRLEN( node_key ) = 10.
temp_no = node_key+0(10).
LOOP AT my_tab INTO my_wa.
IF my_wa-stu_no = temp_no.
MOVE-CORRESPONDING my_wa TO stu_wa.
APPEND stu_wa TO stu_tab.
ENDIF.
ENDLOOP.
ENDIF.
ENDMETHOD. "handle_node_click
METHOD handle_menu.
CALL METHOD menu->clear.
IF strlen( node_key ) > 10.
CALL METHOD menu->add_function
EXPORTING
fcode = 'DONOTHING'
text = '没事 '.
CALL METHOD menu->add_function
EXPORTING
fcode = 'DELETE'
text = '删除 '.
ENDIF.
ENDMETHOD. "handle_menu
METHOD handle_menu_select.
IF fcode = 'DELETE'.
CALL METHOD tree_model->delete_node
EXPORTING
node_key = node_key.
DELETE TABLE stu_tab WITH TABLE KEY course = node_key+10.
DELETE TABLE my_tab WITH TABLE KEY stu_no = node_key+0(10) course = node_key+10.
CALL METHOD grid->refresh_table_display.
DELETE FROM zstucourse WHERE stu_no = node_key+0(10) AND course = node_key+10.
ENDIF.
ENDMETHOD.
ENDCLASS. "handle_class IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Report Z_TREE_DEMO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_tree_demo.
INCLUDE .
INCLUDE z_handle_class_file.
DATA: ok_code TYPE sy-ucomm,
save_ok LIKE ok_code,
dock_container TYPE REF TO cl_gui_docking_container,
dock_container2 LIKE dock_container,
tree_header TYPE treemhhdr,
tree_items TYPE treemcitab,
tree_item TYPE treemcitem,
node_key TYPE string,
node_key2 TYPE string,
temp_score TYPE i,
temp_name(20),
events_tab TYPE cntl_simple_events WITH HEADER LINE.
START-OF-SELECTION.
SELECT zstudent~stu_no course score stu_name stu_age FROM zstudent
INNER JOIN zstucourse ON zstudent~stu_no = zstucourse~stu_no
INTO CORRESPONDING FIELDS OF TABLE my_tab.
events_tab-eventid = cl_column_tree_model=>eventid_node_double_click.
events_tab-appl_event = 'X'.
APPEND events_tab.
events_tab-eventid = cl_column_tree_model=>eventid_node_context_menu_req.
events_tab-appl_event = 'X'.
APPEND events_tab.
fcat_wa-fieldname = 'STU_NAME'.
fcat_wa-scrtext_m = '姓名'.
APPEND fcat_wa TO fcat_tab.
CLEAR fcat_wa.
fcat_wa-fieldname = 'COURSE'.
fcat_wa-scrtext_m = '课程'.
APPEND fcat_wa TO fcat_tab.
CLEAR fcat_wa.
fcat_wa-fieldname = 'SCORE'.
fcat_wa-scrtext_m = '分数'.
APPEND fcat_wa TO fcat_tab.
CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'STATUS_9000'.
SET TITLEBAR 'TITLEBAR_9000'.
ENDMODULE. " STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
SET SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
*& Module initial_container OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE initial_container OUTPUT.
IF dock_container IS INITIAL.
CREATE OBJECT dock_container
EXPORTING
ratio = 40.
ENDIF.
IF dock_container2 IS INITIAL.
CREATE OBJECT dock_container2
EXPORTING
* side = cl_gui_docking_container=>dock_at_right
ratio = 50.
ENDIF.
ENDMODULE. " initial_container OUTPUT
*&---------------------------------------------------------------------*
*& Module initial_tree OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE initial_tree OUTPUT.
IF tree_model IS INITIAL.
tree_header-tooltip = 'Singel Lee''s tree'.
tree_header-t_image = icon_ws_plane.
tree_header-heading = '课程'.
tree_header-width = 40.
CREATE OBJECT tree_model
EXPORTING
node_selection_mode = cl_column_tree_model=>node_sel_mode_single
hierarchy_column_name = 'STU'
hierarchy_header = tree_header.
CALL METHOD tree_model->create_tree_control
EXPORTING
parent = dock_container.
CALL METHOD tree_model->add_column
EXPORTING
name = 'STU_NAME'
width = 20
header_text = '学生姓名'.
CALL METHOD tree_model->add_column
EXPORTING
name = 'SCORE'
width = 20
header_text = '分数'.
tree_item-class = cl_column_tree_model=>item_class_text.
tree_item-item_name = 'STU'.
tree_item-text = '西华大学'.
APPEND tree_item TO tree_items.
CALL METHOD tree_model->add_node
EXPORTING
node_key = 'ROOT'
isfolder = 'X'
expander = 'X'
item_table = tree_items.
CLEAR tree_items.
tree_item-text = '西南交通大学'.
APPEND tree_item TO tree_items.
CALL METHOD tree_model->add_node
EXPORTING
node_key = 'ROOT2'
isfolder = 'X'
* expander = 'X'
item_table = tree_items.
LOOP AT my_tab INTO my_wa.
temp_score = my_wa-score.
temp_name = my_wa-stu_name.
AT NEW stu_no.
CLEAR tree_item.
CLEAR tree_items.
node_key = my_wa-stu_no.
tree_item-class = cl_column_tree_model=>item_class_text.
tree_item-item_name = 'STU'.
tree_item-text = my_wa-stu_no.
APPEND tree_item TO tree_items.
tree_item-item_name = 'STU_NAME'.
tree_item-text = temp_name.
APPEND tree_item TO tree_items.
CALL METHOD tree_model->add_node
EXPORTING
node_key = node_key
relative_node_key = 'ROOT'
relationship = cl_column_tree_model=>relat_last_child
isfolder = 'X'
expander = 'X'
item_table = tree_items.
ENDAT.
AT NEW course.
CONCATENATE node_key my_wa-course INTO node_key2.
CLEAR tree_item.
CLEAR tree_items.
tree_item-class = cl_column_tree_model=>item_class_text.
tree_item-item_name = 'STU'.
tree_item-text = my_wa-course.
APPEND tree_item TO tree_items.
tree_item-item_name = 'SCORE'.
tree_item-text = temp_score.
APPEND tree_item TO tree_items.
CALL METHOD tree_model->add_node
EXPORTING
node_key = node_key2
relative_node_key = node_key
relationship = cl_column_tree_model=>relat_last_child
isfolder = ''
expander = ''
item_table = tree_items.
ENDAT.
ENDLOOP.
CALL METHOD tree_model->set_registered_events
EXPORTING
events = events_tab[].
SET HANDLER handle_class=>handle_node_click FOR tree_model.
SET HANDLER handle_class=>handle_menu FOR tree_model.
SET HANDLER handle_class=>handle_menu_select FOR tree_model.
ENDIF.
ENDMODULE. " initial_tree OUTPUT
*&---------------------------------------------------------------------*
*& Module initial_grid OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE initial_grid OUTPUT.
IF grid IS INITIAL.
CREATE OBJECT grid
EXPORTING
i_parent = dock_container2.
CALL METHOD grid->set_table_for_first_display
CHANGING
it_outtab = stu_tab
it_fieldcatalog = fcat_tab.
ELSE.
CALL METHOD grid->refresh_table_display.
ENDIF.
ENDMODULE. " initial_grid OUTPUT
************屏幕9000的逻辑流:
PROCESS BEFORE OUTPUT.
MODULE STATUS_9000.
MODULE initial_container.
MODULE initial_tree.
MODULE initial_grid.
PROCESS AFTER INPUT.
MODULE USER_COMMAND_9000.