Sep 19, 2016

Colors in ALV Grid Using Function Module 'REUSE_ALV_GRID_DISPLAY'


From sapdev.co.uk


Source code:

*&-------------------------------------------------------------*
*& Report  YALVCOL_GRID
*&
*&-------------------------------------------------------------*
*& Author: SAP Development
*& Demonstrates simple ALV GRID report along with setting colour of
*& individual cells/fields
*&-------------------------------------------------------------*

REPORT  yalvcol_grid                .

TABLES:     ekko.

TYPE-POOLSslis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPESBEGIN OF t_ekko,
         ebeln     
TYPE ekpo-ebeln,
         ebelp     
TYPE ekpo-ebelp,
         statu     
TYPE ekpo-statu,
         aedat     
TYPE ekpo-aedat,
         matnr     
TYPE ekpo-matnr,
         menge     
TYPE ekpo-menge,
         meins     
TYPE ekpo-meins,
         netpr     
TYPE ekpo-netpr,
         peinh     
TYPE ekpo-peinh,
         cellcolor 
TYPE lvc_t_scol,

       
END OF t_ekko.

DATAit_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko 
TYPE t_ekko.

*ALV data declarations
DATAfieldcatalog  TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group  
TYPE slis_t_sp_group_alv,
      gd_layout     
TYPE slis_layout_alv,
      gd_repid      
LIKE sy-repid,
      gt_events     
TYPE slis_t_event,
      gd_prntparams 
TYPE slis_print_alv.


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  
PERFORM data_retrieval.
  
PERFORM build_fieldcatalog.
  
PERFORM build_layout.
  
PERFORM set_cell_colours.
  
PERFORM display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.

* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you  more control of the final product.

* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
*               I.e. Field type may be required in-order for
*                    the 'TOTAL' function to work.

  fieldcatalog
-fieldname   'EBELN'.
  fieldcatalog
-seltext_m   'Purchase Order'.
  fieldcatalog
-col_pos     0.
  fieldcatalog
-outputlen   10.
  fieldcatalog
-emphasize   'X'.
  fieldcatalog
-key         'X'.
*  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = 'X'.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'EBELP'.
  fieldcatalog
-seltext_m   'PO Item'.
  fieldcatalog
-col_pos     1.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'STATU'.
  fieldcatalog
-seltext_m   'Status'.
  fieldcatalog
-col_pos     2.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'AEDAT'.
  fieldcatalog
-seltext_m   'Item change date'.
  fieldcatalog
-col_pos     3.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'MATNR'.
  fieldcatalog
-seltext_m   'Material Number'.
  fieldcatalog
-col_pos     4.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'MENGE'.
  fieldcatalog
-seltext_m   'PO quantity'.
  fieldcatalog
-col_pos     5.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'MEINS'.
  fieldcatalog
-seltext_m   'Order Unit'.
  fieldcatalog
-col_pos     6.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'NETPR'.
  fieldcatalog
-seltext_m   'Net Price'.
  fieldcatalog
-col_pos     7.
  fieldcatalog
-outputlen   15.
  fieldcatalog
-do_sum      'X'.
  fieldcatalog
-datatype     'CURR'.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   'PEINH'.
  fieldcatalog
-seltext_m   'Price Unit'.
  fieldcatalog
-col_pos     8.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.
ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gd_layout
-no_input          'X'.
  gd_layout
-colwidth_optimize 'X'.
  gd_layout
-totals_text       'Totals'(201).
  gd_layout
-coltab_fieldname 'CELLCOLOR'.  "CTAB_FNAME
ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM display_alv_report.
  gd_repid 
sy-repid.
  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    
EXPORTING
      i_callback_program     
gd_repid
      i_callback_top_of_page 
'TOP-OF-PAGE'  "see FORM
*     i_callback_user_command = 'USER_COMMAND'
*     i_grid_title           = outtext
      is_layout              
gd_layout
      it_fieldcat            
fieldcatalog[]
*     it_special_groups      = gd_tabgroup
*     it_events              = gt_events
*     is_print               = gd_prntparams
      i_save                 
'X'
*     is_variant             = z_template
    
TABLES
      t_outtab               
it_ekko
    
EXCEPTIONS
      program_error          
1
      
OTHERS                 2.
  
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  
ENDIF.
ENDFORM.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  
SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
   
UP TO 10 ROWS
    
FROM ekpo
    
INTO CORRESPONDING FIELDS OF TABLE it_ekko.
ENDFORM.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM top-of-page.
*ALV Header declarations
  
DATAt_header      TYPE slis_t_listheader,
        wa_header     
TYPE slis_listheader,
        t_line        
LIKE wa_header-info,
        ld_lines      
TYPE i,
        ld_linesc
(10TYPE c.

* Title
  wa_header
-typ  'H'.
  wa_header
-info 'EKKO Table Report'.
  
APPEND wa_header TO t_header.
  
CLEAR wa_header.

* Date
  wa_header
-typ  'S'.
  wa_header
-key 'Date: '.
  
CONCATENATE  sy-datum+6(2'.'
               sy
-datum+4(2'.'
               sy
-datum(4INTO wa_header-info.   "todays date
  
APPEND wa_header TO t_header.
  
CLEARwa_header.

* Total No. of Records Selected
  
DESCRIBE TABLE it_ekko LINES ld_lines.
  ld_linesc 
ld_lines.
  
CONCATENATE 'Total No. of Records Selected: ' ld_linesc
                    
INTO t_line SEPARATED BY space.
  wa_header
-typ  'A'.
  wa_header
-info t_line.
  
APPEND wa_header TO t_header.
  
CLEARwa_headert_line.

  
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    
EXPORTING
      it_list_commentary 
t_header.
*            i_logo             = 'Z_LOGO'.
ENDFORM.                    "top-of-page


*&---------------------------------------------------------------------*
*&      Form  SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*       Set colour of individual ALV cell, field
*----------------------------------------------------------------------*
FORM set_cell_colours .
  
DATAwa_cellcolor TYPE lvc_s_scol.
  
DATAld_index TYPE sy-tabix.

  
LOOP AT it_ekko INTO wa_ekko.
    ld_index 
sy-tabix.

*   Set colour of EBELN field to various colors based on sy-tabix value
    wa_cellcolor
-fname 'EBELN'.
    wa_cellcolor
-color-col sy-tabix.  "color code 1-7, if outside rage defaults to 7
    wa_cellcolor
-color-int '1'.  "1 = Intensified on, 0 = Intensified off
    wa_cellcolor
-color-inv '0'.  "1 = text colour, 0 = background colour
    
APPEND wa_cellcolor TO wa_ekko-cellcolor.
    
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.

*   Set colour of NETPR field to color 4 if gt 0
    
IF wa_ekko-netpr GT 0.
      wa_cellcolor
-fname 'NETPR'.
      wa_cellcolor
-color-col 4.  "color code 1-7, if outside rage defaults to 7
      wa_cellcolor
-color-int '0'.  "1 = Intensified on, 0 = Intensified off
      wa_cellcolor
-color-inv '0'.  "1 = text colour, 0 = background colour
      
APPEND wa_cellcolor TO wa_ekko-cellcolor.
      
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
    
ENDIF.

*   Set colour of AEDAT field text to red(6)
    wa_cellcolor
-fname 'AEDAT'.
    wa_cellcolor
-color-col 6.  "color code 1-7, if outside rage defaults to 7
    wa_cellcolor
-color-int '0'.  "1 = Intensified on, 0 = Intensified off
    wa_cellcolor
-color-inv '1'.  "1 = text colour, 0 = background colour
    
APPEND wa_cellcolor TO wa_ekko-cellcolor.
    
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
  
ENDLOOP.

ENDFORM.                    " SET_CELL_COLOURS