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



Colors in ALV List Using Function Module 'REUSE_ALV_LIST_DISPLAY'

By Haritha
From saptechnical.com


Colors in ALV List Using Function Module 'REUSE_ALV_LIST_DISPLAY'

Column and Row are colored with a coded color ‘Cxyz’.
    Where C: Color (coding must begin with C)
                 X: Color Number
                 Y: Bold
                 Z: Inverse.

The Selection Screen will be as follows.



In the Selection screen if we enter only the Column Number and color the whole column will be colored as follows.



In the Selection screen if we enter only the row Number and color, the whole column will be colored as follows.


 
 In the Selection screen if we enter both the Row number and the Column Number then that particular cell will be colored as follows (this color is not picked from selection screen, it is already written in the program itself)






Source Code:

REPORT yalvcol_list.

TABLESspfli.

TYPE-POOLSslis.

PARAMETERSp_col      TYPE i,
            p_row      
TYPE i,
            p_color
(4TYPE c.

DATA:
  t_fieldcat      
TYPE slis_t_fieldcat_alv,
  fs_fieldcat     
LIKE LINE OF t_fieldcat,
  fs_layout       
TYPE slis_layout_alv,
  w_color
(4) ,
  w_row           
TYPE i,
  w_fieldname
(20),
  w_prog          
TYPE sy-repid.

DATA:
  
BEGIN OF t_spfli OCCURS 0,
    
color(4),
    
checkbox ,
    cell     
TYPE slis_t_specialcol_alv,
    carrid   
TYPE spfli-carrid,
    connid   
TYPE spfli-connid,
    cityfrom 
TYPE spfli-cityfrom,
    cityto   
TYPE spfli-cityto,
    distance 
TYPE spfli-distance,
  
END OF t_spfli.

DATA:
fs_cell 
LIKE LINE OF t_spfli-cell.

SELECT *
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE t_spfli.

w_color 
p_color.

t_spfli
-color p_color.
IF p_col IS INITIAL AND p_row GT 0.
  
MODIFY t_spfli INDEX p_row TRANSPORTING color.
ENDIF.

fs_fieldcat
-fieldname 'CARRID'.
fs_fieldcat
-ref_tabname 'SPFLI'.
fs_fieldcat
-col_pos 1.
fs_fieldcat
-key 'X'.
fs_fieldcat
-hotspot 'X'.
APPEND fs_fieldcat TO t_fieldcat.

CLEAR fs_fieldcat .
fs_fieldcat
-fieldname 'CONNID'.
fs_fieldcat
-ref_tabname 'SPFLI'.
fs_fieldcat
-col_pos 2.
fs_fieldcat
-key 'X'.
fs_fieldcat
-hotspot 'X'.
APPEND fs_fieldcat TO t_fieldcat.

CLEAR fs_fieldcat .
fs_fieldcat
-fieldname 'DISTANCE'.
fs_fieldcat
-ref_tabname 'SPFLI'.

fs_fieldcat
-col_pos 3.
fs_fieldcat
-key ' '.
fs_fieldcat
-edit 'X'.
APPEND fs_fieldcat TO t_fieldcat.

CLEAR fs_fieldcat.
fs_fieldcat
-fieldname 'CITYFROM'.
fs_fieldcat
-ref_tabname 'SPFLI'.
fs_fieldcat
-col_pos 4.
fs_fieldcat
-key ' '.
APPEND fs_fieldcat TO t_fieldcat.

LOOP AT t_fieldcat INTO fs_fieldcat.
  
IF fs_fieldcat-col_pos EQ p_col.
    fs_fieldcat
-emphasize p_color.
    w_fieldname 
fs_fieldcat-fieldname.
    
IF p_row IS INITIAL AND p_col GT 0.
      
MODIFY t_fieldcat FROM fs_fieldcat TRANSPORTING emphasize.
    
ENDIF.
  
ENDIF.
ENDLOOP.

fs_cell
-fieldname w_fieldname .
fs_cell
-color-col 6.
fs_cell
-nokeycol 'X'.
APPEND fs_cell TO t_spfli-cell.

IF p_row IS NOT INITIAL AND p_col IS NOT INITIAL.
  
MODIFY t_spfli INDEX p_row TRANSPORTING cell.
ENDIF.

fs_layout
-info_fieldname 'COLOR'.
fs_layout
-box_fieldname 'CHECKBOX'.
fs_layout
-coltab_fieldname 'CELL'.
fs_layout
-f2code '&ETA'.

w_prog 
sy-repid.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
  
EXPORTING
    i_callback_program 
w_prog
    is_layout          
fs_layout
    it_fieldcat        
t_fieldcat
  
TABLES
    t_outtab           
t_spfli
  
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.