SAP 建立ALV GRID容器
1、主程式
*&---------------------------------------------------------------------* *& Report Z15_12 *& 可執行程式 *&---------------------------------------------------------------------* *& *& 建立 ALV GRID 容器 *& 注意每個程式都需要啟用方可執行 *&---------------------------------------------------------------------* REPORT Z15_12. DATA: CON1_REF TYPE REF TO CL_GUI_CUSTOM_CONTAINER. " 定義容器, 參照 自定義控制元件"其中自定義控制元件命名為CON1 DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID. "定義ALV變數,參照 CL_GUI_ALV_GRID類" DATA: GS_VARIANT like DISVARIANT, "" 列表變數 gs_cs_variant like disvariant. DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT. " 要顯示在螢幕上的內表 DATA: gs_layout type lvc_s_layo . DATA: gt_toolbar TYPE ui_functions . DATA: gt_sort TYPE lvc_t_sort. " 排序 PARAMETERS: p_var like disvariant-variant. " 引數變數 *& 設定GRID佈局 PERFORM setting_layout CHANGING gs_layout . *& 根據使用者顯示或隱藏佈局按鈕 PERFORM setting_toolbar. *& 排序 PERFORM setting_sort. *& 顯示檢索幫助 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_var. *& gs_variant-report = sy-repid. CALL FUNCTION 'REUSE_ALV_VARIANT_F4' EXPORTING IS_VARIANT = gs_variant * I_TABNAME_HEADER = * I_TABNAME_ITEM = * IT_DEFAULT_FIELDCAT = I_SAVE = 'A' * I_DISPLAY_VIA_GRID = ' ' IMPORTING * E_EXIT = ES_VARIANT = gs_variant EXCEPTIONS NOT_FOUND = 1 PROGRAM_ERROR = 2 OTHERS = 3. IF SY-SUBRC <> 0. * Implement suitable error handling here p_var = gs_variant-variant . ENDIF. FORM setting_layout CHANGING p_layout type lvc_s_layo. p_layout-cwidth_opt = 'X' . "自動調節長度 p_layout-grid_title = 'get_current_cell Method TEST'. "標題欄" p_layout-sel_mode = 'D'. " 選擇模式:可選擇多行" p_layout-zebra = 'X'. "指定條紋(STRIPE)屬性" ENDFORM. " setting_layout. *& 根據使用者顯示或隱藏佈局按鈕 FORM setting_toolbar. DATA : l_exclude TYPE UI_FUNC. l_exclude = cl_gui_alv_grid=>mc_fc_save_variant. APPEND l_exclude TO gt_toolbar. l_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant. APPEND l_exclude TO gt_toolbar. ENDFORM. " SETTING_TOOLBAR *& 排序 子程式 FORM setting_sort. DATA: ls_sort TYPE lvc_s_sort. ls_sort-spos = '1'. " 排序順序 ,整形數字 ls_sort-fieldname = 'CARRID'. " 排序欄位名稱 ls_sort-up = 'X' . " 升序 或 LS_SORT-DOWN = 'X' "降序 ls_sort-subtot = 'X' . " 顯示合計 APPEND ls_sort to gt_sort . ENDFORM. " setting_sort. CLASS lcl_event_receiver DEFINITION. PUBLIC SECTION. " 宣告雙擊事件方法 METHODS: handle_double_click FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID IMPORTING e_row e_column. ENDCLASS. CLASS lcl_event_receiver IMPLEMENTATION. METHOD handle_double_click. "雙擊事件方法的實現" LEAVE TO SCREEN 0 . ENDMETHOD. ENDCLASS. " LCL_event_receiver. DATA: event_receiver type ref to lcl_event_receiver . *CREATE OBJECT event_receiver. * 註冊事件Handler方法 *SET HANDLER event_receiver->handle_double_click FOR g_grid . *& *& 查詢資料,然後呼叫螢幕100. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT UP TO 25 ROWS. CALL SCREEN 100. gs_variant-report = sy-repid. gs_variant-username = sy-uname. *& *& 建立 AVL控制元件 INCLUDE Z15_12_PBO. *INCLUDE Z15_11_PBO. * INCLUDE Z15_10_PBO. * MODULE user_command_0100 INPUT. DATA: l_row type i, l_value type c, l_col type i, ls_row type lvc_s_row, ls_col type lvc_s_col, ls_roid type lvc_s_roid. DATA: ls_scroll type lvc_s_stbl. ls_scroll-row = 'X'. ls_scroll-col = 'X'. *& 模式-> 選擇:ABAP物件模式 ,(繼續)-> 呼叫方法, 在介面欄=G_GRID, 類/介面欄= CL_GUI_ALV_GRID, 方法欄= get_current_cell CALL METHOD G_GRID->GET_CURRENT_CELL IMPORTING E_ROW = l_row E_VALUE = l_value E_COL = l_col ES_ROW_ID = ls_row ES_COL_ID = ls_col ES_ROW_NO = ls_roid. *& 重新查詢已經顯示在ALV的輸出表時使用的方法。 CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY EXPORTING IS_STABLE = ls_scroll I_SOFT_REFRESH = 'X' * EXCEPTIONS * FINISHED = 1 * others = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDMODULE. " user_command_0100.
2、 建立 PBO 子程式
命名為 Z15_12_PBO
*----------------------------------------------------------------------* ***INCLUDE Z15_12_PBO. *----------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Module INIT_CON OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE INIT_CON OUTPUT. *& 1、建立容器物件 IF CON1_REF IS INITIAL. " 如果容器是空的,建立容器物件" CREATE OBJECT CON1_REF EXPORTING CONTAINER_NAME = 'CON1'. *& 2、建立 ALV GRID 控制元件 物件 CREATE OBJECT G_GRID EXPORTING I_PARENT = CON1_REF . * EXCEPTIONS * error_cnt1_create = 1 * error_cnt1_init = 2 * error_cnt1_link = 3 * error_dp_create = 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. ENDIF. " CON1_REF" CREATE OBJECT event_receiver. * 註冊事件Handler方法 SET HANDLER event_receiver->handle_double_click FOR g_grid . *& *& 模式-> 選擇:ABAP物件模式 ,(繼續)-> 呼叫方法, 在介面欄=G_GRID, 類/介面欄= CL_GUI_ALV_GRID, 方法欄= SET_TABLE_FOR_FIRST_DISPLAY *& 3、 ALV GRID顯示資料方法 CALL METHOD g_grid->set_table_for_first_display EXPORTING I_STRUCTURE_NAME = 'SFLIGHT' i_save = 'A' is_variant = gs_variant i_default = ' ' is_layout = gs_layout it_toolbar_excluding = gt_toolbar CHANGING It_OUTTAB = GT_SFLIGHT it_sort = gt_sort. ENDMODULE. " INIT_CON OUTPUT
3、螢幕邏輯流程式
PROCESS BEFORE OUTPUT. * MODULE STATUS_0100. MODULE init_con. * PROCESS AFTER INPUT. MODULE USER_COMMAND_0100.