ALV中背景和字体颜色改变的做法

 REPORT  ztest NO STANDARD PAGE HEADING.

* ALV
TYPE-POOLS: slis.

TYPES: BEGIN OF ty_data,
        key(10),
        2 type string,
        text TYPE c LENGTH 50,
     END OF ty_data.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
     gs_layout   TYPE slis_layout_alv.

* Data to be displayed
DATA: gt_data TYPE TABLE OF ty_data WITH KEY key WITH HEADER LINE.

*---------------------------------------------------------------------*
INITIALIZATION.
 PERFORM e01_fieldcat_init  USING gt_fieldcat[].
 .
*---------------------------------------------------------------------*
START-OF-SELECTION.
 PERFORM selection.

*---------------------------------------------------------------------*
END-OF-SELECTION.
 CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'  
这个是改变字体的
*  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'  
“这个是改变背景的
   EXPORTING
     i_callback_program      = sy-repid
     is_layout               = gs_layout
     it_fieldcat             = gt_fieldcat[]
   TABLES
     t_outtab                = gt_data
   EXCEPTIONS
     program_error           = 1
     OTHERS                  = 2.

*---------------------------------------------------------------------*
*  -->  E01_LT_FIELDCAT                                               *
*---------------------------------------------------------------------*
FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.
 DATA: ls_fieldcat TYPE slis_fieldcat_alv.

* Key
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = 'KEY'.
 ls_fieldcat-tabname      = 'GT_DATA'.
 ls_fieldcat-reptext_ddic = 'KEY'.
 ls_fieldcat-outputlen    = 10.
 APPEND ls_fieldcat TO e01_lt_fieldcat.

* Test number
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = '2'.
 ls_fieldcat-tabname      = 'GT_DATA'.
 ls_fieldcat-reptext_ddic = 'Ord#'.
 ls_fieldcat-emphasize    = 'C611'.  " red
 ls_fieldcat-outputlen    = 5.
 APPEND ls_fieldcat TO e01_lt_fieldcat.

* Text
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = 'TEXT'.
 ls_fieldcat-tabname      = 'GT_DATA'.
 ls_fieldcat-reptext_ddic = 'Text'.
 ls_fieldcat-outputlen    = 40.
 APPEND ls_fieldcat TO e01_lt_fieldcat.
ENDFORM.                    "e01_fieldcat_init

*---------------------------------------------------------------------*
*       FORM SELECTION                                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM selection.
 gt_data-key  = 'Group 1'.  gt_data-2 = '19734'. gt_data-text = 'This is group 1'.
APPEND gt_data.
 gt_data-key  = 'Group 2'.                       gt_data-text = 'This is group 2'.
APPEND gt_data.

ENDFORM.                    "selection

 

 

 

 

 

 

 

ALV的颜色设置分为3:行、列、单元格。
1.
列颜色的设置
slis_t_fieldcat_alv-emphasize 中,写入需要的颜色代码。
Eg:
DATA: fc TYPE slis_t_fieldcat_alv WITH HEADER LINE.
        fc-tabname = 'ITAB'.
         fc-fieldname = ' COL '.
         fc-emphasize = 'C100'.
         append fc.

2.
行颜色的设置
a.
在要输出的 itab 中,加入 color 列,类型为C4),记录需要的颜色代码。
b.
设置 layoutalv_layout-info_fieldname = 'COLOR'.
Eg: DATA: BEGIN OF itab OCCURS 0.
   INCLUDE STRUCTURE mara.
    DATA: color(4) TYPE c.
    DATA: END OF itab.

    DATA: alv_layout TYPE slis_layout_alv.
      
    itab-matnr = '123'.
    itab-color = 'C200'.
    APPEND itab.

    alv_layout-info_fieldname = 'COLOR'.  

3.
单元格颜色设置
a.
在要输出的 itab 中,加入 color 列,类型为 slis_t_specialcol_alv,记录需要的颜色代码。
b.
设置 layoutalv_layout-coltab_fieldname = 'COLOR'.

