Add Column Color in ALV using CL_SALV_TABLE

0
11347

In this tutorial we will learn how to apply colors to a column in ALV report using CL_SALV_TABLE.

To apply column color,

  • Get the list of all columns of ALV using GET_COLUMNS().
  • Choose a particular column by calling the method GET_COLUMN().
  • Apply color by using the method SET_COLOR().

Classes used: CL_SALV_TABLE, CL_SALV_COLUMNS_TABLE, CL_SALV_COLUMN_TABLE.

Create a program in SE38 and copy the below code.

REPORT  zsl_oop_alv_row_color.
*----------------------------------------------------------------------*
*      Add colors to columns in ALV report using factory
*                 class CL_SALV_TABLE                                  *
*----------------------------------------------------------------------*
*                    www.saplearners.com                               *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
*       CLASS lcl_sflight DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight DEFINITION.

  PUBLIC SECTION.
    TYPES: BEGIN OF lty_sflight,
           carrid	         TYPE s_carr_id,
           connid	         TYPE s_conn_id,
           fldate	         TYPE s_date,
           price           TYPE s_price,
           currency	       TYPE s_currcode,
           planetype       TYPE s_planetye,
           seatsmax	       TYPE s_seatsmax,
           seatsocc	       TYPE s_seatsocc,
           END OF lty_sflight.

    METHODS: get_sflight_data,
             get_alv_instance,
             display.

    DATA: lo_alv      TYPE REF TO cl_salv_table,
          gt_sflight TYPE STANDARD TABLE OF lty_sflight.

ENDCLASS.                    "lcl_sflight DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_sflight IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight IMPLEMENTATION.

* Get SFLIGHT data
  METHOD get_sflight_data.
    SELECT carrid connid fldate price currency planetype seatsmax
           seatsocc INTO TABLE me->gt_sflight
                    FROM sflight.
  ENDMETHOD.                    "get_sflight_data

* Get ALV instance
  METHOD get_alv_instance.
    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = lo_alv
          CHANGING
            t_table      = gt_sflight.
      CATCH cx_salv_msg.
    ENDTRY.

  ENDMETHOD.                    "get_alv_instance

* Display ALV
  METHOD display.
    CALL METHOD lo_alv->display.
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_sflight IMPLEMENTATION



START-OF-SELECTION.
  DATA: lo_cl_sflight     TYPE REF TO lcl_sflight,
        lo_columns        TYPE REF TO cl_salv_columns_table,
        lo_column_price   TYPE REF TO cl_salv_column_table,
        ls_color          TYPE lvc_s_colo.

  CREATE OBJECT lo_cl_sflight.

* Get the Data for ALV report
  lo_cl_sflight->get_sflight_data( ).


* Get ALV instance
  lo_cl_sflight->get_alv_instance( ).


*// To apply color to the particular column in ALV
*// 1. Get List of columns
  CALL METHOD lo_cl_sflight->lo_alv->get_columns
    RECEIVING
      value = lo_columns.


*// 2. Pick the particular column to color it
  lo_column_price ?= lo_columns->get_column( 'PRICE' ).


*// 3. Apply Color
  ls_color-col = 3.
  ls_color-int = 1.
*ls_color-INV =
  CALL METHOD lo_column_price->set_color
    EXPORTING
      value = ls_color.


* Display ALV report
  lo_cl_sflight->display( ).

Output without Color:

alv1

Output with Color:

alv2

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!

Stay Connected