SAP ABAP OALV单元格颜色设置

OALV单元格颜色设置

效果如下:

完整代码:

*&---------------------------------------------------------------------*
*& Report  ZTEST04
*&
*&---------------------------------------------------------------------*
*&  by cmh
*&  定义单元格颜色
*&---------------------------------------------------------------------*

REPORT  ZTEST04.
TYPESBEGIN OF ilist_s ,
      name1(10),
      name2(10),
      name3(10),
      name4(10),
 END OF ilist_s.
DATAilist TYPE STANDARD TABLE OF ilist_s  WITH HEADER LINE .

DATABEGIN OF list OCCURS 0.
        INCLUDE STRUCTURE ilist .
DATArowcolor(4TYPE ,            "
      cellcolors TYPE lvc_t_scol ,    "单元格颜色
      cellstyles TYPE lvc_t_styl ,    "单元格样式
      celltext   TYPE alv_t_dtv .
DATAEND OF list.

DATAok_code LIKE sy-ucomm.
DATAl_valid(1TYPE c.

DATAgo_grid             TYPE REF TO cl_gui_alv_grid.

DATAgt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      sla         TYPE lvc_s_layo.

DATAls_fieldcat TYPE lvc_s_fcat.
DATA ls_style TYPE lvc_s_styl .
DATA ls_cellcolor TYPE lvc_s_scol .
DATA ls_celltext     TYPE alv_s_dtv .
*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS handle_button_click
      FOR EVENT button_click OF cl_gui_alv_grid
      IMPORTING es_col_id
                es_row_no.
ENDCLASS.                    "lcl_event_receiver DEFINITION*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_button_click.
    PERFORM test USING es_col_id
                       es_row_no.
  ENDMETHOD.                                                "handle_f4
ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION
DATAevent_receiver TYPE REF TO lcl_event_receiver.
DATAii TYPE .

START-OF-SELECTION.
  PERFORM frm_fieldcat.
  PERFORM frm_getdata.
  PERFORM frm_layout.
  PERFORM frm_display.





*&---------------------------------------------------------------------*
*&      Form  test
*&---------------------------------------------------------------------*
FORM test  USING    p_es_col_id TYPE lvc_s_col
                    p_es_row_no TYPE lvc_s_roid.
  MESSAGE i899(fiWITH p_es_col_id-fieldname p_es_row_no-row_id .
ENDFORM.                    " test
*&---------------------------------------------------------------------*
*&      Form  FRM_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_FIELDCAT .

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     'NAME1'.
  ls_fieldcat-scrtext_l     'TITLE1'.
  APPEND ls_fieldcat TO  gt_fieldcat.
  CLEAR ls_fieldcat.

  ls_fieldcat-fieldname     'NAME2'.
  ls_fieldcat-scrtext_l     'TITLE2'.
*    ls_fieldcat-emphasize = 'C610'.
  APPEND ls_fieldcat TO  gt_fieldcat.
  CLEAR ls_fieldcat.

  ls_fieldcat-fieldname     'NAME3'.
  ls_fieldcat-scrtext_l     'TITLE3'.
  APPEND ls_fieldcat TO  gt_fieldcat.

  CLEAR ls_fieldcat.

  ls_fieldcat-fieldname     'NAME4'.
  ls_fieldcat-scrtext_l     'TITLE4'.
  APPEND ls_fieldcat TO  gt_fieldcat.

ENDFORM.                    " FRM_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  FRM_GETDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_GETDATA .

  list-name1 'A1'.
  list-name2 'B'.
  ls_cellcolor-fname 'NAME1' .
  ls_cellcolor-color-col '5' .
  ls_cellcolor-color-int '1' .
  ls_cellcolor-color-inv '1' .
  APPEND ls_cellcolor TO list-cellcolors .
  APPEND list.

  list-name1 'A2'.
  list-name2 'BB'.
  ls_cellcolor-fname 'NAME2' .
  ls_cellcolor-color-col '3' .
  ls_cellcolor-color-int '1' .
  ls_cellcolor-color-inv '1' .
  APPEND ls_cellcolor TO list-cellcolors .
  APPEND list.

  list-name1 'A3'.
  list-name2 'BBB'.
  list-rowcolor 'C410'.

  ls_style-fieldname 'NAME3' .
  ls_style-style cl_gui_alv_grid=>mc_style_button .       " 按钮样式
*  ls_style-style = cl_gui_alv_grid=>MC_STYLE_HOTSPOT.
  APPEND ls_style TO list-cellstyles .
  ls_celltext-buttoncell 'TEST'.
  APPEND ls_celltext TO list-celltext.
  APPEND list.

  CLEAR list.
  list-name1 'A4'.
  list-name2 'B4'.
  APPEND list.

  CLEAR list.
  list-name1 'A5'.
  list-name2 'B5'.
  APPEND list.

ENDFORM.                    " FRM_GETDATA
*&---------------------------------------------------------------------*
*&      Form  FRM_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_LAYOUT .

  sla-stylefname 'CELLSTYLES'.
  sla-ctab_fname 'CELLCOLORS'.
  sla-info_fname 'ROWCOLOR'.

ENDFORM.                    " FRM_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  FRM_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DISPLAY .

  WRITE ''.

  CREATE OBJECT go_grid
        EXPORTING
          i_parent cl_gui_container=>screen0.

   CREATE OBJECT event_receiver.
  SET HANDLER event_receiver->handle_button_click FOR go_grid.

  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
*      is_variant                    = gs_variant
      i_save                        'A'
      is_layout                     sla
    CHANGING
      it_outtab                     list[]
      it_fieldcatalog               gt_fieldcat[]
    EXCEPTIONS
      invalid_parameter_combination 1
      program_error                 2
      too_many_lines                3
      OTHERS                        4.

ENDFORM.                    " FRM_DISPLAY

 

 另外附上SAP颜色的编码:

ABAP中的颜色代码是由4位字都组成的

Cxyz

C:color的简写,颜色代码均以C开头

 x:标准色代码,SAP中一共有7个标准色

           y:反转颜色启用/关闭 1/0

            z:增强颜色启用/关闭 1/0

       

      标准色代码一览表

X

颜色

主要用处

1

灰蓝色

抬头、标题栏

2

浅灰色

列表内容

3

黄色

汇总

4

蓝色

关键字

5

绿色

正确

6

红色

错误

7

橙色

控制

 

 

 

 

 

 


 

 

 

 

 

 

你可能感兴趣的:(SAP-ALL,ABAP)