Eg: DATA: BEGIN OF itab OCCURS 0.
   INCLUDE STRUCTURE mara.
    DATA: color TYPE slis_t_specialcol_alv.
    DATA: END OF itab.

    DATA: color TYPE slis_t_specialcol_alv.      
    DATA: alv_layout TYPE slis_layout_alv.

    color-color-col = '1'.
    color-color-int = '0'.
    color-color-inv = '0'.
    color-fieldname = ' COL '.
    APPEND color.
      
    itab-matnr = '123'.
    itab-color[] = color[].
    APPEND itab.

    alv_layout-coltab_fieldname = 'COLOR'.

以上是我对ALV GRID着色方法的一个总结,由于技术水平有限可能还存在一些遗漏,还望大家指出。

事例代码:

REPORT ztest.

"双击单元格,可进入新的ALV画面.

TYPE-POOLS: slis.

*一共定义3个内表.
*itab1 :
设置列颜色,
*itab2 :
设置行颜色.
*itab3 :
设置单元格颜色.

DATA: BEGIN OF itab_test OCCURS 0,
        col1(4) TYPE c,
        col2(4) TYPE c,
        col3(4) TYPE c,
        col4(4) TYPE c,
        col5(4) TYPE c,
        col6(4) TYPE c,
      END OF itab_test .

DATA: BEGIN OF itab_test2 OCCURS 0.
        INCLUDE STRUCTURE itab_test.
DATA: color(4) TYPE c.
DATA: END OF itab_test2.

DATA: BEGIN OF itab_test3 OCCURS 0.
        INCLUDE STRUCTURE itab_test.
DATA: color TYPE slis_t_specialcol_alv.
DATA: END OF itab_test3.

DATA: fc TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      fc2 TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      fc3 TYPE slis_t_fieldcat_alv WITH HEADER LINE.

START-OF-SELECTION.
PERFORM create_data.

END-OF-SELECTION.
PERFORM fieldcat_build.
PERFORM show_alv.

*&---------------------------------------------------------------------*
*&      Form create_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_data.

DATA: l_color(4) TYPE c,
        l_char TYPE c,
        i TYPE i.
DATA: color TYPE slis_t_specialcol_alv WITH HEADER LINE.

REFRESH itab_test.
REFRESH itab_test2.
REFRESH itab_test3.
REFRESH fc.
REFRESH fc2.
REFRESH fc3.

WHILE sy-index < 8.
*
内表 1
    itab_test-col1 = ( sy-index - 1 ) * 6 + 1.
    itab_test-col2 = ( sy-index - 1 ) * 6 + 2.
    itab_test-col3 = ( sy-index - 1 ) * 6 + 3.
    itab_test-col4 = ( sy-index - 1 ) * 6 + 4.
    itab_test-col5 = ( sy-index - 1 ) * 6 + 5.
    itab_test-col6 = ( sy-index - 1 ) * 6 + 6.
    APPEND itab_test.

* 内表 2
    MOVE-CORRESPONDING itab_test TO itab_test2.
    l_char = sy-index.

    i = sy-index MOD 2.
    IF i = 0.
      CONCATENATE 'C' l_char '01' INTO l_color.
    ELSE.
      CONCATENATE 'C' l_char '10' INTO l_color.
    ENDIF.
    itab_test2-color = l_color.
    APPEND itab_test2.

* 内表 3
    MOVE-CORRESPONDING itab_test TO itab_test3.
    REFRESH color.

    color-color-col = l_char.

    color-color-int = '0'.
    color-color-inv = '0'.
    color-fieldname = 'COL1'. APPEND color.
    CONCATENATE l_char '00' INTO itab_test3-col1.

    color-color-int = '0'.
    color-color-inv = '1'.
    color-fieldname = 'COL2'. APPEND color.
    CONCATENATE l_char '01' INTO itab_test3-col2.

    color-color-int = '1'.
    color-color-inv = '0'.
    color-fieldname = 'COL3'. APPEND color.
    CONCATENATE l_char '10' INTO itab_test3-col3.

    itab_test3-color[] = color[].
    APPEND itab_test3.

ENDWHILE.

ENDFORM.                    "create_data

************************************************************************
FORM fieldcat_build.
DATA: BEGIN OF fc_struct,
          tabname(10),
          fieldname(5),
          seltext_m(5),
          emphasize(4),
        END OF fc_struct.
DEFINE ac.
    clear: fc,fc2,fc_struct.
    fc_struct = &1.
    fc-tabname   = fc_struct-tabname. "
内表名
    fc-fieldname = fc_struct-fieldname. "
