ALV 颜色设置(行,列,单元格)

【转自:https://www.cnblogs.com/mingdashu/p/color_alv.html】 

BCALV_EDIT_03

http://blog.sina.com.cn/s/blog_a87b19300102who3.html

 

关于ALV表格颜色,这种需求在项目中会经常用到。

 

  • 列颜色

列的颜色很简单,在构成ALV字段的FIELDCAT内表中有一个字段是EMPHASIZE,将一个char型4位的颜色代码分配到FIELDCAT内表这个字段即可。

1 LW_FCAT-EMPHASIZE = 'C710'. "设置字段的颜色
2 
3 MODIFY IT_FIELDCAT FROM LW_FCAT
4 TRANSPORTING EMPHASIZE
5 WHERE FIELDNAME = 'ESLRY'.
  • 行颜色

1. 在构成ALV数据的内表中,添加一个char型4位的字段(CLR),用来记录颜色代码

ALV 颜色设置(行,列,单元格)
1 TABLES ZEMP_TEST.
2 
3 TYPES: BEGIN OF TY_ZEMP.
4            INCLUDE STRUCTURE ZEMP_TEST.
5 TYPES: CLR TYPE CHAR4,"可以控制行颜色
6         END OF TY_ZEMP.
7 
8 DATA: IT_ZEMP TYPE STANDARD TABLE OF TY_ZEMP,
9       IW_ZEMP TYPE TY_ZEMP.
ALV 颜色设置(行,列,单元格)

 

2. 循环内表,设置颜色代码

ALV 颜色设置(行,列,单元格)
1   LOOP AT IT_ZEMP INTO IW_ZEMP.
2      "员工编号为‘3的行,颜色为红色
3    IF IW_ZEMP-EMPID = '3'.
4         IW_ZEMP-CLR = 'C610'.
5         MODIFY IT_ZEMP FROM IW_ZEMP.
6         CLEAR IW_ZEMP.
7      ENDIF.
8   ENDLOOP.
ALV 颜色设置(行,列,单元格)

3. LAYOUT结构中INFO_FIELDNAME的值指定为 CLR

  IW_LAYOUT-INFO_FIELDNAME = 'CLR'.  "行颜色代码的字段
  • 单元格颜色

单元格颜色的设置与行颜色设计基本一致

1. 在构成ALV数据的内表中,添加一个内表 CellColor,用来记录颜色代码,需要设置颜色的字段名

1 TYPES: BEGIN OF TY_ZEMP.
2        INCLUDE STRUCTURE ZEMP_TEST.
3 TYPES: CLR TYPE CHAR4,
4        CELLCOLOR TYPE LVC_T_SCOL, "单元格颜色
5        END OF TY_ZEMP.

CellColor的结构:FNAME        ALV 控制: 内部表字段的字段名称         

COLOR        ALV 控制: 颜色代码     COLOR是一个结构:COL      ALV 控制: 颜色      

                                                                            INT      ALV 控制: 强化    1/0                                    

                                                                            INV      ALV 控制: 相反    1/0    设置颜色是前景,或者是背景         

NOKEYCOL  ALV 控制: 覆盖码颜色

2. 循环内表,设置颜色代码

ALV 颜色设置(行,列,单元格)
 1   LOOP AT IT_ZEMP INTO IW_ZEMP.
 2 
 3      "员工编号为‘3’的行,颜色为红色
 4      IF IW_ZEMP-EMPID = '3'.
 5        IW_ZEMP-CLR = 'C610'.
 6 
 7        MODIFY IT_ZEMP FROM IW_ZEMP.
 8        CLEAR IW_ZEMP.
 9 
10      ENDIF.
11 
12      IF IW_ZEMP-EMPID = '2'.
13        IW_CELLCOLOR-FNAME = 'ENAME'.
14        "员工编号为‘2’的行的‘ENAME’字段颜色为 黄色
15        IW_CELLCOLOR-COLOR-COL = 3.
16        IW_CELLCOLOR-COLOR-INT = 1.
17        IW_CELLCOLOR-COLOR-INV = 0.
18 
19        APPEND IW_CELLCOLOR TO IW_ZEMP-CELLCOLOR.
20        MODIFY IT_ZEMP FROM IW_ZEMP.
21        CLEAR IW_ZEMP.
22 
23      ENDIF.
24   ENDLOOP.
ALV 颜色设置(行,列,单元格)

 

3. LAYOUT结构中COLTAB_FIELDNAME的值指定为CELLCOLOR

  IW_LAYOUT-COLTAB_FIELDNAME = 'CELLCOLOR'. "单元格颜色内表字段

