Line color in ALV

2009 May 18
by Tresna

Using colors in ALV List or ALV Grid has it’s advantages, it could be very useful to increase readability of report and it is very easy to implement if you are already familiar with programming ALV.

There are only two specific codes to add and the rest are your usual ALV codes. First, add a character field to internal table structure that is going to be displayed in the ALV for storing color attribute.

DATA: BEGIN OF it_alv OCCURS 0,
        color(4),        "color line
        field1,
        field2,
      END OF it_alv.

The color is represented in four characters with following details:

  • 1st char = ‘C’
  • 2nd char = Color code (1-7)
  • 3rd char = Intensified on/off (1 or 0)
  • 4th char = Inverse on/off (1 or 0)

See ABAP Keyword Documentation for color code reference.

And the second one, add following to your ALV layout attributes

DATA: v_layout TYPE slis_layout_alv.

v_layout-INFO_FIELDNAME       = 'COLOR'.
v_layout-F2CODE               = '&ETA'.

Read more to see a complete example of how to set line color in ALV.  In this example we will use ALV to output list of reservations and highlight open reservations, finished items, and deleted items with different colors.

TYPE-POOLS : SLIS.

DATA: BEGIN OF it_resb OCCURS 0,
	rsnum like resb-rsnum,	"reservation number
	rspos like resb-rsnum,	"item number
	xloek like resb-xloek,	"is deleted
	kzear like resb-kzear,	"final issue
	matnr like resb-matnr,	"material number
	werks like resb-werks,	"plant
	lgort like resb-lgort,  "storage location
	bdmng like resb-bdmng,	"requirement quantity
	meins like resb-meins,  "unit of measure
	color(4),		"color line
      END OF it_resb.

DATA: v_layout 	     TYPE slis_layout_alv,	"alv layout
      it_fieldcat    TYPE slis_t_fieldcat_alv,	"catalog
      wa_fieldcat    TYPE slis_fieldcat_alv,
      wa_resb 	     LIKE it_resb.

DATA: exit_caused_by_caller,
      exit_caused_by_user TYPE slis_exit_by_user.

* Get data from table
SELECT rsnum rspos xloek kzear matnr werks lgort bdmng meins
  INTO TABLE it_resb
  FROM resb
 WHERE bdart = 'MR'.

* Set color with red for deleted items
wa_resb-color = 'C610'.
MODIFY it_resb FROM wa_resb TRANSPORTING color
 WHERE xloek = 'X'.

* Set color with green for final issue items
wa_resb-color = 'C510'.
MODIFY it_resb FROM wa_resb TRANSPORTING color
 WHERE kzear = 'X'.

* Build ALV Catalog
wa_fieldcat-tabname = 'IT_RESB'.
wa_fieldcat-ref_tabname = 'RESB'.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'RSNUM'.
wa_fieldcat-ddictxt = 'M'.
wa_fieldcat-seltext_l = 'Reservation'.
wa_fieldcat-outputlen = 11.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'RSPOS'.
wa_fieldcat-ddictxt = 'S'.
wa_fieldcat-seltext_l = 'Item'.
wa_fieldcat-outputlen = 4.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'MATNR'.
wa_fieldcat-ddictxt = 'M'.
wa_fieldcat-seltext_l = 'Material'.
wa_fieldcat-outputlen = 10.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'WERKS'.
wa_fieldcat-ddictxt = 'S'.
wa_fieldcat-seltext_l = 'Plant'.
wa_fieldcat-outputlen = 5.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'LGORT'.
wa_fieldcat-ddictxt = 'S'.
wa_fieldcat-seltext_l = 'SLoc'.
wa_fieldcat-outputlen = 5.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'BDMNG'.
wa_fieldcat-ddictxt = 'M'.
wa_fieldcat-seltext_l = 'Quantity'.
wa_fieldcat-outputlen = 10.
wa_fieldcat-cfieldname = 'MEINS'.
wa_fieldcat-ctabname   = 'IT_RESB'.
APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat-cfieldname.
CLEAR wa_fieldcat-ctabname.
wa_fieldcat-fieldname = wa_fieldcat-ref_fieldname = 'MEINS'.
wa_fieldcat-ddictxt = 'M'.
wa_fieldcat-seltext_l = 'UoM'.
wa_fieldcat-outputlen = 5.
APPEND wa_fieldcat TO it_fieldcat.

* Set ALV Layout
* for coloring purpose
v_layout-INFO_FIELDNAME       = 'COLOR'.
v_layout-F2CODE               = '&ETA'.

*Display ALV Grid
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            is_layout               = v_layout
            it_fieldcat             = it_fieldcat[]
       IMPORTING
            e_exit_caused_by_caller = exit_caused_by_caller
            es_exit_caused_by_user  = exit_caused_by_user
       TABLES
            t_outtab                = it_resb
       EXCEPTIONS
            program_error           = 1
            OTHERS                  = 2.
  IF sy-subrc NE 0.
    MESSAGE E002(SY) WITH 'Error when calling ALV'.
  ENDIF.
2 Responses leave one →
  1. 2009 June 7

    Hey,
    please keep posting frequently :)
    i would like to add your site to my SAP Sites directory under ABAP category at http://social.sapdocs.info
    Cheers~

  2. 2009 August 3
    Endi permalink

    Hi Tresna,

    Thanks, this is very useful.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS