- 实际开发中会有一些需求要求添加红绿黄灯列,例如ALVZ中交货单是否已完成拣配、未拣配、未完成拣配等状态进行红绿灯的显示,以求达到ALV简洁明了的目的,显示效果如下图所示。
- 完成这一功能只需要在结构定义时添加一个字段然后赋值即可。字段类型参考ICONNAME。
TYPES:BEGIN OF ty_data,light TYPE iconname, END OF ty_data.DATA gw_data TYPE ty_data.gw_data-light = icon_red_light. "表示红灯gw_data-light = ICON_yellow_LIGHT. "表示黄灯gw_data-light = ICON_green_LIGHT. "表示绿灯
TYPES:BEGIN OF ty_data,sel TYPE char1,light TYPE iconname,name TYPE char10,score TYPE p LENGTH 2 DECIMALS 1,remark TYPE c LENGTH 20,END OF ty_data.DATA gt_data TYPE TABLE OF ty_data.
DATA gw_data TYPE ty_data.** Layout 变量定义
DATA gs_layout TYPE lvc_s_layo.** Fieldcat 变量定义
DATA:gt_fieldcat TYPE lvc_t_fcat,gs_fieldcat LIKE LINE OF gt_fieldcat.** FieldCat的宏
DATA gv_pos TYPE i.DEFINE %%add_fieldcat.gv_pos = gv_pos + 1.gs_fieldcat-col_pos = gv_pos.gs_fieldcat-fieldname = &1.gs_fieldcat-scrtext_m = &2.CASE &1.WHEN 'SEL'.gs_fieldcat-checkbox = 'X'. "当做选择框显示gs_fieldcat-edit = 'X'. "该字段可以编辑ENDCASE.APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat.
END-OF-DEFINITION.START-OF-SELECTION.PERFORM get_data.PERFORM dis_falv.FORM get_data.gt_data[] = VALUE #(( sel = '' light = '' name = 'TOM' score = '58')( sel = '' light = '' name = 'JEERY' score = '98')( sel = '' light = '' name = 'MASTER' score = '68')).LOOP AT gt_data INTO gw_data.IF gw_data-score < 60.gw_data-light = icon_red_light.gw_data-remark = '不及格'.ELSEIF gw_data-score >= 60 AND gw_data-score < 85.gw_data-light = ICON_yellow_LIGHT.gw_data-remark = '良好'.ELSEIF gw_data-score >= 85.gw_data-light = ICON_green_LIGHT.gw_data-remark = '优秀'.ENDIF.MODIFY gt_data FROM gw_data.ENDLOOP.
ENDFORM.FORM dis_falv.
** Fieldcat字段映射处理%%add_fieldcat:'SEL' '选择', "( 前面字段值不可变 后面字段描述可变 每一行都是)'LIGHT' '状态','REMARK' '备注','NAME' '姓名','SCORE' '分数'.** Layout布局处理gs_layout-cwidth_opt = 'X'. "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果)gs_layout-zebra = 'X'. "斑马纹输出** 调用FunctionALV需要传入的参数:展示数据,FieldCat,Layout,StatusCALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'EXPORTINGi_callback_program = sy-repidi_callback_pf_status_set = 'FRM_SET_STATUS'is_layout_lvc = gs_layoutit_fieldcat_lvc = gt_fieldcatTABLESt_outtab = gt_data[]EXCEPTIONSprogram_error = 1OTHERS = 2.
ENDFORM.FORM frm_set_status USING p_extab TYPE slis_t_extab.SET PF-STATUS 'STANDARD'.
ENDFORM.