ALV color

*&---------------------------------------------------------------------* *& 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 TYPES: BEGIN OF ty_vbak, vbeln TYPE vbak-vbeln, erdat TYPE

*&---------------------------------------------------------------------* *& 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 TYPES: BEGIN OF ty_vbak, vbeln TYPE vbak-vbeln, erdat TYPE
*&---------------------------------------------------------------------*
*& 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
    TYPES: BEGIN 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.
    TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
    DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
*   ALV reference
    DATA: o_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.
  DATA: lo_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
    DATA: lx_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.
*
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
*
    lo_functions = co_alv->get_functions( ).
    lo_functions->set_default( abap_true ).
*
  ENDMETHOD.                    "set_pf_status
*
  METHOD set_colors.
*
*.....Color for COLUMN.....
    DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
          lo_col_tab  TYPE REF TO cl_salv_column_table.
    DATA: ls_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_color( ls_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
*
    DATA: lt_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