在SAP显示ALV界面时候需要做一些特殊处理,比如需要批量选择,给行项目添加图标,给单元格做可编辑状态等等一系列的动作。
让ALV处于更多元化的显示(PS:颜色显示没多大意思,很多时候更笨不会用到)
实例代码如下
type-pools : slis,icon. data :begin of gs_itab , matnr type mara-matnr, mtart type mara-mtart, matkl type mara-matkl, MEINS type mara-MEINS, icon LIKE icon-id, ""图标设置 ZFLAG type FLAG, "多选按钮设置 color type char04, ""行颜色设置 cellcolor type LVC_T_SCOL, "单元格颜色设置 FIELD_STYLE TYPE LVC_T_STYL, ""可编辑单元格设置 end of gs_itab. data stylelin type lvc_s_styl. "可编辑单元格结构 data is_cellcolor type lvc_s_scol. ""单元格颜色结构体 data : gt_itab like TABLE OF gs_itab . field-symbols : <fs_itab> LIKE gs_itab. data: ls_cellcolor type lvc_s_scol , gt_field type lvc_t_fcat with header line, ls_layout type lvc_s_layo. DEFINE FIELDCAT. gt_field-COL_POS = &1. gt_field-FIELDNAME = &2. gt_field-SCRTEXT_L = &3. APPEND gt_field. END-OF-DEFINITION. START-OF-SELECTION. SELECT MARA~MATNR MARA~MTART MARA~MATKL mara~MEINS FROM MARA INTO CORRESPONDING FIELDS OF TABLE GT_ITAB UP TO 100 ROWS WHERE MTART = 'Z001'. FIELDCAT '' 'ICON' '图标'. FIELDCAT '' 'MATNR' '物料'. FIELDCAT '' 'MTART' '物料类型'. FIELDCAT '' 'MATKL' '物料组'. FIELDCAT '' 'MEINS' '基本单位'. LOOP AT gt_field. IF gt_field-FIELDNAME = 'MTART'. gt_field-EMPHASIZE = 'C610'. "某列设置单元格 MODIFY gt_field. CLEAR gt_field. ENDIF. IF gt_field-FIELDNAME = 'MATNR' or gt_field-FIELDNAME = 'MEINS' . gt_field-edit = 'X'. MODIFY gt_field. CLEAR gt_field. ENDIF. ENDLOOP. loop at gt_itab ASSIGNING <fs_itab>. <fs_itab>-icon = ICON_GREEN_LIGHT. """""""""""给ALV行号设置颜色"""""""""""""""""" if <fs_itab>-MATKL = '102001'. <fs_itab>-color = 'C510'. endif. """"""""""""将某一个单元设置为可编辑状态 if <fs_itab>-matnr ne '000000001101002881'. STYLELIN-FIELDNAME = 'MATNR'. STYLELIN-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED. " 设置为不可编辑状态 APPEND STYLELIN TO <fs_itab>-FIELD_STYLE. endif. """""""""""将某一个单元设置为可编辑状态 """""""""""给单元阁设置颜色 if <fs_itab>-matkl = '108001'. IS_CELLCOLOR-FNAME = 'MATKL'. IS_CELLCOLOR-COLOR-COL = 6. IS_CELLCOLOR-COLOR-INT = 0. IS_CELLCOLOR-COLOR-INV = 0. APPEND IS_CELLCOLOR TO <fs_itab>-CELLCOLOR. endif. if <fs_itab>-matkl = '102001'. IS_CELLCOLOR-FNAME = 'MATKL'. IS_CELLCOLOR-COLOR-COL = 3. IS_CELLCOLOR-COLOR-INT = 0. IS_CELLCOLOR-COLOR-INV = 0. APPEND IS_CELLCOLOR TO <fs_itab>-CELLCOLOR. endif. """""""""""""""" ENDLOOP. ls_layout-CWIDTH_OPT = 'X'. ls_layout-BOX_FNAME = 'ZFLAG'. "设置多选行 ls_layout-ZEBRA = 'X'. ls_layout-INFO_FNAME = 'COLOR'. "某行数据设置颜色 ls_layout-CTAB_FNAME = 'CELLCOLOR'. "单元格颜色内表字段 ls_layout-STYLEFNAME = 'FIELD_STYLE'. """ 将内表中的字段名存入显示格式 做是否可编辑 LS_LAYOUT-CWIDTH_OPT = 'X'. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING I_CALLBACK_PROGRAM = SY-CPROG IS_LAYOUT_LVC = ls_layout IT_FIELDCAT_LVC = GT_FIELD[] TABLES T_OUTTAB = GT_ITAB[]. * EXCEPTIONS * PROGRAM_ERROR = 1 * OTHERS = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF.
显示效果图: