直接上代码吧,copy过去就可以用:

*&---------------------------------------------------------------------*
*& Report ZLM_ALV_07
*& ​​http://blog.sina.com.cn/sapliumeng​​
*&---------------------------------------------------------------------*
*1)在alv引用类型池定义时加入icon,如type-pools: slis, icon.
*
*2)给显示内表itab定义一字段statu存储状态灯;
*
*3)循环内表itab给字段statu值赋:
*绿灯:itab-statu = icon_led_green.
*红灯:itab-statu = icon_led_red.
*4)alv的fieldcat属性i_fieldcat-icon = 'X'.
*&---------------------------------------------------------------------*

REPORT zlm_alv_07.

*&---------------------------------------------------------------------*
*& 声明
*&---------------------------------------------------------------------*
TYPE-POOLS: slis, icon.

*&---------------------------------------------------------------------*
*&基本数据定义
*&---------------------------------------------------------------------*
DATA:
BEGIN OF str_data,
id TYPE int4,
age TYPE int4,
statu TYPE char10,
END OF str_data.

DATA:
g_it_data LIKE TABLE OF str_data,
g_wa_data LIKE str_data.

*&---------------------------------------------------------------------*
*& ALV相关定义
*&---------------------------------------------------------------------*
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.

*&---------------------------------------------------------------------*
*& 宏定义
*&---------------------------------------------------------------------*
DEFINE fill_fieldcat.
fieldcat-fieldname = &1.
fieldcat-seltext_l = &2.
fieldcat-icon = 'X'. "图标功能
append fieldcat.
END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*& 主逻辑
*&---------------------------------------------------------------------*

START-OF-SELECTION.
PERFORM zform_fill_data.
PERFORM zform_show.

*&---------------------------------------------------------------------*
*& form区
*&---------------------------------------------------------------------*

FORM zform_fill_data.
fill_fieldcat 'ID' '编号'.
fill_fieldcat 'AGE' '年龄'.
fill_fieldcat 'STATU' '灯饰'.

g_wa_data-id = 1.
g_wa_data-age = 12.
g_wa_data-statu = icon_led_red.

APPEND g_wa_data TO g_it_data.

g_wa_data-id = 2.
g_wa_data-age = 13.
g_wa_data-statu = icon_led_green.

APPEND g_wa_data TO g_it_data.
ENDFORM. "zform_fill_data

*&---------------------------------------------------------------------*
*& Form zform_show
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM zform_show.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
it_fieldcat = fieldcat[]
TABLES
t_outtab = g_it_data[].
ENDFORM. "zform_show​



OO 方式ALV 红绿灯

效果:

ALV显示红绿灯(FM ALV 和 OO ALV两种实现方式)_字段

主要代码:

*&---------------------------------------------------------------------*
*& 全局变量
*&---------------------------------------------------------------------*
DATA:


status TYPE icon_d,
carrid TYPE sflight-carrid
connid TYPE sflight-connid
fldate TYPE sflight-fldate
price TYPE sflight-price , "航空运费
cellstyle TYPE lvc_t_styl, "可编辑字段SORT表
END OF str_spfli.
DATA:


START-OF-SELECTION.


connid
fldate
price
FROM sflight INTO CORRESPONDING FIELDS OF TABLE g_it_show.
FIELD-SYMBOLS 《l_show》 LIKE LINE OF g_it_show. "英文单引号会被html误解析,特使用《》







《l_show》-status = icon_led_red.






FORM zform_alv_show .
DATA: l_style TYPE i,
ls_layout TYPE lvc_s_layo,
is_stable TYPE lvc_s_stbl,

* 屏幕初始化



repid
dynnr
* side = cl_gui_docking_container=>dock_at_right ”ALV贴屏幕右边,从屏幕右边开始算宽度
side = cl_gui_docking_container=>dock_at_left
extension = 900 "屏幕宽度
* style = cl_gui_control=>ws_child

cntl_error = 1
cntl_system_error
create_error = 3
lifetime_error = 4

OTHERS = 6.




*添加自定义工具条

*



*构建显示字段样式





* layout 设定样式

ls_layout-cwidth_opt = 'X'.


* event 绑定事件

SET HANDLER g_event->toolbar

* SET HANDLER g_event->handle_f4
SET HANDLER g_event->handle_cell_click


SET HANDLER g_event->handle_onf1
"以下两个方法达到的效果是:光标离开正在编辑的单元格时,就触发了handle_data_changed
"如果不注释掉以下两个方法,则只有执行下一操作时才会触发handle_data_changed,比如点保存点删除






*


is_layout = ls_layout
is_variant


it_fieldcatalog = it_fieldcatalog[]
it_outtab = g_it_show[].











is_stable = is_stable




ENDFORM. " ZFORM_ALV_SHOW