Tree Control 对象与ALV Grid 对象关联
在双击 Tree 对象时,变更ALV Trid 对象的显示,实现界面如图9-11 所示。
Screen 设计界面如图9-12 所示。
主程序:
REPORT ytest36.
DATA: ok_code TYPE sy-ucomm,save_ok TYPE sy-ucomm.
DATA: wa_container TYPE scrfname VALUE 'TREE1',wa_containeralv TYPE scrfname VALUE 'ALV1',wa_custom_container TYPE REF TO cl_gui_custom_container,wa_custom_containeralv TYPE REF TO cl_gui_custom_container,wa_tree TYPE REF TO cl_gui_simple_tree,wa_alv TYPE REF TO cl_gui_alv_grid.
DATA: node_table LIKE TABLE OF mtreesnode,node1 TYPE mtreesnode.
DATA wa_spfli TYPE TABLE OF spfli WITH HEADER LINE.
DATA wa_sflight TYPE TABLE OF sflight.
DATA: nodekey(200) VALUE 'NODE',nodetext(200) VALUE 'NODE'.
CLASS lcl_application DEFINITION DEFERRED.
DATA event_receiver TYPE REF TO lcl_application.
*---------------------------------------------------------------------*
* CLASS lcl_application DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_application DEFINITION.PUBLIC SECTION.METHODS handle_node_double_clickFOR EVENT node_double_clickOF cl_gui_simple_treeIMPORTING node_key.
ENDCLASS. "lcl_application DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_application IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.METHOD handle_node_double_click.nodekey = node_key.READ TABLE node_table WITH KEY node_key = node_keyINTO node1.nodetext = node1-text.CLEAR wa_sflight.SELECT * INTO TABLE wa_sflight FROM sflightWHERE carrid = node1-node_key+2(2)AND connid = node1-node_key+4(4).
* CREATE OBJECT WA_ALV
* EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.CALL METHOD wa_alv->set_table_for_first_displayEXPORTINGi_structure_name = 'SFLIGHT'CHANGINGit_outtab = wa_sflight.ENDMETHOD. "handle_node_double_click
ENDCLASS. "lcl_application IMPLEMENTATION
DATA g_application TYPE REF TO lcl_application.START-OF-SELECTION.SELECT * INTO TABLE wa_spfli FROM spfliORDER BY countryfr carrid ASCENDING.CREATE OBJECT g_application.SET SCREEN 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.IF wa_custom_container IS INITIAL.PERFORM create_tree.ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.save_ok = ok_code.CLEAR ok_code.CASE save_ok.WHEN 'EXIT'.LEAVE PROGRAM.ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form CREATE_TREE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_tree .DATA: events TYPE cntl_simple_events,event1 TYPE cntl_simple_event.CREATE OBJECT wa_custom_containerEXPORTINGcontainer_name = wa_container.CREATE OBJECT wa_custom_containeralvEXPORTINGcontainer_name = wa_containeralv.CREATE OBJECT wa_alvEXPORTINGi_parent = wa_custom_containeralv.CREATE OBJECT wa_treeEXPORTINGparent = wa_custom_containernode_selection_mode = cl_gui_simple_tree=>node_sel_mode_single.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.DATA: countryfr LIKE spfli-countryfr,carrid LIKE spfli-carrid.DATA: str1(255).LOOP AT wa_spfli.IF countryfr NE wa_spfli-countryfr.CLEAR node1.node1-node_key = wa_spfli-countryfr.
*第一层CLEAR node1-relatkey.CLEAR node1-relatship.
*是文件夹node1-hidden = ''.node1-disabled = ''.node1-isfolder = 'X'.
*不带ICONCLEAR node1-n_image.CLEAR node1-exp_image.CLEAR node1-expander.node1-text = wa_spfli-countryfr.APPEND node1 TO node_table.ENDIF.IF ( countryfr NE wa_spfli-countryfr ) OR( carrid <> wa_spfli-carrid ).CLEAR node1.CONCATENATE wa_spfli-countryfr wa_spfli-carridINTO node1-node_key.
*上层关系node1-relatkey = wa_spfli-countryfr.node1-relatship = cl_gui_simple_tree=>relat_last_child.node1-hidden = ''.node1-disabled = ''.node1-isfolder = 'X'.CLEAR node1-n_image.CLEAR node1-exp_image.CLEAR node1-expander.
*节点显示CARRIDnode1-text = wa_spfli-carrid.APPEND node1 TO node_table.ENDIF.countryfr = wa_spfli-countryfr.carrid = wa_spfli-carrid.CLEAR node1.CONCATENATE wa_spfli-countryfr wa_spfli-carridwa_spfli-connid INTO node1-node_key.CONCATENATE wa_spfli-countryfr wa_spfli-carridINTO str1.node1-relatkey = str1.node1-relatship = cl_gui_simple_tree=>relat_last_child.node1-hidden = ''.node1-disabled = ''.node1-isfolder = 'X'.node1-n_image = '@5B@'.node1-exp_image = '@5B@'.CLEAR node1-expander.CONCATENATE wa_spfli-carrid ':' wa_spfli-cityfromINTO str1.node1-text = str1.APPEND node1 TO node_table.ENDLOOP.CALL METHOD wa_tree->add_nodesEXPORTINGtable_structure_name = 'MTREESNODE'node_table = node_table
* EXCEPTIONS
* ERROR_IN_NODE_TABLE = 1
* FAILED = 2
* DP_ERROF = 3
* TABLE_STRUCTURE_NAME_NOT_FOUND = 4
* OTHERS = 5.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.
*定义双击事件event1-eventid = cl_gui_simple_tree=>eventid_node_double_click.event1-appl_event = 'X'.APPEND event1 TO events.CALL METHOD wa_tree->set_registered_eventsEXPORTINGevents = events.
* EXCEPTIONS
* CNTL_ERROR = 1
* CNTL_SYSTEM_ERROR = 2
* ILLEGAL_EVENT_COMBINATION = 3
* OTHERS = 4.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.
*将已定义的双击事件分配至树对象SET HANDLER g_application->handle_node_double_click FOR wa_tree.
ENDFORM. " CREATE_TREE