Add row color in ALV using CL_SALV_TABLE

1
22771

In this tutorial, we will learn how to add color to the row of an ALV using CL_SALV_TABLE.In our earlier we applied colors to the columns,click here to know how.

This is similar to Functional Module ALV, we need an extra column in the output internal table to apply colors to a particular row (or) particular cell. Lets see how we can do it.

To apply colors to row in ALV

  • Create a new column of Type LVC_T_SCOL in your output internal table.
  • Set the above column as Color Column in ALV by using the method SET_COLUMN_COLOR( ).
  • Add the color data to the color column based on your requirement.

Create a program in SE38 and copy the below code.

*&---------------------------------------------------------------------*
*&                  Add color to rows in ALV report                    *
*&---------------------------------------------------------------------*
*&                  www.saplearners.com                                *
*&---------------------------------------------------------------------*
REPORT  zsl_oop_alv_row_color.

*----------------------------------------------------------------------*
*       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,
           color           TYPE lvc_t_scol, "<<< COLOR COLUMN 
           END OF lty_sflight.

    METHODS: get_sflight_data,
             get_alv_instance,
             set_color,
             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 CORRESPONDING FIELDS OF 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
  METHOD set_color.
* Set color to a particular row based on your condition.
    FIELD-SYMBOLS: <lwa_sflight> TYPE lty_sflight.
    DATA: ls_color               TYPE lvc_s_scol.

    LOOP AT gt_sflight ASSIGNING <lwa_sflight>.
      IF <lwa_sflight>-seatsocc > 370.
        ls_color-color-col = 5.
        ls_color-color-int = 0.
        ls_color-color-inv = 0.
        APPEND ls_color TO <lwa_sflight>-color.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.                    "set_color
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.


  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 a ROW in ALV ---------------------*
*// 1. Get List of columns
  CALL METHOD lo_cl_sflight->lo_alv->get_columns
    RECEIVING
      value = lo_columns.

*// 2. Set the Color Column to the ALV
  TRY.
      CALL METHOD lo_columns->set_color_column
        EXPORTING
          value = 'COLOR'.
    CATCH cx_salv_data_error.
  ENDTRY.

*// 3. Add color to particular row
  lo_cl_sflight->set_color( ).

* Display ALV report
  lo_cl_sflight->display( ).

Output before:

alv2

Output after adding colors to row:

alv1

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

If you liked it, please share it! Thanks!

Like us on Facebook and follow us on Twitter.

mail

SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.

Comments are closed.