Saturday, 19 March 2011

SALV Table 9 - Apply Colors

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:
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-6qLXmDGIGREgB4KMTSrBLSuqstYs5J5j_KgcPpFZfF6auUcc6r2KCnRKKrFPUzFr7_k2jFldFRtqAhNe5W3ufCkOuPibAMSLsvf34hBy6PInYxoMWFmOXWiz8dScvKGd6j4r-FtgnHTS/s400/SALV_Colors.jpg

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

Code Snippet to Generate ALV with Colors

 
*&---------------------------------------------------------------------*
*& 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 Column
        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:
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhP7K5lUEFZ0P9NYEfrJaHYZ2Q3NmI-RC2pXpGfYVLxCkaChBslRs-Uw_WuJBj8YtmxzECVocrHishUpPYNjmELMfSB4vQVoVa48Wa6WSQ-AoLLpHQgXfKolVUVZ-mPpBKyB0yqz-oClA-U/s400/OO_SALV_Colors_wo.png

With Colors it will look like:
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ4832casu9MyIe1mymT0ms80Md-fgdX-0YV1MtpGMFtZw0FBwh2Tw70tQiwiRNn4tqTxAtnv1TuISqufeBa8yFgsnvv7aSbfkAOJRtwLVeHcVOZqhLWgjcobZz2ycUEfUEUKz7XTATybF/s400/OO_SALV_Colors.png

Related Links:

 

 

 

 

 

 

 

 

rated 5.0 by 4 people [?]

 

Description: http://help-abap.blogspot.com/2008/10/salv-table-9-apply-colors.html

 

SALV Hierarchical Table 3 - Add Expand/Collapse Option

Today in the series of the SALV Hiearchical list display, we will see how to add the Expand/Collapse button in the Hierarchical ALV. All discussion related to the Hierarchical table display can be found under Tutorials > SALV HS List.

Expand/Collapse button will provide an option to users to only see the Header List. By selecting the expand button, it will show both Header and detail List. By selecting the same button again it will collapse the list and gives only the header list.

Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with the relevent section from this code snippet in the base program. You can find the base program code snippet in the post SALV Hierarchical Table 1 : Simple table display. Adding code to base program is like adding the code correction from the OSS Note. Additionally, we will always include the default PF-status in all the code snippet.

UML diagram for the application would be like:
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQMPGEZ5HHxwrUsZXwNZePGuVgNRP9iTZNlxy1gXRFu_8TnRiT9ahZj4EcBxRzh86bBpQ0LB6BbUX9u_wI6G6fGjwtPKF9H8Y582-RKsBZ2nylE9DydbzX-rUYwd6Hx6DCAOGL8UyRF-E8/s400/SALV_HS_Expand.uxf.jpg

Code snippet to get the Expand / Collapse option in Hierarchical ALV.

Code Snippet to get Expand / Collapse option

*$*$*.....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 GENERATE_OUTPUT method
*
  PRIVATE SECTION.
    METHODS:
      set_default_pf
        CHANGING
          co_hs_alv TYPE REF TO cl_salv_hierseq_table.
*
    METHODS:
      set_expand_option
        CHANGING
          co_hs_alv TYPE REF TO cl_salv_hierseq_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
*    In this area we will call the methods which will set the
*      different properties to the ALV
*
*   Default PF status
    CALL METHOD me->set_default_pf
      CHANGING
        co_hs_alv = o_hs_alv.
*
*   Expand Option
    CALL METHOD me->set_expand_option
      CHANGING
        co_hs_alv = o_hs_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
*    In this area we will implement the methods which are defined in
*      the class definition
*
  METHOD set_default_pf.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    lo_functions = co_hs_alv->get_functions( ).
    lo_functions->set_all( abap_true ).
  ENDMETHOD.                    "set_default_pf
*
  METHOD set_expand_option.
*
    DATA:
      lo_columns TYPE REF TO cl_salv_columns_hierseq.
*
*   Get the Columns of the Master
    TRY.
        lo_columns = co_hs_alv->get_columns( 1 ).
      CATCH cx_salv_not_found.
    ENDTRY.
*
*   set expand column
    TRY.
        lo_columns->set_expand_column( 'EXPAND' ).
      CATCH cx_salv_data_error.                         "#EC NO_HANDLER
    ENDTRY.
*
    DATA: lo_level TYPE REF TO cl_salv_hierseq_level.
*
*   Set items expanded by default
    TRY.
        lo_level = co_hs_alv->get_level( 1 ).
        lo_level->set_items_expanded( ).
      CATCH cx_salv_not_found.
    ENDTRY.
*
  ENDMETHOD.                    "set_expand_option
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*



Adsense