ALV Tree demo(WBS元素分层显示)[引用别人的]详解编程语言

按层次显示WBS编码及描述,附加节点双击展开事件和Item双击跳转CJ03功能。 
ALV Tree demo(WBS元素分层显示)[引用别人的]详解编程语言
 
  
因为本人懒,本例代码都是从标准的BCALV_TREE_01和BCALV_TREE_02拷贝过来修改的,结构不好请别见怪。 
REPORT  zmac_alvtree_test. 
DATA: g_alv_tree         TYPE REF TO cl_gui_alv_tree, 
      g_custom_container TYPE REF TO cl_gui_custom_container. 
TYPES: BEGIN OF ty_wbs, 
        posid  TYPE prps-posid, 
        post1  TYPE prps-post1, 
        stufe  TYPE prps-stufe, 
       END OF ty_wbs. 
 
DATA: gt_wbs TYPE STANDARD TABLE OF ty_wbs, 
      gs_wbs TYPE ty_wbs. 
 
DATA: ok_code LIKE sy-ucomm, 
      save_ok LIKE sy-ucomm,           "OK-Code 
      g_max TYPE i VALUE 255. 
 
DATA: gt_fcat  TYPE  lvc_t_fcat, 
      gs_fcat  TYPE  lvc_s_fcat. 
 
*----------------------------------------------------------------------* 
*       CLASS lcl_tree_event_receiver DEFINITION 
*----------------------------------------------------------------------* 
* 
*----------------------------------------------------------------------* 
CLASS lcl_tree_event_receiver DEFINITION. 
 
  PUBLIC SECTION. 
    METHODS handle_node_double_click 
      FOR EVENT node_double_click OF cl_gui_alv_tree 
      IMPORTING node_key sender. 
    METHODS handle_item_double_click 
      FOR EVENT item_double_click OF cl_gui_alv_tree 
      IMPORTING fieldname node_key. 
ENDCLASS.                    "lcl_tree_event_receiver DEFINITION 
****************************************************************** 
CLASS lcl_tree_event_receiver IMPLEMENTATION. 
 
  METHOD handle_node_double_click. 
    DATA: lt_children TYPE lvc_t_nkey. 
 
    CALL METHOD sender->get_children 
      EXPORTING 
        i_node_key  = node_key 
      IMPORTING 
        et_children = lt_children. 
 
    IF NOT lt_children IS INITIAL. 
 
      CALL METHOD sender->expand_node 
        EXPORTING 
          i_node_key    = node_key 
          i_level_count = 2. 
    ENDIF. 
 
  ENDMETHOD.                    "handle_node_double_click 
 
  METHOD handle_item_double_click. 
    DATA: l_value TYPE lvc_value, 
          l_layi  TYPE lvc_t_layi, 
          l_layn  TYPE lvc_s_layn . 
 
    CALL METHOD g_alv_tree->get_outtab_line 
      EXPORTING 
        i_node_key     = node_key 
      IMPORTING 
        e_outtab_line  = gs_wbs 
        e_node_text    = l_value 
        et_item_layout = l_layi 
        es_node_layout = l_layn 
      EXCEPTIONS 
        node_not_found = 1 
        OTHERS         = 2. 
    IF sy-subrc = 0. 
      SET PARAMETER ID 'PRO' FIELD gs_wbs-posid. 
      CALL TRANSACTION 'CJ03' AND SKIP FIRST SCREEN. 
    ENDIF. 
 
  ENDMETHOD.                    "handle_item_double_click 
 
ENDCLASS.                    "lcl_tree_event_receiver IMPLEMENTATION 
 
END-OF-SELECTION. 
 
  CALL SCREEN 100. 
 
*&---------------------------------------------------------------------* 
*&      Module  PBO  OUTPUT 
*&---------------------------------------------------------------------* 
*       process before output 
*----------------------------------------------------------------------* 
MODULE pbo OUTPUT. 
 
  SET PF-STATUS 'MAIN100'. 
  SET TITLEBAR 'MAINTITLE'. 
 
  IF g_alv_tree IS INITIAL. 
    PERFORM init_tree. 
 
    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. 
  ENDIF. 
 
ENDMODULE.                             " PBO  OUTPUT 
*&---------------------------------------------------------------------* 
*&      Module  PAI  INPUT 
*&---------------------------------------------------------------------* 
*       process after input 
*----------------------------------------------------------------------* 
MODULE pai INPUT. 
  save_ok = ok_code. 
  CLEAR ok_code. 
 
  CASE save_ok. 
    WHEN 'EXIT' OR 'BACK' OR 'CANC'. 
      PERFORM exit_program. 
 
    WHEN OTHERS. 
      CALL METHOD cl_gui_cfw=>dispatch. 
 
  ENDCASE. 
 
  CALL METHOD cl_gui_cfw=>flush. 
ENDMODULE.                             " PAI  INPUT 
 
*&---------------------------------------------------------------------* 
*&      Form  init_tree 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
FORM init_tree. 
 
  DATA: l_tree_container_name(30) TYPE c. 
 
  l_tree_container_name = 'CCONTAINER1'. 
 
  CREATE OBJECT g_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'(100). 
  ENDIF. 
 
* create tree control 
  CREATE OBJECT g_alv_tree 
    EXPORTING 
      parent                      = g_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. 
  IF sy-subrc <> 0. 
    MESSAGE x208(00) WITH 'ERROR'.                          "#EC NOTEXT 
  ENDIF. 
 
  DATA l_hierarchy_header TYPE treev_hhdr. 
  PERFORM build_hierarchy_header CHANGING l_hierarchy_header. 
  PERFORM build_fieldcategory. 
 
  CALL METHOD g_alv_tree->set_table_for_first_display 
    EXPORTING 
      is_hierarchy_header = l_hierarchy_header 
    CHANGING 
      it_outtab           = gt_wbs   "table must be empty ! 
      it_fieldcatalog     = gt_fcat. 
 
  PERFORM create_hierarchy. 
  PERFORM register_events. 
 
  CALL METHOD g_alv_tree->frontend_update. 
 
ENDFORM.                               " init_tree 
*&---------------------------------------------------------------------* 
*&      Form  build_hierarchy_header 
*&---------------------------------------------------------------------* 
*       build hierarchy-header-information 
*----------------------------------------------------------------------* 
*      -->P_L_HIERARCHY_HEADER  strucxture for hierarchy-header 
*----------------------------------------------------------------------* 
FORM build_hierarchy_header CHANGING 
                               p_hierarchy_header TYPE treev_hhdr. 
 
  p_hierarchy_header-heading = 'Wbs number'(300). 
  p_hierarchy_header-tooltip = 'WBS'(400). 
  p_hierarchy_header-width = 50. 
  p_hierarchy_header-width_pix = ' '. 
 
ENDFORM.                               " build_hierarchy_header 
*&---------------------------------------------------------------------* 
*&      Form  exit_program 
*&---------------------------------------------------------------------* 
*       free object and leave program 
*----------------------------------------------------------------------* 
FORM exit_program. 
 
  CALL METHOD g_custom_container->free. 
  LEAVE PROGRAM. 
 
ENDFORM.                               " exit_program 
*&---------------------------------------------------------------------* 
*&      Form  create_hierarchy 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
FORM create_hierarchy. 
 
  DATA: ls_wbs TYPE ty_wbs, 
        lt_wbs TYPE ty_wbs OCCURS 0. 
 
  DATA: lt_nodes TYPE TABLE OF lvc_nkey WITH HEADER LINE, 
        l_cur_node TYPE lvc_nkey, 
        l_former_stufe TYPE prps-stufe, 
        l_index  TYPE i. 
 
  SELECT posid post1 stufe FROM prps INTO TABLE lt_wbs UP TO 200 ROWS. 
 
  SORT lt_wbs BY posid. 
 
  LOOP AT lt_wbs INTO ls_wbs. 
 
    IF ls_wbs-stufe = '1'. 
      REFRESH lt_nodes. 
      l_index = ls_wbs-stufe. 
      PERFORM add_nodes USING    ls_wbs 
                                 '' 
                        CHANGING l_cur_node. 
      READ TABLE lt_nodes INDEX l_index. 
      IF sy-subrc NE 0. 
        APPEND l_cur_node TO lt_nodes. 
      ENDIF. 
 
    ELSEIF ls_wbs-stufe > l_former_stufe.      "on change of l_yyyymm 
      l_index = ls_wbs-stufe - 1. 
      READ TABLE lt_nodes INDEX l_index. 
 
      PERFORM add_nodes USING    ls_wbs 
                                 lt_nodes 
                        CHANGING l_cur_node. 
