ABAP—技術驗證平臺(螢幕類01)
ABAP—技術驗證平臺(螢幕類01)
前言:
該程式程式碼較為健碩,功能區域劃分清晰,可根據需要新增自己需要的事件用於技術驗證(取數邏輯請忽略)。
涵蓋功能:
1、Docking
2、ALV_GRID
3、ALV_TREE
4、Drag and Drop
5、圖片顯示
6、程式中包含一個以縱深結構作為靜態屬性的類
程式碼:
*&---------------------------------------------------------------------*
*& Report ZFIR090T
*&---------------------------------------------------------------------*
*& Author : NathanSun
*& Create Date : 2018-08-31
*& Program Type : Report
*& Description : ***********
*&---------------------------------------------------------------------*
REPORT YN_FIR090T.
*&---------------------------------------------------------------------*
*& 包含 ZFIR090_TOP
*&---------------------------------------------------------------------*
TABLES:VBAK,VBAP,ACDOCA.
TYPE-POOLS:SLIS ,ICON .
"OOALV-DEFINITION
DATA GO_ALV_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA GO_ALV_GRID_SUB TYPE REF TO CL_GUI_ALV_GRID.
DATA LO_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA OK_CODE TYPE SY-UCOMM.
DATA OK_SAVE TYPE SY-UCOMM.
* global fields
DATA: DRAGDROP_TREE TYPE REF TO CL_DRAGDROP,
DRAGDROP_ALV TYPE REF TO CL_DRAGDROP,
FLG_NEW,
GRID TYPE REF TO CL_GUI_ALV_GRID,
GS_LAYOUT_ALV TYPE LVC_S_LAYO,
GS_TOOLBAR TYPE STB_BUTTON,
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
G_CONTAINER_OBJECT TYPE REF TO CL_GUI_DOCKING_CONTAINER,
G_DROPEFFECT TYPE I,
G_HANDLE_TREE TYPE I,
G_HANDLE_ALV TYPE I,
G_HIERARCHY_HEADER TYPE TREEV_HHDR,
G_REPID LIKE SY-REPID,
CONTROLS_CREATED1,
TREE1 TYPE REF TO CL_GUI_ALV_TREE,
IT_NODE_KEY TYPE LVC_T_NKEY .
TYPES pic_line(1022) TYPE x.
DATA pic_tab TYPE TABLE OF pic_line.
DATA l_alignment TYPE i.
FIELD-SYMBOLS:<F1>.
"""" 如下引數定義為操作EXCEL所用
INCLUDE: OLE2INCL,
ZFIR090_EXCEL.
DATA: I_EXCEL_CONTROL TYPE REF TO EXCEL_CONTROL,
I_EXCEL_DESTINATION TYPE REF TO EXCEL_DESTINATION.
DATA: I_ROWS_TABLE TYPE ROWS_TAB.
DATA: BEGIN OF EXCEL_COLUMN OCCURS 0,
S_INDEX LIKE SY-TABIX,
S_ID TYPE C,
END OF EXCEL_COLUMN.
TYPES: BEGIN OF GTS_DATA,
KUNNR TYPE VBAK-KUNNR, "客戶;
NAME1 TYPE KNA1-NAME1, "客戶名稱
MATNR TYPE ACDOCA-MATNR, "物料編碼
ZUONR TYPE ACDOCA-ZUONR, "DN編號
ZUONR_F TYPE CHAR10, "DN編號前十位
ZUONR_B TYPE CHAR6, "DN編號後六位
WADAT_IST TYPE LIKP-WADAT_IST, "DN時間
MSL TYPE ACDOCA-MSL, "銷售數量
WERKS TYPE ACDOCA-WERKS, "工廠
RUNIT TYPE ACDOCA-RUNIT, "單位
HSL TYPE ACDOCA-HSL, "發出商品金額
KDAUF TYPE ZDN_CREATE_HIS-KDAUF, "銷售訂單
KDPOS TYPE ZDN_CREATE_HIS-KDPOS, "銷售訂單專案
NETPR TYPE VBAP-NETPR, "淨價
KPEIN TYPE VBAP-KPEIN, "定價單位
UMZIN TYPE VBAP-UMZIN, "轉換因子
UMZIZ TYPE VBAP-UMZIZ, "轉換因子
NETSR TYPE VBAP-NETPR, "銷售收入
WAERK TYPE VBAP-WAERK, "憑證貨幣
KNUMV TYPE EKKO-KNUMV, "憑證條件號
EBELN TYPE EKPO-EBELN, "採購憑證
EBELP TYPE EKPO-EBELP, "行專案
UMREN TYPE EKPO-UMREN, "分母
UMREZ TYPE EKPO-UMREZ, "分子
S_NETPR TYPE VBAP-NETPR, "訂單本幣淨價
EXCH_RATE TYPE BAPI1093_0, "匯率
RETURN TYPE BAPIRET1, "報錯
VKORG TYPE TVKO-VKORG, "銷售組織
VTWEG TYPE MVKE-VTWEG, "分銷渠道
PRODH TYPE MVKE-PRODH, "產品層次
VTEXT1 TYPE T179T-VTEXT, "大類
VTEXT2 TYPE T179T-VTEXT, "中類
VTEXT3 TYPE T179T-VTEXT, "小類
ZXSML TYPE VBAP-NETPR, "毛利
ZHSLPS TYPE ACDOCA-HSL, "毛利率
ZXSMPS TYPE CHAR20, "毛利率
P_NODE TYPE LVC_NKEY , "層級
END OF GTS_DATA.
DATA: GT_DATA TYPE TABLE OF GTS_DATA WITH HEADER LINE.
DATA: GT_DATA_C TYPE TABLE OF GTS_DATA WITH HEADER LINE.
DATA: GT_DATA_D TYPE TABLE OF GTS_DATA WITH HEADER LINE.
DATA: GT_DATA_E TYPE TABLE OF GTS_DATA WITH HEADER LINE.
DATA: GT_DATA_F TYPE TABLE OF GTS_DATA WITH HEADER LINE.
CONSTANTS:P_RLDNR TYPE ACDOCA-RLDNR VALUE '0L'.
CONSTANTS:P_RRCTY TYPE ACDOCA-RRCTY VALUE 0 .
CONSTANTS:P_RACCT TYPE ACDOCA-RACCT VALUE '1406010100'.
CONSTANTS:P_AWTYP TYPE ACDOCA-AWTYP VALUE 'BKPFF'.
CONSTANTS:SELECTED VALUE 'X'.
DATA: CONTROLS_CREATED.
DATA: LV_WAERS TYPE T001-WAERS .
DATA OUT_01 TYPE CHAR30. "法人公司
DATA OUT_02 TYPE CHAR30. "截止至日期
DATA OUT_03 TYPE CHAR30. "編制人
DATA OUT_04 TYPE CHAR30. "幣別
DATA OUT_05 TYPE CHAR16 VALUE '0'. "銷售收入-期末金額
DATA OUT_06 TYPE CHAR16 VALUE '0'. "銷售收入-1月內
DATA OUT_07 TYPE CHAR16 VALUE '0'. "銷售收入-1~2月
DATA OUT_08 TYPE CHAR16 VALUE '0'. "銷售收入-2~3月
DATA OUT_09 TYPE CHAR16 VALUE '0'. "銷售收入-3~6月
DATA OUT_10 TYPE CHAR16 VALUE '0'. "銷售收入-超過6個月
DATA OUT_11 TYPE CHAR16 VALUE '0'. "發出商品金額-期末金額
DATA OUT_12 TYPE CHAR16 VALUE '0'. "發出商品金額-1月內
DATA OUT_13 TYPE CHAR16 VALUE '0'. "發出商品金額-1~2月
DATA OUT_14 TYPE CHAR16 VALUE '0'. "發出商品金額-2~3月
DATA OUT_15 TYPE CHAR16 VALUE '0'. "發出商品金額-3~6月
DATA OUT_16 TYPE CHAR16 VALUE '0'. "發出商品金額-超過6個月
DATA OUT_17 TYPE CHAR16 VALUE '0'. "銷售數量-合計
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS: P_RBUKRS LIKE ACDOCA-RBUKRS MODIF ID M1 OBLIGATORY . "公司程式碼
PARAMETERS: P_GJAHR LIKE ACDOCA-GJAHR MODIF ID M1 OBLIGATORY . "年度
PARAMETERS: P_BUDAT LIKE ACDOCA-BUDAT MODIF ID M1 OBLIGATORY . "截止日期
SELECTION-SCREEN END OF BLOCK B1.
*
*SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
*PARAMETERS: p_chong AS CHECKBOX USER-COMMAND mx MODIF ID mb.
*SELECTION-SCREEN END OF BLOCK b3.
*&---------------------------------------------------------------------*
*& 包含 ZFIR090_CLASS
*&---------------------------------------------------------------------*
CLASS DEMO DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
MAIN,
CLASS_CONSTRUCTOR.
PRIVATE SECTION.
CLASS-DATA:
BEGIN OF STRUCT1,
A1 TYPE STRING VALUE `a1_XX`,
A2 TYPE STRING VALUE `a2_XX`,
BEGIN OF ISTRUCT,
A1 TYPE STRING VALUE `a1_YY`,
A2 TYPE STRING VALUE `a2_YY`,
END OF ISTRUCT,
ITAB LIKE STANDARD TABLE OF STRUCT1-ISTRUCT WITH EMPTY KEY,
END OF STRUCT1.
ENDCLASS.
CLASS DEMO IMPLEMENTATION.
METHOD MAIN.
DATA(OUT) = CL_DEMO_OUTPUT=>NEW( ).
OUT->BEGIN_SECTION( `縱深結構的內表展示`
)->WRITE( STRUCT1-ISTRUCT
)->WRITE( STRUCT1-ITAB ).
OUT->DISPLAY( ).
ENDMETHOD.
METHOD CLASS_CONSTRUCTOR.
STRUCT1-ITAB = VALUE #(
( A1 = `a1_xx` A2 = `a2_xx` )
( A1 = `a1_yy` A2 = `a2_yy` ) ).
ENDMETHOD.
ENDCLASS.
*----------------------------------------------------------------------*
* CLASS cl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS CL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
" 宣告單擊事件的方法
METHODS HANDLE_HOTSPOT_CLICK
FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW_ID E_COLUMN_ID.
" 宣告雙擊事件方法
METHODS HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN.
" 宣告TOOLBAR事件方法
METHODS HANDLE_TOOLBAR
FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE.
" 宣告USER-COMMAND 事件方法
METHODS HANDLE_COMMAND
FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
"修改時觸發
METHODS HANDLE_DATA_CHANGED
FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
"Drag
METHODS HANDLE_ONDRAG
FOR EVENT ONDRAG OF CL_GUI_ALV_GRID
IMPORTING E_ROW
E_COLUMN
ES_ROW_NO
E_DRAGDROPOBJ.
"Drop
METHODS HANDLE_ONDROP
FOR EVENT ONDROP OF CL_GUI_ALV_GRID
IMPORTING E_ROW
E_COLUMN
E_DRAGDROPOBJ.
PRIVATE SECTION.
ENDCLASS. "CL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS CL_EVENT_RECEIVER IMPLEMENTATION.
" 單擊事件方法的實現
METHOD HANDLE_HOTSPOT_CLICK.
ENDMETHOD. "HANDLE_HOTSPOT_CLICK
" 雙擊事件方法的實現
METHOD HANDLE_DOUBLE_CLICK.
CLEAR GT_DATA_C .
READ TABLE GT_DATA_C[] INDEX E_ROW-INDEX INTO GT_DATA_C .
CLEAR GT_DATA_D[] .
LOOP AT GT_DATA INTO GT_DATA WHERE PRODH = GT_DATA_C-PRODH.
APPEND GT_DATA TO GT_DATA_D[] .
CLEAR GT_DATA .
ENDLOOP.
GO_ALV_GRID_SUB->REFRESH_TABLE_DISPLAY( ).
ENDMETHOD. "HANDLE_DOUBLE_CLICK
" 實現TOOLBAR事件方法
METHOD HANDLE_TOOLBAR.
DATA: LS_TOOLBAR TYPE STB_BUTTON.
DELETE E_OBJECT->MT_TOOLBAR WHERE FUNCTION = '&PRINT_BACK'.
CLEAR: LS_TOOLBAR.
LS_TOOLBAR-BUTN_TYPE = 3. " 分隔符
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
CLEAR: LS_TOOLBAR.
LS_TOOLBAR-FUNCTION = 'BT_8'. "功能碼
LS_TOOLBAR-ICON = ICON_INFORMATION. " 圖示名稱
LS_TOOLBAR-TEXT = '顯示資訊'. " 按鈕上顯示的文字
LS_TOOLBAR-QUICKINFO = '顯示資訊'. " 圖示的提示資訊
LS_TOOLBAR-BUTN_TYPE = 0. " 0表示正常按鈕
LS_TOOLBAR-DISABLED = ''. " X表示灰色,不可用
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD. "HANDLE_TOOLBAR
" 實現USER-COMMAND 事件方法
METHOD HANDLE_COMMAND.
* DATA: REF TYPE REF TO CL_GUI_ALV_GRID.
* DATA : LV_STABLE TYPE LVC_S_STBL. "重新整理穩定性
** RANGES: L_WERKS FOR T001W-WERKS.
** RANGES: L_MATNR FOR MARA-MATNR.
* DATA:L_BUKRS TYPE T001K-BUKRS.
*
* LV_STABLE-ROW = '1'.
* LV_STABLE-COL = '1'.
*
* CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* IMPORTING
* E_GRID = REF.
* CALL METHOD REF->CHECK_CHANGED_DATA.
CASE E_UCOMM.
WHEN 'BT_8'.
DEMO=>MAIN( ).
* SORT gt_data[] BY bedat.
* CALL METHOD go_alv_grid->refresh_table_display "內表改變時重新整理ALV
* EXPORTING
* is_stable = lv_stable.
WHEN OTHERS.
ENDCASE.
* CALL METHOD REF->REFRESH_TABLE_DISPLAY
* EXPORTING
* IS_STABLE = LV_STABLE.
ENDMETHOD. "HANDLE_COMMAND
" 實現DATA_CHANGED 事件方法
METHOD HANDLE_DATA_CHANGED.
ENDMETHOD. "handle_data_changed
* Drag & Drop
METHOD HANDLE_ONDRAG.
CHECK NOT E_ROW IS INITIAL.
* PERFORM DISPLAY_FLIGHTS USING NODE_KEY.
* CALL METHOD CL_GUI_CFW=>SET_NEW_OK_CODE EXPORTING NEW_CODE =
* FCODE_ENTR.
ENDMETHOD.
* Drag & Drop
METHOD HANDLE_ONDROP.
CHECK NOT E_ROW IS INITIAL.
*
ENDMETHOD.
ENDCLASS. "cl_event_receiver IMPLEMENTATION
***INCLUDE BCALV_EVENT_REC_DRAGDROP.
CLASS CL_TREE_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
DATA: UCOMM TYPE SY-UCOMM.
DATA: SELFIELD TYPE SLIS_SELFIELD.
* toolbar
METHODS HANDLE_TOOLBAR_SET
FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE.
* user command
METHODS HANDLE_USER_COMMAND
FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
* double click
METHODS HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN.
* context menue
METHODS HANDLE_CONTEXT_MENU
FOR EVENT CONTEXT_MENU_REQUEST OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT.
METHODS HANDLE_ONDROP
FOR EVENT ONDROP OF CL_GUI_ALV_GRID
IMPORTING E_ROW
E_COLUMN
E_DRAGDROPOBJ.
PRIVATE SECTION.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS CL_EVENT_RECEIVER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS CL_TREE_EVENT_RECEIVER IMPLEMENTATION.
* handle user_command
METHOD HANDLE_USER_COMMAND.
* CASE E_UCOMM.
* WHEN FCODE_DISP.
* PERFORM DISPLAY_DETAIL.
* WHEN FCODE_PLANE.
* PERFORM DISPLAY_SAPLANE.
* WHEN OTHERS.
* ENDCASE.
ENDMETHOD.
* handle double_click
METHOD HANDLE_DOUBLE_CLICK.
* PERFORM DISPLAY_SAPLANE.
ENDMETHOD.
* handle toolbar
METHOD HANDLE_TOOLBAR_SET.
** create own Menubuttons and ToolbarButtons
** append a separator to normal toolbar
* CLEAR GS_TOOLBAR.
* MOVE 3 TO GS_TOOLBAR-BUTN_TYPE.
* APPEND GS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
** append detail button
* CLEAR GS_TOOLBAR.
* MOVE FCODE_DISP TO GS_TOOLBAR-FUNCTION.
* MOVE ICON_DETAIL TO GS_TOOLBAR-ICON.
* MOVE TEXT-005 TO GS_TOOLBAR-QUICKINFO.
* MOVE ' ' TO GS_TOOLBAR-DISABLED.
* APPEND GS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
** append a separator to normal toolbar
* CLEAR GS_TOOLBAR.
* MOVE 3 TO GS_TOOLBAR-BUTN_TYPE.
* APPEND GS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
** append new button
* CLEAR GS_TOOLBAR.
* MOVE FCODE_PLANE TO GS_TOOLBAR-FUNCTION.
* MOVE ICON_WS_PLANE TO GS_TOOLBAR-ICON.
* MOVE TEXT-002 TO GS_TOOLBAR-QUICKINFO.
* MOVE ' ' TO GS_TOOLBAR-DISABLED.
* APPEND GS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD.
* context menue
METHOD HANDLE_CONTEXT_MENU.
* CALL METHOD E_OBJECT->ADD_FUNCTION
* EXPORTING
* FCODE = FCODE_DISP
* TEXT = TEXT-005.
* CALL METHOD E_OBJECT->ADD_FUNCTION
* EXPORTING
* FCODE = FCODE_PLANE
* TEXT = TEXT-002.
ENDMETHOD.
* Drag & Drop
METHOD HANDLE_ONDROP.
*
ENDMETHOD.
ENDCLASS.
*
DATA: EVENT_RECEIVER TYPE REF TO CL_TREE_EVENT_RECEIVER.
*----------------------------------------------------------------------*
* CLASS CL_TREE_EVENT_RECEIVER IMPLEMENTATION *
*----------------------------------------------------------------------*
CLASS CL_TREE_EVENT_RECEIVER_DRAG DEFINITION.
PUBLIC SECTION.
* double click item
METHODS HANDLE_DOUBLE_CLICK
FOR EVENT NODE_DOUBLE_CLICK OF CL_GUI_ALV_TREE
IMPORTING NODE_KEY.
* Drag
METHODS HANDLE_ON_DRAG
FOR EVENT ON_DRAG OF CL_GUI_ALV_TREE
IMPORTING DRAG_DROP_OBJECT
FIELDNAME
NODE_KEY.
PRIVATE SECTION.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS CL_TREE_EVENT_RECEIVER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS CL_TREE_EVENT_RECEIVER_DRAG IMPLEMENTATION.
* handle double_click
METHOD HANDLE_DOUBLE_CLICK.
CHECK NOT NODE_KEY IS INITIAL.
PERFORM DISPLAY_FLIGHTS USING NODE_KEY.
ENDMETHOD.
* Drag & Drop
METHOD HANDLE_ON_DRAG.
CHECK NOT NODE_KEY IS INITIAL.
PERFORM DISPLAY_FLIGHTS USING NODE_KEY.
* CALL METHOD CL_GUI_CFW=>SET_NEW_OK_CODE EXPORTING NEW_CODE =
* FCODE_ENTR.
ENDMETHOD.
ENDCLASS.
*
DATA: TREE_EVENT_RECEIVER TYPE REF TO CL_TREE_EVENT_RECEIVER_DRAG.
*&---------------------------------------------------------------------*
*& Form DISPLAY_FLIGHTS
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
* -->P_NODE_KEY text
*&---------------------------------------------------------------------*
FORM DISPLAY_FLIGHTS USING P_NODE_KEY TYPE LVC_NKEY.
DATA: GRID_TITLE TYPE LVC_TITLE.
DATA: LS_DATA TYPE GTS_DATA,
E_PARENT_NODE_KEY TYPE LVC_NKEY,
LT_CHILDREN TYPE LVC_T_NKEY WITH HEADER LINE,
LT_CHILDREN1 TYPE LVC_T_NKEY WITH HEADER LINE.
REFRESH GT_DATA_D .
CALL METHOD TREE1->GET_OUTTAB_LINE
EXPORTING I_NODE_KEY = P_NODE_KEY
IMPORTING E_OUTTAB_LINE = LS_DATA.
CLEAR GT_DATA_F[] .
CASE LS_DATA-P_NODE.
WHEN 'L'.
GRID_TITLE = '大類:'&& LS_DATA-VTEXT1 && ' 明細資料'.
CLEAR LT_CHILDREN[].
CALL METHOD TREE1->GET_CHILDREN
EXPORTING I_NODE_KEY = P_NODE_KEY
IMPORTING ET_CHILDREN = LT_CHILDREN[]