> ALV Tree 생성로직 <
*&---------------------------------------------------------------------*
*& Tree 관련 Object.
*&---------------------------------------------------------------------*
DATA: G_DOCKING_CONTAINER_0100 TYPE REF TO CL_GUI_DOCKING_CONTAINER,
G_TREE_0100 TYPE REF TO CL_GUI_ALV_TREE.
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
GS_FIELDCAT TYPE LVC_S_FCAT,
GS_VARIANT TYPE DISVARIANT,
GS_HIERARCHY_HEADER TYPE TREEV_HHDR,
G_TOOLBAR TYPE REF TO CL_GUI_TOOLBAR.
DATA: G_COLPOS TYPE LVC_COLPOS.
*&---------------------------------------------------------------------*
*& Tree 관련 Variables.
*&---------------------------------------------------------------------*
DATA: BEGIN OF GT_DATA OCCURS 0,
FIELD TYPE C LENGTH 10,
TEXT TYPE C LENGTH 40,
LEVEL TYPE I,
PARENT_FIELD TYPE C LENGTH 10,
WAERS LIKE BKPF-WAERS,
AMT LIKE BSEG-WRBTR,
RATE TYPE P DECIMALS 2,
NODE_KEY TYPE LVC_NKEY,
END OF GT_DATA.
DATA: BEGIN OF GS_DATA_CHAR,
AMT(30),
RATE(30),
END OF GS_DATA_CHAR.
DATA: GT_DATA_CHAR LIKE GS_DATA_CHAR OCCURS 0.
DATA: BEGIN OF GT_TOP_NODES OCCURS 0,
NODE TYPE LVC_NKEY,
END OF GT_TOP_NODES.
DATA: GV_TOP_NODE TYPE LVC_NKEY.
*&---------------------------------------------------------------------*
*& Module CREATE_OBJECT_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE CREATE_OBJECT_0100 OUTPUT.
CHECK G_TREE_0100 IS INITIAL.
*-- Container 생성.
CREATE OBJECT G_DOCKING_CONTAINER_0100
EXPORTING
DYNNR = SY-DYNNR
REPID = SY-REPID
SIDE = G_DOCKING_CONTAINER_0100->DOCK_AT_TOP
EXTENSION = 2000
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5.
*-- Tree ALV 생성.
CREATE OBJECT G_TREE_0100
EXPORTING
PARENT = G_DOCKING_CONTAINER_0100
NODE_SELECTION_MODE = CL_GUI_COLUMN_TREE=>NODE_SEL_MODE_SINGLE
ITEM_SELECTION = 'X'
NO_HTML_HEADER = 'X'
NO_TOOLBAR = SPACE.
*-- Variant 설정.
CLEAR GS_VARIANT.
GS_VARIANT-REPORT = SY-REPID.
GS_VARIANT-HANDLE = SY-DYNNR.
*-- 헤더필드설정.
CLEAR: GS_HIERARCHY_HEADER.
GS_HIERARCHY_HEADER-HEADING = '항목'. "-- 헤더필드의 헤더텍스트.
GS_HIERARCHY_HEADER-TOOLTIP = '툴팁'. "-- 헤더필드의 툴팁.
GS_HIERARCHY_HEADER-WIDTH = 40. "-- 헤더필드의 너비.
GS_HIERARCHY_HEADER-WIDTH_PIX = SPACE. "-- 헤더필드의 너비고정여부.
*-- 필드카탈로그 설정.
CLEAR: GT_FIELDCAT, GT_FIELDCAT[], GS_FIELDCAT, G_COLPOS.
G_COLPOS = G_COLPOS + 1.
GS_FIELDCAT-COL_POS = G_COLPOS.
GS_FIELDCAT-FIELDNAME = 'AMT'.
GS_FIELDCAT-COLTEXT = '금액'.
GS_FIELDCAT-TABNAME = 'GS_DATA_CHAR'.
GS_FIELDCAT-DD_OUTLEN = 20.
GS_FIELDCAT-OUTPUTLEN = 20.
GS_FIELDCAT-JUST = 'R'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
G_COLPOS = G_COLPOS + 1.
GS_FIELDCAT-COL_POS = G_COLPOS.
GS_FIELDCAT-FIELDNAME = 'RATE'.
GS_FIELDCAT-COLTEXT = '비율'.
GS_FIELDCAT-TABNAME = 'GS_DATA_CHAR'.
GS_FIELDCAT-DD_OUTLEN = 10.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-JUST = 'C'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
*-- Set Table for First Display.
CALL METHOD G_TREE_0200->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_HIERARCHY_HEADER = GS_HIERARCHY_HEADER
I_BACKGROUND_ID = 'ALV_BACKGROUND'
IS_VARIANT = GS_VARIANT
CHANGING
IT_OUTTAB = GT_DATA_CHAR "-- 이 테이블에는 값이 없어도 됨.
IT_FIELDCATALOG = GT_FIELDCAT[].
*-- 툴바읽기.
CALL METHOD G_TREE_0100->GET_TOOLBAR_OBJECT
IMPORTING
ER_TOOLBAR = G_TOOLBAR.
CHECK NOT G_TOOLBAR IS INITIAL.
*-- 툴바에서 계산버튼 삭제.
CALL METHOD G_TOOLBAR->DELETE_BUTTON
EXPORTING
FCODE = '&CALC'
EXCEPTIONS
CNTL_ERROR = 1
CNTB_ERROR_FCODE = 2
OTHERS = 3.
*-- Tree 데이터생성.
PERFORM COMPOSIT_OUTTAB_FOR_ACTION.
*-- Tree 초기상태설정.
PERFORM EXECUTE_EXPAND.
CALL METHOD G_TREE_0200->FRONTEND_UPDATE.
CALL METHOD CL_GUI_CFW=>FLUSH
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form COMPOSIT_OUTTAB_FOR_ACTION
*&---------------------------------------------------------------------*
FORM COMPOSIT_OUTTAB_FOR_ACTION .
DATA: LV_LEVEL TYPE I,
LV_PARENT TYPE LVC_NKEY,
LS_NODE_LAYOUT TYPE LVC_S_LAYN,
LT_ITEM_LAYOUT TYPE LVC_T_LAYI,
LS_ITEM_LAYOUT TYPE LVC_S_LAYI,
LV_NEW_NODE_KEY TYPE LVC_NKEY,
LV_NODE_TEXT TYPE LVC_VALUE,
LT_DATA LIKE TABLE OF GT_DATA WITH HEADER LINE.
*-- 기존 노드 Clear.
CALL METHOD G_TREE_0100->DELETE_ALL_NODES
EXCEPTIONS
FAILED = 1
CNTL_SYSTEM_ERROR = 2
OTHERS = 3.
LT_DATA[] = GT_DATA[].
LOOP AT GT_DATA.
CLEAR: LV_PARENT, LS_NODE_LAYOUT,
LT_ITEM_LAYOUT[], LT_ITEM_LAYOUT, LS_ITEM_LAYOUT.
*-- 상위항목확인.
LV_LEVEL = GT_DATA-LEVEL - 1.
READ TABLE LT_DATA WITH KEY LEVEL = LV_LEVEL
FIELD = GT_DATA-PARENT_FIELD.
IF SY-SUBRC EQ 0.
LV_PARENT = LT_DATA-NODE_KEY.
ENDIF.
*-- 데이터처리.
WRITE GT_DATA-AMT CURRENCY GT_DATA-WAERS TO GS_DATA_CHAR-AMT.
CONDENSE GS_DATA_CHAR-AMT NO-GAPS.
GS_DATA_CHAR-RATE = GT_DATA-RATE.
CONDENSE GS_DATA_CHAR-RATE NO-GAPS.
*-- 아이템 기본속성설정.
LS_ITEM_LAYOUT-FIELDNAME = G_TREE_0200->C_HIERARCHY_COLUMN_NAME.
LS_ITEM_LAYOUT-FONT = CL_GUI_COLUMN_TREE=>ITEM_FONT_PROP.
LS_ITEM_LAYOUT-STYLE = CL_GUI_COLUMN_TREE=>STYLE_DEFAULT.
LS_ITEM_LAYOUT-ALIGNMENT = CL_GUI_COLUMN_TREE=>ALIGN_RIGHT.
APPEND LS_ITEM_LAYOUT TO LT_ITEM_LAYOUT.
*-- 노드속성설정.
LV_NODE_TEXT = GT_DATA-TEXT. "-- 노드의 헤더필드값.
"-- 마지막 항목이 아니면 폴더, 마지막이면 폴더아님.
IF GT_DATA-XLAST EQ 'X'.
LS_NODE_LAYOUT-ISFOLDER = SPACE.
LS_NODE_LAYOUT-N_IMAGE = ICON_BEN_OFFER. "-- 하위항목 접었을때 아이콘.
LS_NODE_LAYOUT-EXP_IMAGE = ICON_BEN_OFFER. "-- 하위항목 펼쳤을때 아이콘.
ELSE.
LS_NODE_LAYOUT-ISFOLDER = 'X'.
LS_NODE_LAYOUT-N_IMAGE = ICON_CLOSED_FOLDER. "-- 하위항목 접었을때 아이콘.
LS_NODE_LAYOUT-EXP_IMAGE = ICON_OPEN_FOLDER. "-- 하위항목 펼쳤을때 아이콘.
ENDIF.
*-- 노드추가.
CALL METHOD G_TREE_0100->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = LV_PARENT
I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
I_NODE_TEXT = LV_NODE_TEXT
IS_OUTTAB_LINE = GS_DATA_CHAR
IS_NODE_LAYOUT = LS_NODE_LAYOUT
IT_ITEM_LAYOUT = LT_ITEM_LAYOUT
IMPORTING
E_NEW_NODE_KEY = LV_NEW_NODE_KEY.
IF GT_DATA-LEVEL = 1.
GV_TOP_NODE = LV_NEW_NODE_KEY.
ENDIF.
*-- 생성된 노드키를 Internal Table에 반영.
GT_DATA-NODE_KEY = LV_NEW_NODE_KEY.
MODIFY GT_DATA TRANSPORTING NODE_KEY WHERE FIELD = GT_DATA-FIELD.
LT_DATA-NODE_KEY = LV_NEW_NODE_KEY.
MODIFY LT_DATA TRANSPORTING NODE_KEY WHERE FIELD = GT_DATA-FIELD.
*-- Top Node 리스트 저장.
IF GT_DATA-LEVEL EQ 1 AND GT_DATA-XLAST EQ SPACE.
GT_TOP_NODES-NODE = LV_NEW_NODE_KEY.
APPEND GT_TOP_NODES.
ENDIF.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form EXECUTE_EXPAND
*&---------------------------------------------------------------------*
FORM EXECUTE_EXPAND .
CHECK NOT GT_TOP_NODES[] IS INITIAL.
LOOP AT GT_TOP_NODES.
CALL METHOD G_TREE_0200->EXPAND_NODE
EXPORTING
I_NODE_KEY = GT_TOP_NODES-NODE
I_LEVEL_COUNT = 1
I_EXPAND_SUBTREE = SPACE
EXCEPTIONS
FAILED = 1
ILLEGAL_LEVEL_COUNT = 2
CNTL_SYSTEM_ERROR = 3
NODE_NOT_FOUND = 4
CANNOT_EXPAND_LEAF = 5
OTHERS = 6.
ENDLOOP.
READ TABLE GT_TOP_NODES INDEX 1.
CALL METHOD G_TREE_0200->SET_TOP_NODE
EXPORTING
I_NODE_KEY = GT_TOP_NODES-NODE
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
NODE_NOT_FOUND = 2
FAILED = 3
OTHERS = 4.
ENDFORM.
끝.