Friday, May 23, 2008

Changing Color of ALV GRID in SAP ABAP

Changing Color of ALV GRID in SAP ABAP

The following program shows how to color individual rows of ALV Grid. This is just a demonstration and data is not retrieved from SAP tables.

REPORT ZEX_ALV1 .

*&---------------------------------------------------------------------*
*& ABAPLOVERS ALV GRID COLOR DEMO                                      *
*&                                                                     *
*&---------------------------------------------------------------------*

type-pools: slis.                                 "ALV Declarations

*Data Declaration------------------------------------------------------*
*Types
TYPES: BEGIN OF ty_one,
  Column1(10),
  Column2(10),
  Column3(10),
  Column4(10),
  line_color(4) type c,     "For storing Row Color Attributes
 END OF ty_one.
* Internal Table Declaration-------------------------------------------*
DATA: int_tab TYPE STANDARD TABLE OF ty_one INITIAL SIZE 0.

* Work Area Declaration------------------------------------------------*
DATA:  wa_one TYPE ty_one.

*ALV data declarations
data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_tab_group type slis_t_sp_group_alv,
      gd_layout    type slis_layout_alv,
      gd_repid     like sy-repid.



*Start-of-selection---------------------------------------------------*.
START-OF-SELECTION.
* Select the desired data. In this example we will not select anything

* Buils Table
perform build_table.

* Build the Catelog---------------------------------------------------*
perform build_fieldcatalog.

* Build the Layout----------------------------------------------------*
perform build_layout.

* Display the ALV-----------------------------------------------------*
perform display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
form build_fieldcatalog.

  fieldcatalog-fieldname    = 'Column1'.
  fieldcatalog-seltext_m    = 'Col1'.
  fieldcatalog-col_pos      = 0.
  fieldcatalog-outputlen    = 10.
  fieldcatalog-emphasize    = 'X'.
  fieldcatalog-key          = 'X'.

  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'Column2'.
  fieldcatalog-seltext_m   = 'Col2'.
  fieldcatalog-col_pos     = 1.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'Column3'.
  fieldcatalog-seltext_m   = 'Col3'.
  fieldcatalog-col_pos     = 2.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'Column4'.
  fieldcatalog-seltext_m   = 'COl4'.
  fieldcatalog-col_pos     = 3.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'Column5'.
  fieldcatalog-seltext_m   = 'Col5'.
  fieldcatalog-col_pos     = 4.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

endform.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-totals_text       = 'Totals'(201).

* Set layout field for color attributes
  gd_layout-info_fieldname =      'LINE_COLOR'.

endform.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
  gd_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
             i_callback_program      = gd_repid
*            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
*            i_callback_user_command = 'USER_COMMAND'
*            i_grid_title           = outtext
             is_layout               = gd_layout
             it_fieldcat             = fieldcatalog[]
*            it_special_groups       = gd_tabgroup
*            IT_EVENTS                = GT_XEVENTS
             i_save                  = 'X'
*            is_variant              = z_template

       tables
            t_outtab                = int_tab
       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.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*

*----------------------------------------------------------------------*
form Build_Table.
data: ld_color(1) type c.

Move: ' ' to wa_one.
Append wa_one to int_tab.
Append wa_one to int_tab.
Append wa_one to int_tab.
Append wa_one to int_tab.
Append wa_one to int_tab.

*Populate field with color attributes
loop at int_tab into wa_one.
* Populate color variable with colour properties
* Char 1 = C (This is a color property)
* Char 2 = 3 (Color codes: 1 - 7)
* Char 3 = Intensified on/off ( 1 or 0 )
* Char 4 = Inverse display on/off ( 1 or 0 )
*           i.e. wa_ekko-line_color = 'C410'
  ld_color = ld_color + 1.

* Only 7 colours so need to reset color value
  if ld_color = 8.
    ld_color = 1.
  endif.
  concatenate 'C' ld_color '10' into wa_one-line_color.
  modify int_tab from wa_one.
endloop.
endform.                    "