目录
代码:
REPORT ZMMRTEST. *********************************************************************** * Tables Definitions ************************************************************************ TABLES:marc. ************************************************************************ * Data Definitions ************************************************************************ TYPES: BEGIN OF ty_data, linecolor(4) TYPE c, "行颜色 matnr LIKE mara-matnr, "物料 maktx LIKE makt-maktx, "物料说明 END OF ty_data. DATA: gt_data TYPE TABLE OF ty_data, gt_makt LIKE TABLE OF makt, wa_data LIKE LINE OF gt_data, wa_makt LIKE LINE OF gt_makt. TYPE-POOLS slis. DATA: gs_layout TYPE slis_layout_alv, gt_fieldcat TYPE slis_t_fieldcat_alv, wa_fieldcat TYPE slis_fieldcat_alv. ************************************************************************ * Includes Module ************************************************************************ ************************************************************************ * Selection Screen ************************************************************************ PARAMETERS: p_werks LIKE marc-werks DEFAULT '2000' OBLIGATORY. SELECT-OPTIONS: s_matnr FOR marc-matnr. ************************************************************************ * Initialization ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen ************************************************************************ AT SELECTION-SCREEN. ************************************************************************ * At Selection Screen Output ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process ************************************************************************ START-OF-SELECTION. PERFORM get_data. PERFORM display_data. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* FORM get_data . DATA: l_index LIKE sy-tabix. SELECT marc~matnr makt~maktx INTO CORRESPONDING FIELDS OF TABLE gt_data FROM marc INNER JOIN makt ON makt~matnr = marc~matnr WHERE marc~matnr IN s_matnr AND marc~werks EQ p_werks AND makt~spras = sy-langu. SORT: gt_data BY matnr. LOOP AT gt_data INTO wa_data. l_index = sy-tabix. IF l_index MOD 2 = 0. wa_data-linecolor = 'C310'. "设置颜色的值 ENDIF. MODIFY gt_data FROM wa_data INDEX l_index. ENDLOOP. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form DISPLAY_DATA *&---------------------------------------------------------------------* FORM display_data. "栏位最适宽度 gs_layout-colwidth_optimize = 'X'. "指定数据输出内表中哪列存储的是颜色 gs_layout-info_fieldname = 'LINECOLOR'. PERFORM set_fieldcat. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = gs_layout it_fieldcat = gt_fieldcat[] * i_callback_pf_status_set = 'alv_pf_status' " 触发事件调用子程序 * i_callback_user_command = 'alv_user_command' " 鼠标事件操作子程序 i_save = 'A' TABLES t_outtab = gt_data EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. " DISPLAY_DATA *&---------------------------------------------------------------------* *& Form SET_FIELDCAT *&---------------------------------------------------------------------* FORM set_fieldcat. DEFINE fieldcat. wa_fieldcat-fieldname = &1. "对应内表字段名 wa_fieldcat-seltext_l = &2. "输出列文本 APPEND wa_fieldcat TO gt_fieldcat.CLEAR wa_fieldcat. END-OF-DEFINITION. fieldcat 'MATNR' '物料'. fieldcat 'MAKTX' '物料说明'. ENDFORM. " SET_FIELDCAT
运行效果:
代码:
REPORT ZMMRTEST. *********************************************************************** * Tables Definitions ************************************************************************ TABLES:marc. ************************************************************************ * Data Definitions ************************************************************************ TYPES: BEGIN OF ty_data, matnr LIKE mara-matnr, "物料 maktx LIKE makt-maktx, "物料说明 END OF ty_data. DATA: gt_data TYPE TABLE OF ty_data, gt_makt LIKE TABLE OF makt, wa_data LIKE LINE OF gt_data, wa_makt LIKE LINE OF gt_makt. TYPE-POOLS slis. DATA: gs_layout TYPE slis_layout_alv, gt_fieldcat TYPE slis_t_fieldcat_alv, wa_fieldcat TYPE slis_fieldcat_alv. ************************************************************************ * Includes Module ************************************************************************ ************************************************************************ * Selection Screen ************************************************************************ PARAMETERS: p_werks LIKE marc-werks DEFAULT '2000' OBLIGATORY. SELECT-OPTIONS: s_matnr FOR marc-matnr. ************************************************************************ * Initialization ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen ************************************************************************ AT SELECTION-SCREEN. ************************************************************************ * At Selection Screen Output ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process ************************************************************************ START-OF-SELECTION. PERFORM get_data. PERFORM display_data. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* FORM get_data . SELECT marc~matnr makt~maktx INTO CORRESPONDING FIELDS OF TABLE gt_data FROM marc INNER JOIN makt ON makt~matnr = marc~matnr WHERE marc~matnr IN s_matnr AND marc~werks EQ p_werks AND makt~spras = sy-langu. SORT: gt_data BY matnr. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form DISPLAY_DATA *&---------------------------------------------------------------------* FORM display_data. "栏位最适宽度 gs_layout-colwidth_optimize = 'X'. PERFORM set_fieldcat. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = gs_layout it_fieldcat = gt_fieldcat[] * i_callback_pf_status_set = 'alv_pf_status' " 触发事件调用子程序 * i_callback_user_command = 'alv_user_command' " 鼠标事件操作子程序 i_save = 'A' TABLES t_outtab = gt_data EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. " DISPLAY_DATA *&---------------------------------------------------------------------* *& Form SET_FIELDCAT *&---------------------------------------------------------------------* FORM set_fieldcat. DEFINE fieldcat. wa_fieldcat-fieldname = &1. "对应内表字段名 wa_fieldcat-seltext_l = &2. "输出列文本 wa_fieldcat-emphasize = &3. "栏位的颜色 APPEND wa_fieldcat TO gt_fieldcat.CLEAR wa_fieldcat. END-OF-DEFINITION. fieldcat 'MATNR' '物料' 'C310'. fieldcat 'MAKTX' '物料说明' ''. ENDFORM. " SET_FIELDCAT
运行效果:
REPORT ZMMRTEST. *********************************************************************** * Tables Definitions ************************************************************************ TABLES:marc. ************************************************************************ * Data Definitions ************************************************************************ TYPES: BEGIN OF ty_data, matnr LIKE mara-matnr, "物料 maktx LIKE makt-maktx, "物料说明 color TYPE lvc_t_scol, "单元格颜色 END OF ty_data. DATA: gt_data TYPE TABLE OF ty_data, gt_makt LIKE TABLE OF makt, wa_data LIKE LINE OF gt_data, wa_makt LIKE LINE OF gt_makt. TYPE-POOLS slis. DATA: gs_layout TYPE slis_layout_alv, gt_fieldcat TYPE slis_t_fieldcat_alv, wa_fieldcat TYPE slis_fieldcat_alv, wa_cellcolor TYPE lvc_s_scol. "ALV单元格颜色 ************************************************************************ * Includes Module ************************************************************************ ************************************************************************ * Selection Screen ************************************************************************ PARAMETERS: p_werks LIKE marc-werks DEFAULT '2000' OBLIGATORY. SELECT-OPTIONS: s_matnr FOR marc-matnr. ************************************************************************ * Initialization ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen ************************************************************************ AT SELECTION-SCREEN. ************************************************************************ * At Selection Screen Output ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process ************************************************************************ START-OF-SELECTION. PERFORM get_data. PERFORM display_data. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* FORM get_data . DATA: l_index LIKE sy-tabix. SELECT marc~matnr makt~maktx INTO CORRESPONDING FIELDS OF TABLE gt_data FROM marc INNER JOIN makt ON makt~matnr = marc~matnr WHERE marc~matnr IN s_matnr AND marc~werks EQ p_werks AND makt~spras = sy-langu. SORT: gt_data BY matnr. LOOP AT gt_data INTO wa_data. l_index = sy-tabix. IF l_index MOD 2 = 0. wa_cellcolor-fname = 'MATNR'. wa_cellcolor-color-col = 6. wa_cellcolor-color-int = 1. wa_cellcolor-color-inv = 0. APPEND wa_cellcolor TO wa_data-color. ELSE. wa_cellcolor-fname = 'MAKTX'. wa_cellcolor-color-col = 5. wa_cellcolor-color-int = 1. wa_cellcolor-color-inv = 0. APPEND wa_cellcolor TO wa_data-color. ENDIF. MODIFY gt_data FROM wa_data INDEX l_index. ENDLOOP. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form DISPLAY_DATA *&---------------------------------------------------------------------* FORM display_data. "栏位最适宽度 gs_layout-colwidth_optimize = 'X'. "单元格颜色 gs_layout-coltab_fieldname = 'COLOR'. PERFORM set_fieldcat. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = gs_layout it_fieldcat = gt_fieldcat[] * i_callback_pf_status_set = 'alv_pf_status' " 触发事件调用子程序 * i_callback_user_command = 'alv_user_command' " 鼠标事件操作子程序 i_save = 'A' TABLES t_outtab = gt_data EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. " DISPLAY_DATA *&---------------------------------------------------------------------* *& Form SET_FIELDCAT *&---------------------------------------------------------------------* FORM set_fieldcat. DEFINE fieldcat. wa_fieldcat-fieldname = &1. "对应内表字段名 wa_fieldcat-seltext_l = &2. "输出列文本 APPEND wa_fieldcat TO gt_fieldcat.CLEAR wa_fieldcat. END-OF-DEFINITION. fieldcat 'MATNR' '物料' . fieldcat 'MAKTX' '物料说明' . ENDFORM. " SET_FIELDCAT
运行效果: