메뉴 건너뛰기

SAP 한국 커뮤니티



Tree ALV 샘플

양키(이경환) 2014.02.05 18:22 조회 수 : 10671 추천:1

 
  DATAlt_sflight TYPE STANDARD TABLE OF qmsm,
         ls_sflight LIKE LINE OF lt_sflight,
         l_yyyymm TYPE qmsm-qmnum,            "year and month of sflight-fldate
         l_yyyymm_last TYPE qmsm-qmnum,
         l_carrid LIKE qmsm-qmnum,
         l_carrid_last LIKE qmsm-qmnum.


* §1a. Define reference variables
 DATAg_alv_tree         TYPE REF TO cl_gui_alv_tree,
       g_custom_container TYPE REF TO cl_gui_custom_container.

DATAgt_sflight      TYPE qmsm OCCURS 0,      "Output-Table
       ok_code LIKE sy-ucomm,
       save_ok LIKE sy-ucomm,           "OK-Code
       g_max TYPE VALUE 255.

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.
* §6. Call dispatch to process toolbar functions
       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.
* §1b. Create ALV Tree Control and corresponding Container.

* create container for alv-tree
   DATAl_tree_container_name(30TYPE 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(00WITH '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(00WITH 'ERROR'.                          "#EC NOTEXT
   ENDIF.

* §2. Create Hierarchy-header
* The simple ALV Tree uses the text of the fields which were used
* for sorting to define this header. When you use
* the 'normal' ALV Tree the hierarchy is build up freely
* by the programmer this is not possible, so he has to define it
* himself.
   DATA l_hierarchy_header TYPE treev_hhdr.
   PERFORM build_hierarchy_header CHANGING l_hierarchy_header.

* §3. Create empty Tree Control
* IMPORTANT: Table 'gt_sflight' 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.
   CALL METHOD g_alv_tree->set_table_for_first_display
     EXPORTING
       i_structure_name    'QMSM'
       is_hierarchy_header l_hierarchy_header
     CHANGING
       it_outtab           gt_sflight"table must be empty !

* §4. Create hierarchy (nodes and leaves)
   PERFORM create_hierarchy.

* §5. Send data to frontend.
   CALL METHOD g_alv_tree->frontend_update.

* wait for automatic flush at end of pbo
 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 'Notification'(300).
   p_hierarchy_header-tooltip 'number'(400).
   p_hierarchy_header-width 40.
   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.



  DATAl_month_key TYPE lvc_nkey,
         l_monthkey TYPE lvc_nkey,
         l_carrid_key TYPE lvc_nkey,
         l_carridkey TYPE lvc_nkey,
         l_last_key TYPE lvc_nkey,
         l_lastkey TYPE lvc_nkey.

  DATAlt_qmma TYPE STANDARD TABLE OF qmma ,
         ls_qmma LIKE LINE OF lt_qmma.
* §4a. Select data
   SELECT INTO CORRESPONDING FIELDS OF TABLE lt_sflight
     FROM qmsm."WHERE qmnum = '000200000006 '. "UP TO g_max ROWS.

*  SORT lt_sflight BY qmnum .
* 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 lt_sflight INTO ls_sflight.
* 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.
     l_yyyymm ls_sflight-qmnum.
     l_carrid ls_sflight-qmnum."ls_sflight-mncod.

* Top level nodes:
     IF l_yyyymm <> l_yyyymm_last.      "on change of l_yyyymm
       l_yyyymm_last l_yyyymm.

*Providing no key means that the node is added on top level:
       PERFORM add_month USING l_yyyymm ls_sflight space
                      CHANGING l_month_key.
* The month changed, thus, there is no predecessor carrier
       CLEAR l_carrid_last.
     ENDIF.

* Carrier nodes:
* (always inserted as child of the last month
*  which is identified by 'l_month_key')
     IF l_carrid <> l_carrid_last.      "on change of l_carrid
       l_carrid_last l_carrid.
*Folder Task
       PERFORM add_carrid_line USING    ls_sflight
                                        l_month_key
                               CHANGING l_carrid_key.
*Folder Activity
       l_carrid_last l_carrid.
       PERFORM add_carridline USING     ls_sflight
                                        l_month_key
                               CHANGING l_carridkey.
     ENDIF.

** Leaf:
** (always inserted as child of the last carrier
**  which is identified by 'l_carrid_key')
*Folder Task Item
     PERFORM add_complete_line USING  ls_sflight
                                      l_carrid_key
                             CHANGING l_last_key.
*Folder Activity Item
     ON CHANGE OF ls_sflight-qmnum.
       SELECT INTO CORRESPONDING FIELDS OF TABLE lt_qmma FROM qmma WHERE qmnum ls_sflight-qmnum.
       LOOP AT lt_qmma INTO ls_qmma.
         PERFORM add_completeline USING  ls_qmma
                                         l_carridkey
                             CHANGING    l_lastkey.
       ENDLOOP.
     ENDON.
   ENDLOOP.

ENDFORM.                               " create_hierarchy

*&---------------------------------------------------------------------*
*&      Form  add_month
*&---------------------------------------------------------------------*
 FORM add_month  USING     p_yyyymm TYPE qmnum
                           p_flight  TYPE qmsm
                           p_relat_key TYPE lvc_nkey
                 CHANGING  p_node_key TYPE lvc_nkey.



  DATAl_node_text TYPE lvc_value,
         ls_sflight TYPE qmsm,
         l_month(15TYPE c.            "output string for month

* get month name for node text
   PERFORM get_month USING p_yyyymm
                     CHANGING l_month.
   CONCATENATE 'Notification' '->'p_yyyymm INTO l_node_text .

* add node:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
   CALL METHOD g_alv_tree->add_node
     EXPORTING
       i_relat_node_key p_relat_key
       i_relationship   cl_gui_column_tree=>relat_last_child
       i_node_text      l_node_text
       is_outtab_line   =  p_flight
     IMPORTING
       e_new_node_key   p_node_key.

ENDFORM.                               " add_month
*--------------------------------------------------------------------
 FORM add_carrid_line USING     ps_sflight TYPE qmsm
                                p_relat_key TYPE lvc_nkey
                      CHANGING  p_node_key TYPE lvc_nkey.

  DATAl_node_text TYPE lvc_value,
         ls_sflight TYPE qmsm.

* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
*  l_node_text =  ps_sflight-mncod.
   CONCATENATE 'Task' '' INTO l_node_text.
   CALL METHOD g_alv_tree->add_node
     EXPORTING
       i_relat_node_key p_relat_key
       i_relationship   cl_gui_column_tree=>relat_last_child
       i_node_text      l_node_text
       is_outtab_line   ls_sflight
     IMPORTING
       e_new_node_key   p_node_key.

ENDFORM.                               " add_carrid_line
*&---------------------------------------------------------------------*
*&      Form  add_carridline
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_SFLIGHT   text
*      -->P_RELAT_KEY  text
*      -->P_NODE_KEY   text
*----------------------------------------------------------------------*
 FORM add_carridline USING      ps_sflight TYPE qmsm
                                p_relat_key TYPE lvc_nkey
                      CHANGING  p_node_key TYPE lvc_nkey.

  DATAl_node_text TYPE lvc_value,
         ls_sflight TYPE qmsm.

* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
*  l_node_text =  ps_sflight-mncod.
   CONCATENATE 'Activity' '' INTO l_node_text.
   CALL METHOD g_alv_tree->add_node
     EXPORTING
       i_relat_node_key p_relat_key
       i_relationship   cl_gui_column_tree=>relat_last_child
       i_node_text      l_node_text
       is_outtab_line   ls_sflight
     IMPORTING
       e_new_node_key   p_node_key.

ENDFORM.                               " add_carrid_line
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
 FORM add_completeline USING   ps_sflight TYPE qmma
                                p_relat_key TYPE lvc_nkey
                      CHANGING  p_node_key TYPE lvc_nkey.

  DATAl_node_text TYPE lvc_value.

  WRITE ps_sflight-manum TO l_node_text MM/DD/YYYY.

* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
   CALL METHOD g_alv_tree->add_node
     EXPORTING
       i_relat_node_key p_relat_key
       i_relationship   cl_gui_column_tree=>relat_last_child
       is_outtab_line   ps_sflight
       i_node_text      l_node_text
     IMPORTING
       e_new_node_key   p_node_key.

ENDFORM.                               " add_complete_line
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_SFLIGHT   text
*      -->P_RELAT_KEY  text
*      -->P_NODE_KEY   text
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  GET_MONTH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_YYYYMM  text
*      <--P_L_MONTH  text
*----------------------------------------------------------------------*
 FORM get_month USING    p_yyyymm
                CHANGING p_month.
* Returns the name of month according to the digits in p_yyyymm

  DATAl_monthdigits(2TYPE c.

  l_monthdigits p_yyyymm+4(2).
   CASE l_monthdigits.
     WHEN '01'.
       p_month 'January'(701).
     WHEN '02'.
       p_month 'February'(702).
     WHEN '03'.
       p_month 'March'(703).
     WHEN '04'.
       p_month 'April'(704).
     WHEN '05'.
       p_month 'May'(705).
     WHEN '06'.
       p_month 'June'(706).
     WHEN '07'.
       p_month 'July'(707).
     WHEN '08'.
       p_month 'August'(708).
     WHEN '09'.
       p_month 'September'(709).
     WHEN '10'.
       p_month 'October'(710).
     WHEN '11'.
       p_month 'November'(711).
     WHEN '12'.
       p_month 'December'(712).
   ENDCASE.
   CONCATENATE p_yyyymm+0(4'->' p_month INTO p_month.

ENDFORM.                               " GET_MONTH
*-----------------------------------------------------------------------
*&---------------------------------------------------------------------*
*&      Form  ADD_COMPLETE_LINE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LS_SFLIGHT  text
*      -->P_L_CARRID_KEY  text
*      <--P_L_LAST_KEY  text
*----------------------------------------------------------------------*
 FORM add_complete_line USING   ps_sflight TYPE qmsm
                                p_relat_key TYPE lvc_nkey
                      CHANGING  p_node_key TYPE lvc_nkey.

  DATAl_node_text TYPE lvc_value.

  WRITE ps_sflight-mncod TO l_node_text MM/DD/YYYY.

* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
   CALL METHOD g_alv_tree->add_node
     EXPORTING
       i_relat_node_key p_relat_key
       i_relationship   cl_gui_column_tree=>relat_last_child
       is_outtab_line   ps_sflight
       i_node_text      l_node_text
     IMPORTING
       e_new_node_key   p_node_key.


ENDFORM.                    " ADD_COMPLETE_LINE
번호 제목 글쓴이 날짜 조회 수
107 KR_POSTCODE : 한국주소입력 Version 2.0 대략나 2021.03.31 2201
106 HTTP 통신하는 로직 [3] 대략나 2016.12.20 5914
105 XML 을 아밥 스트럭쳐 또는 인터널테이블로 변경시키는 로직 [1] 대략나 2016.12.20 3918
104 ALV EDIT 샘플 프로그램 sapjoy 2015.06.26 9323
103 파일 메일(e-mail) 첨부로 보내기 [2] sapjoy 2014.10.10 7368
102 데이터 이메일(e-mail) xls 파일로 보내기 [2] sapjoy 2014.10.10 6730
» Tree ALV 샘플 [5] 양키(이경환) 2014.02.05 10671
100 Split 의 사용 [1] 양키(이경환) 2014.02.05 11236
99 소수점 뒷자리 0 제거 로직 file 양키(이경환) 2014.01.20 9618
98 10이하의 홀수 마방진 은미짱 2013.10.19 6830
97 [Module pool] Container에 webpage 넣기. [2] 냥냥 2013.03.20 11238
96 COMAPY CODE에 할당된 Fiscal variant에 대한 calendar date 변환 paran 2013.03.01 10505
95 멀 어떻게 해야 하는건질 몰라 질문 드립니다 [5] 쏠라맨 2012.10.13 11704
94 nugg 프로그램을 sap 에 생성하기.. [3] file MadMax 2012.10.11 10891
93 인터널 테이블을 사용한 구구단 출력 두가지 입니다 - WRITE, ALV 이용 [1] kofnhuge 2012.10.10 14127
92 Search For Program/Function/Table Desc&Name [8] file 초짜 2011.09.30 9863
91 스크린에서 버튼을 tree 처럼 만들기. [32] file Jenny 2011.08.12 17707
90 통화단위, krw, usd 일반 필드에 변환시 로직(소수점, 자리수) [4] sapjoy 2011.07.20 15638
89 Dynamic Select 소스 [5] 떡밥 2011.06.04 17828
88 [onepaper] 참고자료 Archiving 세팅에 필요한 파일소스입니다. [7] file 원니컴 2011.03.22 13831