最后在调用ALV函数时,设置layout,fieldcat参数

     IS_LAYOUT                         = IW_LAYOUT
     IT_FIELDCAT                       = IT_FIELDCAT
  • ALV工具栏

可以自定义工具栏,或者添加标准ALV工具栏,然后去除一些不需要的功能,这样可以保留标准工具栏的功能

ALV 颜色设置(行,列,单元格)
 1   DATA:BEGIN OF TAB OCCURS 0,
 2          FCODE LIKE RSMPE-FUNC,
 3        END OF TAB.
 4 
 5   TAB-FCODE = '&OL0'.APPEND TAB.
 6   TAB-FCODE = '&OAD'.APPEND TAB.
 7   TAB-FCODE = '&AVE'.APPEND TAB.
 8 
 9   SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'
10   EXCLUDING TAB IMMEDIATELY.
ALV 颜色设置(行,列,单元格)

 

  • 求和
LW_FCAT-DO_SUM = 'X'.  "立即求和,只有I,F,P类型的列可以求和

----------------------------------------------------------

 运行效果如下:

ALV 颜色设置(行,列,单元格)

 

在SE38环境下的程序名输入栏输入’DEMO*’后按F4,你可以查到SAP所有的DEMO示例程序,会学到很多ABAP功能的实现方法,

输入’BCALV*’后按F4,你可以查到很多ALV示例程序。

 

附:ALV的颜色代码:http://blog.csdn.net/wren2004/article/details/3941551

  ALV 颜色设置(行,列,单元格)

*----------------------------------------------------------------------------------------------------------------------------------、

ALV 颜色设置(行,列,单元格)

 

ALV 颜色设置(行,列,单元格)
  1 INCLUDE <icon>.
  2 
  3 INCLUDE <symbol>.
  4 
  5  
  6 
  7 TYPE-POOLS slis.
  8 
  9  
 10 
 11 TABLES spfli.
 12 
 13  
 14 
 15 TYPES: BEGIN OF ty_layout,
 16 
 17         exception(1),
 18 
 19         checkbox,
 20 
 21         icon TYPE icon_d,
 22 
 23         symbol TYPE icon_d,
 24 
 25         color(4),
 26 
 27         cell_color TYPE slis_t_specialcol_alv,
 28 
 29         hyperlink TYPE string,
 30 
 31         link_handle TYPE int4,
 32 
 33        END OF ty_layout.
 34 
 35 TYPES BEGIN OF ty_spfli.
 36 
 37 INCLUDE TYPE ty_layout.
 38 
 39 INCLUDE TYPE spfli.
 40 
 41 TYPES END OF ty_spfli.
 42 
 43 TYPES ty_tab_spfli TYPE ty_spfli OCCURS 0.
 44 
 45  
 46 
 47 PERFORM f_main.
 48 
 49  
 50 
 51 *&---------------------------------------------------------------------*
 52 
 53 *&      Form  f_main
 54 
 55 *&---------------------------------------------------------------------*
 56 
 57 *       ALV display
 58 
 59 *----------------------------------------------------------------------*
 60 
 61 FORM f_main.
 62 
 63   DATA: lt_spfli TYPE ty_tab_spfli,
 64 
 65         lt_fieldcat TYPE slis_t_fieldcat_alv,
 66 
 67         ls_layout TYPE slis_layout_alv,
 68 
 69         lt_hyperlink TYPE lvc_t_hype,
 70 
 71         lt_add_fieldcat TYPE slis_t_add_fieldcat.
 72 
 73  
 74 
 75   PERFORM get_data TABLES lt_spfli.
 76 
 77  
 78 
 79   PERFORM set_catalog CHANGING lt_fieldcat.
 80 
 81  
 82 
 83   PERFORM set_layout USING ls_layout.
 84 
 85  
 86 
 87   PERFORM hyperlink_build TABLES lt_hyperlink.
 88 
 89  
 90 
 91   PERFORM add_fieldcat_build TABLES lt_add_fieldcat.
 92 
 93  
 94 
 95   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 96 
 97     EXPORTING
 98 
 99       i_callback_program = sy-repid
100 
101       i_grid_title       = 'Xin''s ALV'
102 
103       it_fieldcat        = lt_fieldcat
104 
105       it_hyperlink       = lt_hyperlink
106 
107       is_layout          = ls_layout
108 
109       it_add_fieldcat    = lt_add_fieldcat
110 
111     TABLES
112 
113       t_outtab           = lt_spfli.
114 
115 ENDFORM.                    "f_main
116 
117  
118 
119 *&---------------------------------------------------------------------*
120 
121 *&      Form  set_layout
122 
123 *&---------------------------------------------------------------------*
124 
125 *       text
126 
127 *----------------------------------------------------------------------*
128 
129 *      -->P_LAYOUT   text
130 
131 *----------------------------------------------------------------------*
132 
133 FORM set_layout USING p_layout TYPE slis_layout_alv.
134 
135   p_layout-zebra = 'X'.
136 
137   p_layout-colwidth_optimize = 'X'.
138 
139   p_layout-window_titlebar = 'LinHuanXin''s ALV Demo'.
140 
141   p_layout-no_hline = 'X'.
142 
143   p_layout-no_vline = 'X'.
144 
145   p_layout-info_fieldname = 'COLOR'.
146 
147   p_layout-coltab_fieldname = 'CELL_COLOR'.
148 
149   p_layout-lights_fieldname = 'EXCEPTION'.
150 
151 ENDFORM.                    "set_layout
152 
153  
154 
155 *&---------------------------------------------------------------------*
156 
157 *&      Form  get_data
158 
159 *&---------------------------------------------------------------------*
160 
161 *       Get the data for output
162 
163 *----------------------------------------------------------------------*
164 
165 *      -->PT_SPFLI   data internal table
166 
167 *----------------------------------------------------------------------*
168 
169 FORM get_data TABLES pt_spfli TYPE ty_tab_spfli.
170 
171  
172 
173   DATA: lt_cell TYPE slis_t_specialcol_alv,
174 
175         ls_cell LIKE LINE OF lt_cell,
176 
177         lwa_spfli LIKE LINE OF pt_spfli.
178 
179  
180 
181   SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE pt_spfli.
182 
183  
184 
185   LOOP AT pt_spfli INTO lwa_spfli.
186 
187  
188 
189     IF lwa_spfli-distid = 'KM'.
190 
191       lwa_spfli-color = 'C610'.
192 
193     ENDIF.
194 
195     IF lwa_spfli-distance < 1000.
196 
197       CLEAR ls_cell.
198 
199       REFRESH lt_cell.
200 
201       ls_cell-fieldname = 'DISTANCE'.
202 
203       ls_cell-color-col = '4'.
204 
205       ls_cell-color-int = '1'.
206 
207       ls_cell-color-inv = '0'.
208 
209       ls_cell-nokeycol = 'X'.
210 
211       APPEND ls_cell TO lt_cell.
212 
213       lwa_spfli-cell_color = lt_cell.
214 
215       lwa_spfli-checkbox = 'X'.
216 
217       lwa_spfli-exception = '1'.
218 
219     ELSE.
220 
221       lwa_spfli-exception = '2'.
222 
223     ENDIF.
224 
225  
226 
227     lwa_spfli-link_handle = sy-tabix.
228 
229     lwa_spfli-hyperlink = 'www.163.com'.
230 
231     lwa_spfli-icon = icon_detail.
232 
233     lwa_spfli-symbol = sym_folder.
234 
235     MODIFY pt_spfli FROM lwa_spfli.
236 
237   ENDLOOP.
238 
239 ENDFORM.                    "get_data
240 
241  
242 
243 *&---------------------------------------------------------------------*
244 
245 *&      Form  add_fieldcat_build
246 
247 *&---------------------------------------------------------------------*
248 
249 *       text
250 
251 *----------------------------------------------------------------------*
252 
253 *      -->PT_FIELDCAT  text
254 
255 *----------------------------------------------------------------------*
256 
257 FORM add_fieldcat_build TABLES pt_fieldcat TYPE slis_t_add_fieldcat.
258 
259   DATA lwa_fieldcat LIKE LINE OF pt_fieldcat.
260 
261  
262 
263   lwa_fieldcat-fieldname = 'HYPERLINK'.
264 
265   lwa_fieldcat-web_field = 'LINK_HANDLE'.
266 
267   APPEND lwa_fieldcat TO pt_fieldcat.
268 
269 ENDFORM.                    "get_data
270 
271  
272 
273 *&---------------------------------------------------------------------*
274 
275 *&      Form  hyperlink_build
276 
277 *&---------------------------------------------------------------------*
278 
279 *       text
280 
281 *----------------------------------------------------------------------*
282 
283 *      -->PT_HYPERLINK  text
284 
285 *----------------------------------------------------------------------*
286 
287 FORM hyperlink_build TABLES pt_hyperlink TYPE lvc_t_hype.
288 
289   DATA lwa_hyperlink LIKE LINE OF pt_hyperlink.
290 
291  
292 
293   DO 13 TIMES.
294 
295     lwa_hyperlink-handle = sy-index.
296 
297     lwa_hyperlink-href   = 'www.sina.com.cn'.
298 
299     APPEND lwa_hyperlink TO pt_hyperlink.
300 
301   ENDDO.
302 
303   DO 13 TIMES.
304 
305     lwa_hyperlink-handle = 13 + sy-index.
306 
307     lwa_hyperlink-href   = 'www.163.com'.
308 
309     APPEND lwa_hyperlink TO pt_hyperlink.
310 
311   ENDDO.
312 
313  
314 
315 ENDFORM.                    "hyperlink_build
316 
317  
318 
319 *&---------------------------------------------------------------------*
320 
321 *&      Form  set_catalog
322 
323 *&---------------------------------------------------------------------*
324 
325 *       set fieldcatalog for alv output
326 
327 *----------------------------------------------------------------------*
328 
329 FORM set_catalog CHANGING pt_fieldcat TYPE slis_t_fieldcat_alv.
330 
331   DATA lwa_fieldcat TYPE slis_fieldcat_alv.
332 
333  
334 
335   "macro
336 
337   DEFINE add_field.
338 
339     clear lwa_fieldcat.
340 
341     lwa_fieldcat-fieldname = &1.
342 
343     lwa_fieldcat-seltext_l = &2.
344 
345     lwa_fieldcat-emphasize = &3.
346 
347     lwa_fieldcat-key = &4.
348 
349     lwa_fieldcat-fix_column = &4.
350 
351     append lwa_fieldcat to pt_fieldcat.
352 
353   END-OF-DEFINITION.
354 
355  
356 
357   "add field catalog
358 
359   add_field 'MANDT'     'Client'                 ''     'X'.
360 
361   add_field 'CARRID'    'Airline Code'           ''     'X'.
362 
363   add_field 'CONNID'    'Fight Number'           ''     'X'.
364 
365   add_field 'COUNTRYFR' 'Country From'           ''     ''.
366 
367   add_field 'CITYFROM'  'City From'              ''     ''.
368 
369   add_field 'AIRPFROM'  'Airpart From'           ''     ''.
370 
371   add_field 'COUNTRYTO' 'Country To'             'C510' ''.
372 
373   add_field 'CITYTO'    'City To'                ''     ''.
374 
375   add_field 'AIRPTO'    'Airpart To'             ''     ''.
376 
377   add_field 'FLTIME'    'Fly Time'               ''     ''.
378 
379   add_field 'DEPTIME'   'Dept Time'              ''     ''.
380 
381   add_field 'ARRTIME'   'Arrive Time'            ''     ''.
382 
383   add_field 'DISTANCE'  'Distance'               ''     ''.
384 
385   add_field 'DISTID'    'Mass unit of distance'  ''     ''.
386 
387   add_field 'FLTYPE'    'Flight type'            ''     ''.
388 
389   add_field 'PERIOD'    'Arrival n day(s) later' ''     ''.
390 
391   add_field 'HYPERLINK' 'HYPERLINK'              ''     ''.
392 
393  
394 
395   "checkbox
396 
397   CLEAR lwa_fieldcat.
398 
399   lwa_fieldcat-fieldname = 'CHECKBOX'.
400 
401   lwa_fieldcat-seltext_l = 'Checkbox'.
402 
403   lwa_fieldcat-checkbox = 'X'.
404 
405   lwa_fieldcat-fix_column = 'X'.
406 
407   APPEND lwa_fieldcat TO pt_fieldcat.
408 
409  
410 
411   "icon
412 
413   CLEAR lwa_fieldcat.
414 
415   lwa_fieldcat-fieldname = 'ICON'.
416 
417   lwa_fieldcat-seltext_l = 'Icon'.
418 
419   lwa_fieldcat-icon = 'X'.
420 
421   APPEND lwa_fieldcat TO pt_fieldcat.
422 
423  
424 
425   "symbol
426 
427   CLEAR lwa_fieldcat.
428 
429   lwa_fieldcat-fieldname = 'SYMBOL'.
430 
431   lwa_fieldcat-seltext_l = 'Symbol'.
432 
433   lwa_fieldcat-symbol = 'X'.
434 
435   APPEND lwa_fieldcat TO pt_fieldcat.
436 
437  
438 
439 ENDFORM.                    "set_catalog
ALV 颜色设置(行,列,单元格)

 

ALV 颜色设置(行,列,单元格)
1 BUTN_TYPE                  按钮类型
2                                                可用的按钮类型:
3                                                   0 Button(normal)
4                                                   1 Menu and default button
5                                                   2 Menu
6                                                   3 分割符
7                                                   4 Radio button
8                                                   5 Checkbox
9                                                   6 Menu entry
ALV 颜色设置(行,列,单元格)
上一篇:SAP-简单的OALV演示练习


下一篇:abap alv双击事件