在ALV GRID中可以修改想要強調的行顏色。首先內表中追加 linecolor欄位。
結構:
DATA: BEGIN GT_OUTTAB OCCURS 0.
INCLUDE STRUCTURE <DDIC-Struktur>.
DATA: linecolor(4) TYPE c. " 用於指定顏色的欄位
DATA: END OF GT_OUTTAB.
主要程式碼如下
~~省略~~ DATA: begin of gt_sflight OCCURS 0 . INCLUDE STRUCTURE sflight. DATA: light TYPE c. " 訊號燈 DATA: linecolor(4) TYPE c. "行顏色, 注意欄位名與長度沒有空格。 DATA: END OF gt_sflight. ~~省略~~ "訊號燈" LOOP AT gt_sflight. ~~~~省略~~ "設定行顏色 CASE gt_sflight-carrid. WHEN 'AA' . " 藍色 gt_sflight-linecolor = 'C100'. WHEN 'AZ'. " 黃色 gt_sflight-linecolor = 'C300'. WHEN 'DL' . " 綠色 gt_sflight-linecolor = 'C500'. ENDCASE. "設定行顏色 MODIFY gt_sflight. ENDLOOP. " 訊號燈 CALL SCREEN 100. ~~省略~~ FORM setting_layout CHANGING p_layout type lvc_s_layo. ~~~~~省略~~ p_layout-info_fname = 'LINECOLOR'. "顏色行 ENDFORM. " setting_layout.
詳細程式碼如下:
*&---------------------------------------------------------------------* *& Report Z15_21 *& 可執行程式 *&---------------------------------------------------------------------* *& *& 建立 ALV GRID 容器 *& 注意每個程式都需要啟用方可執行 *& 實現重新整理按鈕的 重新整理事件。 *& 實現使用欄位目錄 *& 使用FIELD SYMBOL 功能設定欄位顯示背景顏色 *& DATA: LIGHT TYPE C. 訊號燈。 *& COLORING ROWS: 顏色行 *&---------------------------------------------------------------------* REPORT Z15_21. TYPE-POOLS: icon. "" 宣告圖示 *& 內表結構型別 :開始 TYPES: BEGIN OF t_str. INCLUDE STRUCTURE sflight. TYPES: company type c LENGTH 6. TYPES: END OF t_str. *& 內表結構型別 :結束 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. " 排序 DATA: gt_fieldcat type lvc_t_fcat. " 宣告欄位目錄變數 *DATA: gt_sflight TYPE TABLE OF T_STR. *DATA: gt_sflight like sflight occurs 0 WITH HEADER LINE. DATA: begin of gt_sflight OCCURS 0 . INCLUDE STRUCTURE sflight. DATA: light TYPE c. " 訊號燈 DATA: linecolor(4) TYPE c. "行顏色, 注意欄位名與長度沒有空格。 DATA: END OF gt_sflight. TYPES: BEGIN OF TY_SFLIGHT, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, FLDATE TYPE SFLIGHT-FLDATE, PRICE TYPE SFLIGHT-PRICE, CURRENCY TYPE SFLIGHT-CURRENCY, PLANETYPE TYPE SFLIGHT-PLANETYPE, SEATSMAX TYPE SFLIGHT-SEATSMAX, SEATSOCC TYPE SFLIGHT-SEATSOCC, PAYMENTSUM TYPE SFLIGHT-PAYMENTSUM, SEATSMAX_B TYPE SFLIGHT-SEATSMAX_B, SEATSOCC_B TYPE SFLIGHT-SEATSOCC_B, SEATSMAX_F TYPE SFLIGHT-SEATSMAX_F, SEATSOCC_F TYPE SFLIGHT-SEATSOCC_F, END OF TY_SFLIGHT. PARAMETERS: p_var like disvariant-variant. " 引數變數 *& 設定GRID佈局 PERFORM setting_layout CHANGING gs_layout . *& 根據使用者顯示或隱藏佈局按鈕 PERFORM setting_toolbar. *& 排序 PERFORM setting_sort. PERFORM getting_catalog. *& 呼叫設定欄位目錄 setting_catalog PERFORM setting_catalog. *& 顯示檢索幫助 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 = 'Exception Test'. "標題欄" p_layout-sel_mode = 'D'. " 選擇模式:可選擇多行" p_layout-zebra = 'X'. "指定條紋(STRIPE)屬性" p_layout-excp_fname = 'LIGHT'. " 訊號燈 欄位 p_layout-info_fname = 'LINECOLOR'. "顏色行 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. *& 獲取欄位目錄的子程式 FORM getting_catalog. DATA: lt_fieldcat type kkblo_t_fieldcat. *& 呼叫函式 (函式組:SKBH) CALL FUNCTION 'K_KKB_FIELDCAT_MERGE' EXPORTING I_CALLBACK_PROGRAM = SY-REPID I_TABNAME = 'GT_SFLIGHT' * I_STRUCNAME = I_INCLNAME = SY-REPID * I_BYPASSING_BUFFER = * I_BUFFER_ACTIVE = CHANGING CT_FIELDCAT = LT_FIELDCAT[] * EXCEPTIONS * INCONSISTENT_INTERFACE = 1 * OTHERS = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. IF SY-SUBRC EQ 0. "呼叫函式, 函式組:SLVC" CALL FUNCTION 'LVC_TRANSFER_FROM_KKBLO' EXPORTING * I_TECH_COMPLETE = * I_STRUCTURE_NAME = IT_FIELDCAT_KKBLO = lt_fieldcat[] * IT_SORT_KKBLO = * IT_FILTER_KKBLO = * IT_SPECIAL_GROUPS_KKBLO = * IT_FILTERED_ENTRIES_KKBLO = * IT_GROUPLEVELS_KKBLO = * IS_SUBTOT_OPTIONS_KKBLO = * IS_LAYOUT_KKBLO = * IS_REPREP_ID_KKBLO = * I_CALLBACK_PROGRAM_KKBLO = * IT_ADD_FIELDCAT = * IT_EXCLUDING_KKBLO = * IT_EXCEPT_QINFO_KKBLO = IMPORTING ET_FIELDCAT_LVC = gt_fieldcat[] * ET_SORT_LVC = * ET_FILTER_LVC = * ET_SPECIAL_GROUPS_LVC = * ET_FILTER_INDEX_LVC = * ET_GROUPLEVELS_LVC = * ES_TOTAL_OPTIONS_LVC = * ES_LAYOUT_LVC = * ES_VARIANT_LVC = * E_VARIANT_SAVE_LVC = * ES_PRINT_INFO_LVC = * ES_REPREP_LVC = * E_REPREP_ACTIVE_LVC = * ET_EXCLUDING_LVC = * ET_EXCEPT_QINFO_LVC = * TABLES * IT_DATA = * EXCEPTIONS * IT_DATA_MISSING = 1 * OTHERS = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDIF. ENDFORM ." getting_catalog. " *& 設定欄位目錄的子程式 FORM setting_catalog. DATA: ls_fieldcat type lvc_s_fcat. " 宣告 欄位目錄變更 FIELD-SYMBOLS: <ls_fcat> TYPE lvc_s_fcat. * ls_fieldcat-fieldname = 'COMPANY'. * ls_fieldcat-coltext = 'Company Info'. * ls_fieldcat-just = 'C'. * ls_fieldcat-key = 'X'. * ls_fieldcat-outputlen = '6'. *ls_fieldcat-no_out = 'X'. " 不在ALV GRID *中顯示,注意在第一列設定不顯示,而第二列以後的列需要顯示,則需要在第二列 *&說明顯示欄位,否則後面欄位不顯示。 * APPEND ls_fieldcat to gt_fieldcat. * * ls_fieldcat-fieldname = 'CARRID'. "" 欄位名稱 * ls_fieldcat-coltext = 'Carrid ID'. "" 欄位顯示名稱 ,承運方 * ls_fieldcat-just = 'L'. "" 居左 * ls_fieldcat-KEY = 'X'. "" 主鍵標記 * ls_fieldcat-outputlen = '2'. "" 輸出寬度 * ls_fieldcat-no_out = ''. " 顯示欄位" * APPEND ls_fieldcat TO gt_fieldcat . * * ls_fieldcat-fieldname = 'CONNID'. * ls_fieldcat-coltext = 'Flight Number'. ""航班號 (連線號) * ls_fieldcat-just = 'C'. "" 居中 * ls_fieldcat-KEY = 'X'. * ls_fieldcat-outputlen = '4'. * APPEND ls_fieldcat TO gt_fieldcat . * * ls_fieldcat-fieldname = 'PRICE'. * ls_fieldcat-coltext = 'Airfare'."" 文字說明(即:欄位說明) * ls_fieldcat-just = 'R'. " 居右 * ls_fieldcat-KEY = ''. * ls_fieldcat-outputlen = '15'. * APPEND ls_fieldcat TO gt_fieldcat . *& 迴圈內表所有欄位 * LOOP AT gt_fieldcat INTO ls_fieldcat. * if ls_fieldcat-fieldname = 'PRICE'. * ls_fieldcat-coltext = 'AIR PRICE'. * ls_fieldcat-just = 'C'. * ls_fieldcat-emphasize = 'X'." 設定單元格背景色 * MODIFY gt_fieldcat FROM ls_fieldcat. * ENDIF. * ENDLOOP. *& 在ALV GRID中的前四個欄位是主鍵,顏色指定為藍色且固定了下面主鍵對應的 *& 捲軸。 LOOP AT gt_fieldcat ASSIGNING <ls_fcat>. if <ls_fcat>-fieldname EQ 'PRICE'. <ls_fcat>-key = 'X'. " 將單價欄位 設定為主鍵 ENDIF. IF <ls_fcat>-fieldname EQ 'FLDATE'. <ls_fcat>-edit_mask = '____/__/__' . "設定日期格式為: YYYY/MM/DD ENDIF. IF <ls_fcat>-fieldname EQ 'CURRENCY'. <ls_fcat>-fix_column = 'X'. "將 幣種 欄位設定為固定列 <ls_fcat>-just = 'R'. " 設定對齊方式,R:右對齊,L:左對齊,C:中間對齊。 ENDIF. endloop. ENDFORM. " setting_catalog. *& *CLASS lcl_dragdrop DEFINITION. * PUBLIC SECTION. * DATA: wa type ty_sflight, * index type i. " index of line to be move. *ENDCLASS. " LCL_dragdrop DEFINITION *& 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. " 宣告 工具欄事件方法 METHODS: handle_toolbar FOR EVENT toolbar OF CL_GUI_ALV_GRID IMPORTING e_object e_interactive . ""&宣告工具欄的重新整理按鈕事件方法 METHODS: handle_command FOR EVENT user_command OF CL_GUI_ALV_GRID IMPORTING e_ucomm. ""& 宣告 拖放方法(Drag & Drop) METHODS: handle_alv_drag FOR EVENT ondrag OF CL_GUI_ALV_GRID IMPORTING e_row e_column e_dragdropobj, handle_alv_drop FOR EVENT ondrop OF CL_GUI_ALV_GRID IMPORTING e_row e_column e_dragdropobj. PRIVATE SECTION. " ENDCLASS. CLASS lcl_event_receiver IMPLEMENTATION. METHOD handle_double_click. "雙擊事件方法的實現" LEAVE TO SCREEN 0 . ENDMETHOD. " 工具欄事件方法的實現 METHOD handle_toolbar . DATA: ls_toolbar TYPE stb_button. CLEAR ls_toolbar. ls_toolbar-butn_type = 3. APPEND ls_toolbar TO e_object->mt_toolbar. CLEAR ls_toolbar. ls_toolbar-function = 'RESH'. ls_toolbar-icon = icon_refresh. ls_toolbar-quickinfo = 'Refresh'. ls_toolbar-text = ''. ls_toolbar-disabled = ''. APPEND ls_toolbar TO e_object->mt_toolbar. " 增加一個 重新整理按鈕 ENDMETHOD. "" handle_toolbar ” "& 工具欄重新整理按鈕的重新整理方法的實現 METHOD handle_command. DATA: l_scroll type lvc_s_stbl. CASE e_ucomm. WHEN 'RESH'. SELECT * FROM SFLIGHT INTO TABLE gt_sflight UP TO 40 ROWS . "" 重新整理時全表查詢 l_scroll-row = 'X'. l_scroll-col = 'X'. CALL METHOD g_grid->refresh_table_display EXPORTING i_soft_refresh = '' is_stable = l_scroll. ENDCASE. ENDMETHOD. " handle_command. METHOD handle_alv_drag. * DATA: l_dragdrop TYPE REF TO lcl_dragdrop. ENDMETHOD. " handle_alv_drag. METHOD handle_alv_drop. * DATA: l_dragdrop TYPE REF TO lcl_dragdrop, * l_drop_index type i, * l_scroll type lvc_s_stbl. " 重新整理 ALV GRID 元件 * l_scroll-row = 'X'. * l_scroll-col = 'X'. * CATCH SYSTEM-EXCEPTIONS move_cast_error = 1. * l_dragdrop ?= e_dragdropobj->object. * CALL METHOD g_grid->refresh_table_display * EXPORTING * i_soft_refresh = 'X' * is_stable = l_scroll. * ENDCATCH. IF sy-subrc <> 0. " if anything went wrong aborting the drap and drop operation: CALL METHOD e_dragdropobj->abort. ENDIF. ENDMETHOD. " handle_alv_drop ENDCLASS. " LCL_event_receiver. DATA: event_receiver type ref to lcl_event_receiver . *& 查詢資料,然後呼叫螢幕100. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT UP TO 90 ROWS. "訊號燈" LOOP AT gt_sflight. IF gt_sflight-seatsocc <= 30. gt_sflight-light = '1'. " 1: 紅燈 ELSEIF gt_sflight-seatsocc <= 50. gt_sflight-light = '2'. "2: 黃燈 ELSE. gt_sflight-light = '3'. ""3:綠燈 ENDIF. "設定行顏色 CASE gt_sflight-carrid. WHEN 'AA' . " 藍色 gt_sflight-linecolor = 'C100'. WHEN 'AZ'. " 黃色 gt_sflight-linecolor = 'C300'. WHEN 'DL' . " 綠色 gt_sflight-linecolor = 'C500'. ENDCASE. "設定行顏色 MODIFY gt_sflight. ENDLOOP. " 訊號燈 CALL SCREEN 100. gs_variant-report = sy-repid. gs_variant-username = sy-uname. *& *& 建立 AVL控制元件 INCLUDE Z15_21_PBO. * INCLUDE Z15_20_PBO. *& *& PAI 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.
包含檔案:Z15_21_PBO
*----------------------------------------------------------------------* ***INCLUDE Z15_21_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" *& #REGION: 註冊事件 CREATE OBJECT event_receiver. * 註冊事件Handler方法 SET HANDLER event_receiver->handle_double_click FOR g_grid . *& *& 註冊重新整理按鈕事件 方法 SET HANDLER event_receiver->handle_toolbar FOR g_grid. *& 註冊工具欄重新整理按鈕的重新整理事件 方法 SET HANDLER event_receiver->handle_command FOR g_grid. *& 註冊 拖放事件 方法 SET HANDLER event_receiver->handle_alv_drop for g_grid. SET HANDLER event_receiver->handle_alv_drag for g_grid. *& #ENDREGION: 註冊事件 *& 模式-> 選擇: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[] " 若不帶表頭,則直接使用內表即可(即:GT_SFLGITH), *& 定義內表時若帶有表頭,則呼叫ALV時需要傳遞表體。 it_fieldcatalog = gt_fieldcat " 欄位目錄 it_sort = gt_sort. ENDMODULE. " INIT_CON OUTPUT *CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY * EXPORTING * I_BUFFER_ACTIVE = * I_BYPASSING_BUFFER = * I_CONSISTENCY_CHECK = * I_STRUCTURE_NAME = * IS_VARIANT = * I_SAVE = * I_DEFAULT = 'X' * IS_LAYOUT = * IS_PRINT = * IT_SPECIAL_GROUPS = * IT_TOOLBAR_EXCLUDING = * IT_HYPERLINK = * IT_ALV_GRAPHICS = * IT_EXCEPT_QINFO = * IR_SALV_ADAPTER = * CHANGING * IT_OUTTAB = * IT_FIELDCATALOG = * IT_SORT = * IT_FILTER = * EXCEPTIONS * INVALID_PARAMETER_COMBINATION = 1 * PROGRAM_ERROR = 2 * TOO_MANY_LINES = 3 * others = 4 * . *IF SY-SUBRC <> 0. ** Implement suitable error handling here *ENDIF.
效果圖: