SALV Table 9 – Apply Colors

By | October 7, 2008 | SALV Table, SALV Tutorial, Tutorials | 77,663 | 2

In the series of the SALV Model Table display, today we will see how to apply colors to the ALV Grid. In this regards, we will see how to apply colors to the Specific Cell, Specific Row or Specific Column. You can find all the Previous discussion at Tutorials > SALV Table Display .

Color plays a very important role in the formatting. It will help us to generate the a rich list which can help users to notice the exceptional data – for example, negative salary to employee or Low material availabilty.

At most colors can be applied to three levels:

  1. Perticular Cell: To apply color at perticular cell, we need to add the details about the field and color in COLOR table at each record. Than we have to set this Color Table in the object of the COLUMNS which contains the information about all our information (CL_SALV_COLUMNS_TABLE).
  2. Entire Row: To apply color to Entire row, we have to do the same thing as the applying the color to Perticular Cell, but we will not specify the FIELDNAME. This way system will understand that it has to apply the color to entire row.
  3. Entire Column: To apply color to Entire column, we have to get the specific Column from the COLUMNS object and than we need to set the Color Property of the Specific Column and we are done.

In this example I need to change the output table sturcture as compared to previous discussion in this series, so I will provide the entire code which generate the output which has all three scenario. For test purpose, We will apply: Red color to Sales Doc Type in 3rd Row, Green Color to 5th row, Yellow color to Created on Column.

UML Diagram for this application is like:

Code snippet to generate the ALV with Colors using the SALV model.

*&---------------------------------------------------------------------*
*& This code snippet will show how to use the CL_SALV_TABLE to
*&   generate the ALV and COLORs for Column, Row and Specific Cell
*&---------------------------------------------------------------------*
REPORT  ztest_oo_alv_color.
*
*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
  PUBLIC SECTION.
*
*   Final output table
    TYPESBEGIN OF ty_vbak,
           vbeln     TYPE vbak-vbeln,
           erdat     TYPE erdat,
           auart     TYPE auart,
           kunnr     TYPE kunnr,
           t_color   TYPE lvc_t_scol,
           END   OF ty_vbak.
    TYPESty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
    DATAt_vbak TYPE STANDARD TABLE OF ty_vbak.
*
*   ALV reference
    DATAo_alv TYPE REF TO cl_salv_table.
*
    METHODS:
*     data selection
      get_data,
*
*     Generating output
      generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
*    In this section we will define the private methods which can
*      be implemented to set the properties of the ALV and can be
*      called in the
*
  PRIVATE SECTION.
    METHODS:
      set_pf_status
        CHANGING
          co_alv TYPE REF TO cl_salv_table.
*
    METHODS:
      set_colors
        CHANGING
          co_alv  TYPE REF TO cl_salv_table
          ct_vbak TYPE ty_t_vbak.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS.                    "lcl_report DEFINITION
*
*
START-OF-SELECTION.
  DATAlo_report TYPE REF TO lcl_report.
*
  CREATE OBJECT lo_report.
*
  lo_report->get_data).
*
  lo_report->generate_output).
*
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
  METHOD get_data.
*   data selection
    SELECT vbeln erdat auart kunnr
           INTO  CORRESPONDING FIELDS OF TABLE t_vbak
           FROM  vbak
           UP TO 20 ROWS.
*
  ENDMETHOD.                    "get_data
*
*.......................................................................
  METHOD generate_output.
* New ALV instance
*   We are calling the static Factory method which will give back
*   the ALV object reference.
*
* exception class
    DATAlx_msg TYPE REF TO cx_salv_msg.
    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table o_alv
          CHANGING
            t_table      t_vbak ).
      CATCH cx_salv_msg INTO lx_msg.
    ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
*    In this area we will call the methods which will set the
*      different properties to the ALV
*
*   Set default PF status
    CALL METHOD set_pf_status
      CHANGING
        co_alv o_alv.
*
*   Set the colors to ALV display
    CALL METHOD set_colors
      CHANGING
        co_alv  o_alv
        ct_vbak t_vbak.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen
    o_alv->display).
*
  ENDMETHOD.                    "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
*    In this area we will implement the methods which are defined in
*      the class definition
*
*
  METHOD set_pf_status.
*
    DATAlo_functions TYPE REF TO cl_salv_functions_list.
*
    lo_functions co_alv->get_functions).
    lo_functions->set_defaultabap_true ).
*
  ENDMETHOD.                    "set_pf_status
*
  METHOD set_colors.
*
*.....Color for COLUMN.....
    DATAlo_cols_tab TYPE REF TO cl_salv_columns_table,
          lo_col_tab  TYPE REF TO cl_salv_column_table.
    DATAls_color TYPE lvc_s_colo.    " Colors strucutre
*
*   get Columns object
    lo_cols_tab co_alv->get_columns).
*
    INCLUDE <color>.
*
*   Get ERDAT column & set the yellow Color fot it
    TRY.
        lo_col_tab ?= lo_cols_tab->get_column'ERDAT' ).
        ls_color-col col_total.
        lo_col_tab->set_colorls_color ).
      CATCH cx_salv_not_found.
    ENDTRY.
*
*.......Color for Specific Cell & Rows.................
*   Applying color on the 3rd Row and Column AUART
*   Applying color on the Entire 5th Row
*
    DATAlt_s_color TYPE lvc_t_scol,
          ls_s_color TYPE lvc_s_scol,
          la_vbak    LIKE LINE OF ct_vbak,
          l_count    TYPE i.
*
    LOOP AT ct_vbak INTO la_vbak.
      l_count l_count + 1.
      CASE l_count.
*       Apply RED color to the AUART Cell of the 3rd Row
        WHEN 3.
          ls_s_color-fname     'AUART'.
          ls_s_color-color-col col_negative.
          ls_s_color-color-int 0.
          ls_s_color-color-inv 0.
          APPEND ls_s_color TO lt_s_color.
          CLEAR  ls_s_color.
*
*       Apply GREEN color to the entire row # 5
*         For entire row, we don't pass the Fieldname
        WHEN 5.
          ls_s_color-color-col col_positive.
          ls_s_color-color-int 0.
          ls_s_color-color-inv 0.
          APPEND ls_s_color TO lt_s_color.
          CLEAR  ls_s_color.
      ENDCASE.
*     Modify that data back to the output table
      la_vbak-t_color lt_s_color.
      MODIFY ct_vbak FROM la_vbak.
      CLEAR  la_vbak.
      CLEAR  lt_s_color.
    ENDLOOP.
*
*   We will set this COLOR table field name of the internal table to
*   COLUMNS tab reference for the specific colors
    TRY.
        lo_cols_tab->set_color_column'T_COLOR' ).
      CATCH cx_salv_data_error.                         "#EC NO_HANDLER
    ENDTRY.
*
  ENDMETHOD.                    "set_colors
*
*
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS.                    "lcl_report IMPLEMENTATION

Without Colors it will look like:

With Colors it will look like:

Related Links:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

2 Comments

  • I really like your exception handling. Very staightfoward.

  • Raj

    We created structure with table types as the components and populate each.
    We create a table-type of this structure.
    So now we have a table-of-tables. Is it possible to use cl_salv_table=>factory to display the table-of-tables?

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.