字段名
    fc-seltext_m = fc_struct-seltext_m. "
字段描述
    fc-emphasize = fc_struct-emphasize. "
列颜色
    append fc.

    fc2-tabname   = fc_struct-tabname. "内表名
    fc2-fieldname = fc_struct-fieldname. "
字段名
    fc2-seltext_m = fc_struct-seltext_m. "
字段描述
    append fc2.

    if fc3-fieldname < 'COL4'.
      fc3-tabname   = fc_struct-tabname. "
内表名
      fc3-fieldname = fc_struct-fieldname. "
字段名
      fc3-seltext_m = fc_struct-seltext_m. "
字段描述
      append fc3.
    endif.
END-OF-DEFINITION.

ac 'ITAB_TEST COL1 COL 1 C 100'.
ac 'ITAB_TEST COL2 COL 2 C 200'.
ac 'ITAB_TEST COL3 COL 3 C 300'.
ac 'ITAB_TEST COL4 COL 4 C 400'.
ac 'ITAB_TEST COL5 COL 5 C 500'.
ac 'ITAB_TEST COL6 COL 6 C 600'.

ENDFORM.                    "fieldcat_build

*&---------------------------------------------------------------------*
*&      Form show_alv
*&---------------------------------------------------------------------*
*     
显示内表 1
*----------------------------------------------------------------------*
FORM show_alv.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      it_fieldcat             = fc[]
      i_callback_user_command = 'PROCESS_USER_COMMAND_1'
      i_save                  = 'A'
    TABLES
      t_outtab                = itab_test
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.

IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.                    "show_alv

*&---------------------------------------------------------------------*
*&      Form PROCESS_USER_COMMAND_1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->UCOMM        text
*      -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM process_user_command_1 USING ucomm
               rs_selfield TYPE slis_selfield.

IF rs_selfield-fieldname <> space AND rs_selfield-tabindex > 0.
    PERFORM show_alv_2.
ENDIF.

ENDFORM.                    "PROCESS_USER_COMMAND_1

*&---------------------------------------------------------------------*
*&      Form show_alv_2
*&---------------------------------------------------------------------*
*      
显示内表 2
*----------------------------------------------------------------------*
FORM show_alv_2.
DATA: alv_layout       TYPE slis_layout_alv.

alv_layout-info_fieldname = 'COLOR'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      it_fieldcat             = fc2[]
      i_callback_user_command = 'PROCESS_USER_COMMAND_2'
      is_layout               = alv_layout
      i_save                  = 'U'
    TABLES
      t_outtab                = itab_test2
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.

IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.                                                    "show_alv_2

*&---------------------------------------------------------------------*
*&      Form PROCESS_USER_COMMAND_2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->UCOMM        text
*      -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM process_user_command_2 USING ucomm
               rs_selfield TYPE slis_selfield.

IF rs_selfield-fieldname <> space AND rs_selfield-tabindex > 0.
    PERFORM show_alv_3.
ENDIF.

ENDFORM.                    "PROCESS_USER_COMMAND_2

*&---------------------------------------------------------------------*
*&      Form show_alv_3
*&---------------------------------------------------------------------*
*      
显示内表 3
*----------------------------------------------------------------------*
FORM show_alv_3.
DATA: alv_layout       TYPE slis_layout_alv.

alv_layout-coltab_fieldname = 'COLOR'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = fc3[]
      is_layout          = alv_layout
      i_save             = 'U'
    TABLES
      t_outtab           = itab_test3
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.

IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.                                                    "show_alv_3

 

 

 

 

 

alv行列颜色控制和双击感应

ALV中控制输出的颜色

* V_ALV_LAYOUT-ZEBRA = 'X'.
V_ALV_LAYOUT-INFO_FIELDNAME = 'COLOR'.
将上面第一行关闭,增加第二行代码。
在放输出数据的内表或工作区增加一个字段‘COLOR’

设定一个DATA :   LAYOUT    TYPE SLIS_LAYOUT_ALV.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 中     is_layout          = layout
给内表每行的COLOR字段放不同的颜色代码,如C110C310等。条件自己写,输出时就会有不同颜色。

列颜色控制比较简单,见彩色部分:

FORM FILL_FIELD_CATALOG_TABLE USING F T O D H TL .

DATA L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.

CLEAR L_FIELDCAT.

