在ALV的开发过程中经常需要给行,列,单元格设置不同的颜色,用来帮助用户区分一些意外或重要的信息,比如某些物料缺少库存,某订单的状态是未清之类的。本文就着重介绍如何给SALV设置颜色。
颜色设定的有以下三种:
1,单元格颜色
为特定的单元格设置颜色,这需要在ALV输出内表中添加一个专门保存颜色的字段,类型为lvc_t_scol,设置完颜色后(包括列名字,行号码),通过调用方法set_color_column()将颜色字段传递给SALV。
2,行颜色
与单元格颜色设置方法类似,只是不用指定列名字,只要指定行号就可以了。
3,列颜色
相对简单,只要在取得列对象引用(cl_salv_column_table )后,通过调用方法set_color()就能实现。
本实例中,要实现设置sale document type列第三行颜色为红色,create on列为黄色,第五行为绿色
ALV中颜色色号请参照-->ALV单元格颜色代码
在SALV教程2-添加标准状态栏(status)代码的基础上添加方法set_colors().
新的UML流程图如下:
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
*&---------------------------------------------------------------------* *& 一个简单的通过类CL_SALV_TABLE生成SALV的例子 *&---------------------------------------------------------------------* REPORT z_salv_8. * *----------------------------------------------------------------------* * CLASS lcl_report DEFINITION *----------------------------------------------------------------------* CLASS lcl_report DEFINITION. * PUBLIC SECTION. * * 定义SALV输出内表 TYPES: BEGIN OF ty_vbak, vbeln TYPE vbak-vbeln, erdat TYPE erdat, auart TYPE auart, kunnr TYPE kunnr, t_color TYPE lvc_t_scol, "设置单元格颜色用 END OF ty_vbak. * TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak. DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak. * * ALV reference DATA: o_alv TYPE REF TO cl_salv_table. * METHODS: * 抽取数据 get_data, * * 生成输出内容 generate_output. * *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$* * * 定义private method来设定SALV不同特性 PRIVATE SECTION. METHODS: set_pf_status CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: set_colors CHANGING co_alv TYPE REF TO cl_salv_table ct_vbak TYPE ty_t_vbak. *$*$*.....CODE_ADD_1 - End....................................1..*$*$* ENDCLASS. "lcl_report DEFINITION *----------------------------------------------------------------------* * CLASS lcl_report IMPLEMENTATION *----------------------------------------------------------------------* * 类实现 CLASS lcl_report IMPLEMENTATION. * METHOD get_data. * 抽取数据 SELECT vbeln erdat auart kunnr INTO CORRESPONDING FIELDS OF TABLE t_vbak FROM vbak UP TO 20 ROWS. * ENDMETHOD. "get_data *....................................................................... METHOD generate_output. * New ALV instance * We are calling the static Factory method which will give back * the ALV object reference. * * exception class DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = o_alv CHANGING t_table = t_vbak ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. * *$*$*.....CODE_ADD_2 - Begin..................................2..*$*$* * * In this area we will call the methods which will set the * different properties to the ALV * 调用status设置方法 CALL METHOD set_pf_status CHANGING co_alv = o_alv. * 调用设置颜色用方法 CALL METHOD set_colors CHANGING co_alv = o_alv ct_vbak = t_vbak. *$*$*.....CODE_ADD_2 - End....................................2..*$*$* * * * 调用Display方法将数据输出到屏幕上 o_alv->display( ). * ENDMETHOD. "generate_output * *$*$*.....CODE_ADD_3 - Begin..................................3..*$*$* * * In this area we will implement the methods which are defined in * the class definition * status设置方法实现 METHOD set_pf_status. DATA: lo_functions TYPE REF TO cl_salv_functions_list. * 设置默认的标准状态栏 lo_functions = co_alv->get_functions( ). lo_functions->set_default( abap_true ). ENDMETHOD. METHOD set_colors. * *.....Color for COLUMN..... DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table, lo_col_tab TYPE REF TO cl_salv_column_table. DATA: ls_color TYPE lvc_s_colo. " Colors strucutre * * get Columns object lo_cols_tab = co_alv->get_columns( ). * INCLUDE <color>. * * 取得 ERDAT列对象 & 设定黄颜色 TRY. lo_col_tab ?= lo_cols_tab->get_column( 'ERDAT' ). ls_color-col = col_total. "色号 lo_col_tab->set_color( ls_color ). CATCH cx_salv_not_found. ENDTRY. * *.......Color for Specific Cell & Rows................. * Applying color on the 3rd Row and Column AUART * Applying color on the Entire 5th Row * DATA: lt_s_color TYPE lvc_t_scol, ls_s_color TYPE lvc_s_scol, la_vbak LIKE LINE OF ct_vbak, l_count TYPE i. * LOOP AT ct_vbak INTO la_vbak. l_count = l_count + 1. CASE l_count. * 设定AUART字段,第三行的单元格颜色 WHEN 3. ls_s_color-fname = 'AUART'. ls_s_color-color-col = col_negative. ls_s_color-color-int = 0. ls_s_color-color-inv = 0. APPEND ls_s_color TO lt_s_color. CLEAR ls_s_color. * * 设定第五行颜色, * 不赋值列名字就是对于整行颜色的设置, WHEN 5. ls_s_color-color-col = col_positive. ls_s_color-color-int = 0. ls_s_color-color-inv = 0. APPEND ls_s_color TO lt_s_color. CLEAR ls_s_color. ENDCASE. * Modify that data back to the output table la_vbak-t_color = lt_s_color. MODIFY ct_vbak FROM la_vbak. CLEAR la_vbak. CLEAR lt_s_color. ENDLOOP. * * We will set this COLOR table field name of the internal table to * COLUMNS tab reference for the specific colors TRY. lo_cols_tab->set_color_column( 'T_COLOR' ). CATCH cx_salv_data_error. "#EC NO_HANDLER ENDTRY. * ENDMETHOD. "set_colors *$*$*.....CODE_ADD_3 - End....................................3..*$*$* * ENDCLASS. "lcl_report IMPLEMENTATION START-OF-SELECTION. DATA: lo_report TYPE REF TO lcl_report. CREATE OBJECT lo_report. * 抽取数据 lo_report->get_data( ). * 生成SALV lo_report->generate_output( ). |
运行显示如下:行,列,单元格的颜色都设置成功。
以上。
发表评论