*---->保存新的当前节点 
      APPEND l_cur_node TO lt_nodes. 
    ELSEIF ls_wbs-stufe < l_former_stufe. 
      l_index = ls_wbs-stufe - 1. 
      READ TABLE lt_nodes INDEX l_index. 
 
      PERFORM add_nodes USING    ls_wbs 
                                 lt_nodes 
                        CHANGING l_cur_node. 
*---->将当前节点及之后后的所有临时存储节点KEY删除 
      l_index = l_index + 1. 
      DELETE lt_nodes INDEX l_index. 
      DO. 
        READ TABLE lt_nodes INDEX l_index. 
        IF sy-subrc = 0. 
          DELETE lt_nodes INDEX l_index. 
        ELSE. 
          EXIT. 
        ENDIF. 
      ENDDO. 
*---->保存新的当前节点 
      APPEND l_cur_node TO lt_nodes. 
    ELSE. 
      l_index = ls_wbs-stufe - 1. 
      READ TABLE lt_nodes INDEX l_index. 
      PERFORM add_nodes USING    ls_wbs 
                                 lt_nodes 
                        CHANGING l_cur_node. 
    ENDIF. 
    l_former_stufe = ls_wbs-stufe. 
  ENDLOOP. 
 
ENDFORM.                               " create_hierarchy 
*&---------------------------------------------------------------------* 
*&      Form  add_nodes 
*&---------------------------------------------------------------------* 
FORM add_nodes  USING     ps_wbs TYPE ty_wbs 
                          p_cnode_key TYPE lvc_nkey 
                CHANGING  p_nnode_key TYPE lvc_nkey. 
 
  DATA: l_node_text TYPE lvc_value. 
 
  CALL FUNCTION 'CONVERSION_EXIT_ABPSN_OUTPUT' 
    EXPORTING 
      input  = ps_wbs-posid 
    IMPORTING 
      output = l_node_text. 
 
  CALL METHOD g_alv_tree->add_node 
    EXPORTING 
      i_relat_node_key = p_cnode_key 
      i_relationship   = cl_gui_column_tree=>relat_last_child 
      i_node_text      = l_node_text 
      is_outtab_line   = ps_wbs 
    IMPORTING 
      e_new_node_key   = p_nnode_key. 
 
ENDFORM.                               " add_month 
*&---------------------------------------------------------------------* 
*&      Form  build_fieldcategory 
*&---------------------------------------------------------------------* 
FORM build_fieldcategory . 
*  gs_fcat-fieldname      =  'POSID'. 
*  gs_fcat-seltext        =  text-f06.    " 字段输出名称 
*  gs_fcat-coltext        =  text-f06.    " 字段输出名称 
*  gs_fcat-just           =  'L'. 
*  gs_fcat-outputlen      =  '40'.        " 输出长度 
*  APPEND gs_fcat TO gt_fcat. 
*  CLEAR  gs_fcat. 
  gs_fcat-fieldname      =  'POST1'. 
  gs_fcat-seltext        =  text-f01.    " 字段输出名称 
  gs_fcat-coltext        =  text-f01.    " 字段输出名称 
  gs_fcat-just           =  'L'. 
  gs_fcat-outputlen      =  '40'.        " 输出长度 
  APPEND gs_fcat TO gt_fcat. 
  CLEAR  gs_fcat. 
ENDFORM.                    " build_fieldcategory 
*&---------------------------------------------------------------------* 
*&      Form  register_events 
*&---------------------------------------------------------------------* 
FORM register_events . 
  DATA: lt_events TYPE cntl_simple_events, 
        l_event TYPE cntl_simple_event, 
        l_event_receiver TYPE REF TO lcl_tree_event_receiver. 
 
  CALL METHOD g_alv_tree->get_registered_events 
    IMPORTING 
      events = lt_events. 
 
  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_item_double_click. 
  APPEND l_event TO lt_events. 
 
  CALL METHOD g_alv_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. 
 
  CREATE OBJECT l_event_receiver. 
  SET HANDLER l_event_receiver->handle_node_double_click FOR g_alv_tree. 
  SET HANDLER l_event_receiver->handle_item_double_click FOR g_alv_tree. 
 
ENDFORM.                    " register_events

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/18677.html

(0)
上一篇 2021年7月19日 21:21
下一篇 2021年7月19日 21:21

相关推荐

发表回复

登录后才能评论