1.信号灯
绿灯:3
黄灯:2
红灯:1
黄灯:2
红灯:1
关键代码如下:
DATA: BEGIN OF gt_data OCCURS 0.INCLUDE STRUCTURE spfli.
DATA: light TYPE c. " 显示信号灯的字段
DATA: END OF gt_data.LOOP AT gt_data." 为內表的信号灯字段赋值IF gt_data-carrid = 'AA'.gt_data-light = '1'.ELSEIF gt_data-carrid = 'AB'.gt_data-light = '2'.ELSE.gt_data-light = '3'.ENDIF.MODIFY gt_data TRANSPORTING light.
ENDLOOP.FORM set_layout CHANGING p_layout TYPE lvc_s_layo." 设置ALV布局p_layout-excp_fname = 'LIGHT'.
ENDFORM.
2.行颜色
关键代码如下:
DATA: BEGIN OF gt_data OCCURS 0.INCLUDE STRUCTURE spfli.
DATA: linecolor(4) TYPE c. " 指定行颜色的字段
DATA: END OF gt_data.LOOP AT gt_data." 为行颜色字段赋值CASE gt_data-carrid.WHEN 'AA'.gt_data-linecolor = 'C400'.WHEN 'AB'.gt_data-linecolor = 'C500'.WHEN OTHERS.gt_data-linecolor = 'C600'.ENDCASE.MODIFY gt_data TRANSPORTING linecolor.
ENDLOOP.FORM set_layout CHANGING p_layout TYPE lvc_s_layo." 设置布局p_layout-info_fname = 'LINECOLOR'.
ENDFORM.
3.单元格颜色
关键代码如下:
DATA: BEGIN OF gt_data OCCURS 0.INCLUDE STRUCTURE spfli.
DATA: cellcolor TYPE lvc_t_scol.
DATA: END OF gt_data.FORM set_cellcolor.DATA: lt_color TYPE lvc_t_scol,ls_color TYPE lvc_s_scol,ls_fieldcat TYPE lvc_s_fcat,l_index TYPE i.LOOP AT gt_data.l_index = l_index + 1.CLEAR: lt_color[].LOOP AT gt_fieldcat INTO ls_fieldcat.CLEAR: ls_color.ls_color-fname = ls_fieldcat-fieldname.IF ls_color-fname EQ 'AIRPFROM'.CASE gt_data-airpfrom.WHEN 'ASP'.ls_color-color-col = 3. " ls_color-color是一结构,仍包含三个字段ls_color-color-int = 0.ls_color-color-inv = 0.WHEN 'GIG'.ls_color-color-col = 6.ls_color-color-int = 0.ls_color-color-inv = 0.WHEN OTHERS.ls_color-color-col = 5.ls_color-color-int = 0.ls_color-color-inv = 0.ENDCASE.INSERT ls_color INTO TABLE lt_color.ENDIF.ENDLOOP.CLEAR: gt_data-cellcolor[].INSERT LINES OF lt_color INTO TABLE gt_data-cellcolor.MODIFY gt_data INDEX l_index TRANSPORTING cellcolor.ENDLOOP.
ENDFORM.FORM set_layout CHANGING p_layout TYPE lvc_s_layo.p_layout-ctab_fname = 'CELLCOLOR'.
ENDFORM.
4.完整示例
c
(1). 创建100屏幕
PROCESS BEFORE OUTPUT.MODULE STATUS_0100.MODULE INIT_CON.PROCESS AFTER INPUT.MODULE USER_EXIT_0100 AT EXIT-COMMAND.
(2). 在100屏幕上面画自定义控件,命名为CUS01
(3). 程序代码
REPORT ztest_sy09.DATA: BEGIN OF gt_data OCCURS 0.INCLUDE STRUCTURE spfli.
DATA: light TYPE c,linecolor(4) TYPE c,cellcolor TYPE lvc_t_scol.
DATA: END OF gt_data.DATA: g_con01 TYPE REF TO cl_gui_custom_container.DATA: g_grid01 TYPE REF TO cl_gui_alv_grid.DATA: gs_fieldcat TYPE lvc_s_fcat.
DATA: gt_fieldcat TYPE lvc_t_fcat.CONSTANTS: g_cus01 TYPE scrfname VALUE 'CUS01'.START-OF-SELECTION.PERFORM get_data.CALL SCREEN 100.*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text 获取数据
*----------------------------------------------------------------------*
FORM get_data.SELECT *INTO CORRESPONDING FIELDS OF TABLE gt_dataUP TO 30 ROWSFROM spfli.ENDFORM. "GET_DATA*&---------------------------------------------------------------------*
*& Form set_light
*&---------------------------------------------------------------------*
* text 信号灯字段赋值
*----------------------------------------------------------------------*
FORM set_light.LOOP AT gt_data.CASE gt_data-carrid.WHEN 'AA'.gt_data-light = '1'. " 红灯WHEN 'AB'.gt_data-light = '2'. " 黄灯WHEN OTHERS.gt_data-light = '3'. " 绿灯ENDCASE.MODIFY gt_data TRANSPORTING light linecolor.ENDLOOP.ENDFORM. "set_light*&---------------------------------------------------------------------*
*& Form set_linecolor
*&---------------------------------------------------------------------*
* text 行颜色字段赋值
*----------------------------------------------------------------------*
FORM set_linecolor.LOOP AT gt_data.CASE gt_data-carrid.WHEN 'AA'.gt_data-linecolor = 'C400'.WHEN 'AB'.gt_data-linecolor = 'C500'.ENDCASE.MODIFY gt_data TRANSPORTING linecolor.ENDLOOP.
ENDFORM. "set_linecolor*&---------------------------------------------------------------------*
*& Form set_cellcolor
*&---------------------------------------------------------------------*
* text 单元格颜色字段赋值
*----------------------------------------------------------------------*
FORM set_cellcolor.DATA: lt_color TYPE lvc_t_scol,ls_color TYPE lvc_s_scol,ls_fieldcat TYPE lvc_s_fcat,l_index TYPE i.LOOP AT gt_data.l_index = l_index + 1.CHECK gt_data-carrid = 'AC'." 只处理AC行的单元格CLEAR: lt_color[].LOOP AT gt_fieldcat INTO ls_fieldcat.CLEAR: ls_color.ls_color-fname = ls_fieldcat-fieldname.IF ls_color-fname EQ 'AIRPFROM'.CASE gt_data-airpfrom.WHEN 'ASP'.ls_color-color-col = 3.ls_color-color-int = 0.ls_color-color-inv = 0.WHEN 'GIG'.ls_color-color-col = 6.ls_color-color-int = 0.ls_color-color-inv = 0.ENDCASE.INSERT ls_color INTO TABLE lt_color.ENDIF.ENDLOOP.CLEAR: gt_data-cellcolor[].INSERT LINES OF lt_color INTO TABLE gt_data-cellcolor.MODIFY gt_data INDEX l_index TRANSPORTING cellcolor.ENDLOOP.
ENDFORM. "SET_cellcolor*&---------------------------------------------------------------------*
*& Form SET_FIELDCAT
*&---------------------------------------------------------------------*
* text ALV显示字段文本
*----------------------------------------------------------------------*
FORM set_fieldcat.CLEAR: gt_fieldcat[],gs_fieldcat.DEFINE macro_fieldcat.CLEAR gs_fieldcat.gs_fieldcat-fieldname = &1.gs_fieldcat-coltext = &2.APPEND gs_fieldcat TO gt_fieldcat.END-OF-DEFINITION.macro_fieldcat 'CARRID' '航线代码'.macro_fieldcat 'CONNID' '航班连接Id'.macro_fieldcat 'COUNTRYFR' '起飞国家'.macro_fieldcat 'CITYFROM' '起飞城市'.macro_fieldcat 'AIRPFROM' '起飞机场'.macro_fieldcat 'COUNTRYTO' '目标国家'.macro_fieldcat 'CITYTO' '目标城市'.macro_fieldcat 'AIRPTO' '目标机场'.macro_fieldcat 'FLTIME' '航班时间'.macro_fieldcat 'DEPTIME' '启程时间'.macro_fieldcat 'ARRTIME' '到达时间'.macro_fieldcat 'DISTANCE' '距离'.ENDFORM. "SET_FIELDCAT*&---------------------------------------------------------------------*
*& Form SET_LAYOUT
*&---------------------------------------------------------------------*
* text 布局
*----------------------------------------------------------------------*
* -->P_LAYOUT text
*----------------------------------------------------------------------*
FORM set_layout CHANGING p_layout TYPE lvc_s_layo.p_layout-excp_fname = 'LIGHT'.p_layout-info_fname = 'LINECOLOR'.p_layout-ctab_fname = 'CELLCOLOR'.
ENDFORM. "SET_LAYOUT*&---------------------------------------------------------------------*
*& Module INIT_CON OUTPUT
*&---------------------------------------------------------------------*
* text 创建ALV
*----------------------------------------------------------------------*
MODULE init_con OUTPUT.DATA: ls_layout TYPE lvc_s_layo.IF g_con01 IS INITIAL.PERFORM set_fieldcat.PERFORM set_light.PERFORM set_linecolor.PERFORM set_cellcolor.PERFORM set_layout CHANGING ls_layout." 创建SAP容器实例CREATE OBJECT g_con01EXPORTINGcontainer_name = g_cus01 " 自定义控件名称EXCEPTIONScntl_error = 1cntl_system_error = 2create_error = 3lifetime_error = 4lifetime_dynpro_dynpro_link = 5OTHERS = 6.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF." 创建GRID实例CREATE OBJECT g_grid01EXPORTINGi_parent = g_con01EXCEPTIONSerror_cntl_create = 1error_cntl_init = 2error_cntl_link = 3error_dp_create = 4OTHERS = 5.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF." 调用ALV 显示方法CALL METHOD g_grid01->set_table_for_first_displayEXPORTING
* i_structure_name = 'SPFLI' " 参照表结构字段显示is_layout = ls_layout " 布局CHANGINGit_fieldcatalog = gt_fieldcat " 显示字段it_outtab = gt_data[]EXCEPTIONSinvalid_parameter_combination = 1program_error = 2too_many_lines = 3OTHERS = 4.IF sy-subrc <> 0.
* Implement suitable error handling hereENDIF.ENDIF.ENDMODULE. " INIT_CON OUTPUT*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text 工具条
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.SET PF-STATUS 'STATUS_BAR'.ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_EXIT_0100 INPUT
*&---------------------------------------------------------------------*
* text 取消事件
*----------------------------------------------------------------------*
MODULE user_exit_0100 INPUT.LEAVE TO SCREEN 0.
ENDMODULE. " USER_EXIT_0100 INPUT
注: (1). 参考资料:《SAP ABAP开发从入门到精通》 作者:金圣俊; 译者:张钟淑; 机械工业出版社;
(2). 如有疏漏之处,还请不吝赐教!