STATICS POS LIKE SY-INDEX VALUE 20.

POS = POS + 1.

CLEAR T_FLD.

MOVE 1 TO T_FLD-ROW_POS.

MOVE POS TO T_FLD-COL_POS.

MOVE F TO L_FIELDCAT-FIELDNAME.

MOVE T TO L_FIELDCAT-TABNAME.

MOVE O TO L_FIELDCAT-OUTPUTLEN.

MOVE D TO L_FIELDCAT-SELTEXT_M.

MOVE H TO L_FIELDCAT-HOTSPOT."设定固定列  

MOVE TL TO L_FIELDCAT-EMPHASIZE."设定颜色

APPEND L_FIELDCAT TO FIELDCAT.

ENDFORM.                    " FILL_FIELD_CATALOG_TABLE

双击alv_grid后显示当前行数据的明细数据的方法:


call function 'REUSE_ALV_GRID_DISPLAY'
   exporting
         i_callback_program    = gd_repid
*          i_callback_top_of_page = 'TOP-OF-PAGE'   "see FORM  
                        "
自定义alv_grid top_of_page事件
*          i_callback_user_command = 'USER_COMMAND'
                        "
自定义alv_grid的用户事件
*          i_grid_title           = outtext
         is_layout             = gd_layout
         it_fieldcat          = fieldcatalog[]
*          it_special_groups    = gd_tabgroup
*          IT_EVENTS             = GT_XEVENTS
                            "
将用户自定义事件添加到alv_grid的事件中
         i_save                = 'X'
*          is_variant              = z_template

   tables
         t_outtab             = it_ekko
   exceptions
         program_error           = 1
         others                = 2.
1
去掉   i_callback_user_command ='USER_COMMAND'    *          IT_EVENTS = GT_XEVENTS的注释,  然后自定义事件user_command,  user_command 事件添加到 gt_xevents.
2
user_command中可以得到it_ekko的当前行号, 这样就可以取得这一行的数据了, 然后根据关键字得到明细数据, 再调用 reuse_alv_grid_display 函数,就可以将明细数据显示在一个新的alv_grid.
这种方法多次调用就可以实现多层次的alv_grid中显示明细数据.

type-pools: slis.

data: is_layout TYPE slis_layout_alv.

DATA: BEGIN OF list_dtl OCCURS 0.

        INCLUDE STRUCTURE itab.

DATA:   color TYPE slis_t_specialcol_alv,

      END OF list_dtl.

initialization.

is_layout-coltab_fieldname = 'COLOR'.

start-of-selection.

DATA: linecolor TYPE slis_specialcol_alv OCCURS 0 WITH HEADER LINE.

LOOP AT list_dtl.

    REFRESH linecolor.

    CLEAR linecolor.

    linecolor-color-col = 1.

    linecolor-fieldname = 'VBELN'.

    APPEND linecolor.

    list_dtl-color[] = linecolor[].

    modify list_dtl.

ENDLOOP.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

       EXPORTING

            i_callback_program       = t_repid

            i_callback_pf_status_set = u_status

            i_callback_user_command = u_command

            is_layout                = is_layout

            it_fieldcat              = it_fieldcat[]

       TABLES

            t_outtab                 = list_dtl

       EXCEPTIONS

            program_error            = 1

            OTHERS                   = 2.

ALV GRID的颜色

ALV的颜色分为行的颜色、列的颜色和CELL的颜色。

1.行的颜色

    需要在要显示的内表中添加一个字段放置颜色信息,比如COLOR(4) TYPE C .

    ITAB-COLOR = 'C700'.

    此外,还要根据采用的FM方式还是CLASS方式设置一下显示颜色

·        FM方式: LS_LAYOUT-INFO_FIELDNAME = 'COLOR'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        IS_LAYOUT     = LS_LAYOUT
        IT_FIELDCAT = LT_FIELDCAT
      TABLES
        T_OUTTAB      = ITAB.

·        CLASS方式:  SLA-INFO_FNAME = 'COLOR'.
        CALL METHOD GO_GRID->SET_TABLE_FOR_FIRST_DISPLAY
        EXPORTING
           IS_VARIANT                      = GS_VARIANT
           I_SAVE                          = 'A'
   IS_LAYOUT                       = SLA
  CHANGING
           IT_OUTTAB                       = ITAB[]
           IT_FIELDCATALOG                 = GT_FIELDCAT[]
        EXCEPTIONS
           INVALID_PARAMETER_COMBINATION = 1
           PROGRAM_ERROR                   = 2
           TOO_MANY_LINES                  = 3
           OTHERS                          = 4.

2.列的颜色
设置FIELDCAT就可以了,如:TMP_FIELDCAT-EMPHASIZE = 'C500'.  "列的颜色

3.CELL的颜色
需要在要显示的内表添加一个字段:cellcolors TYPE lvc_t_scol.
  ls_cellcolor-color-col = '5' .
  ls_cellcolor-color-int = '1' .
  ls_cellcolor-color-inv = '1' .
  APPEND ls_cellcolor TO list-cellcolors .
  APPEND list.

 

例子:

REPORT  Z_BARRY_ALV_CL_COLOR2                     .

TYPES: BEGIN OF ilist_s ,
        name1(10),
        name2(10),
        name3(10),
 END OF ilist_s.
DATA: ilist TYPE STANDARD TABLE OF ilist_s  WITH HEADER LINE .

DATA: BEGIN OF list OCCURS 0.
          INCLUDE STRUCTURE ilist .
DATA: rowcolor(4) TYPE c ,
        cellcolors TYPE lvc_t_scol ,
        cellstyles TYPE lvc_t_styl ,
        celltext     TYPE alv_t_dtv .
DATA: END OF list.

DATA: ok_code LIKE sy-ucomm.
DATA: l_valid(1) TYPE c.
DATA: go_grid               TYPE REF TO cl_gui_alv_grid.

DATA: gt_fieldcat TYPE lvc_t_fcat,
        gs_fieldcat TYPE lvc_s_fcat,
        sla           TYPE lvc_s_layo.
DATA: ls_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

DATA: event_receiver TYPE REF TO lcl_event_receiver.
DATA: ii TYPE i .

START-OF-SELECTION.

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

list-name1 = 'A'.
  list-name2 = 'ARE'.
  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 = 'AA'.
  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 = 'HOW'.
  list-name2 = 'ARE'.
  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.

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

WRITE 'a'.
  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.
*&---------------------------------------------------------------------*
*&        Form  test
*&---------------------------------------------------------------------*
FORM test  USING      p_es_col_id TYPE lvc_s_col
                      p_es_row_no TYPE lvc_s_roid.
  MESSAGE i899(fi) WITH p_es_col_id-fieldname p_es_row_no-row_id .
ENDFORM.                      " test

 

 

 

 

 

ALV颜色&按钮

*&---------------------------------------------------------------------*
*& Report Z_BARRY_ALV_CL_COLOR2                                       *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT Z_BARRY_ALV_CL_COLOR2                   .

TYPES: BEGIN OF ilist_s ,
      name1(10),
      name2(10),
      name3(10),
END OF ilist_s.
DATA: ilist TYPE STANDARD TABLE OF ilist_s WITH HEADER LINE .

DATA: BEGIN OF list OCCURS 0.
        INCLUDE STRUCTURE ilist .
DATA: rowcolor(4) TYPE c ,
      cellcolors TYPE lvc_t_scol ,
      cellstyles TYPE lvc_t_styl ,
      celltext   TYPE alv_t_dtv .
DATA: END OF list.

DATA: ok_code LIKE sy-ucomm.
DATA: l_valid(1) TYPE c.
DATA: go_grid             TYPE REF TO cl_gui_alv_grid.

DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      sla         TYPE lvc_s_layo.
DATA: ls_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

DATA: event_receiver TYPE REF TO lcl_event_receiver.
DATA: ii TYPE i .

START-OF-SELECTION.

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

list-name1 = 'A'.
list-name2 = 'ARE'.
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 = 'AA'.
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 = 'HOW'.
list-name2 = 'ARE'.
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.

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

WRITE 'a'.
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.
*&---------------------------------------------------------------------*
*&      Form test
*&---------------------------------------------------------------------*
FORM test USING    p_es_col_id TYPE lvc_s_col
                    p_es_row_no TYPE lvc_s_roid.
MESSAGE i899(fi) WITH p_es_col_id-fieldname p_es_row_no-row_id .
ENDFORM.                    " test

你可能感兴趣的:(ALV中背景和字体颜色改变的做法)