Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to colorize ALV GRID using CL_SALV_LAYOUT

shiz0frenik
Participant
0 Kudos

How to colorize ALV GRID using CL_SALV_LAYOUT ?

DATA: gc_alv_table TYPE REF TO cl_salv_table,

gc_layout TYPE REF TO cl_salv_layout,

gc_layout = gc_alv_table->get_layout( ).

Usually i use info_fname propperty to do it but it doesn't exist here

3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos

For CL_SALV_TABLE, see demo program SALV_DEMO_TABLE_COLUMNS.

(Program to generate demo data: BCALV_GENERATE_ALV_T_T2)

juan_suros
Contributor
0 Kudos

As you will see in Susan's examples, the trick is to include a field of type lvc_t_scol in your alv display structure. Set references to this field as you set the class attributes.

Here is a very simple example program.

*&---------------------------------------------------------------------*
*& Report ZDEMO_ALV_REPORT_COLOR
*&---------------------------------------------------------------------*
* Minimal example of ALV report setting individual field color
*----------------------------------------------------------------------*
REPORT zdemo_alv_report_color
NO STANDARD PAGE HEADING
LINE-SIZE 80
LINE-COUNT 65(0)
MESSAGE-ID z9.
***************************
LOAD-OF-PROGRAM. "EVENT ***
***************************
  TABLES: vbak.
TYPES:
BEGIN OF data_table_type.
INCLUDE STRUCTURE vbap. TYPES:
color TYPE lvc_t_scol,
END OF data_table_type,
data_tt TYPE data_table_type OCCURS 0.
DATA:
lo_message TYPE REF TO cx_salv_msg,
lo_alv TYPE REF TO cl_salv_table,
t_data TYPE data_tt,
gs_layout TYPE lvc_s_layo,
gv_color TYPE i.
SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.
PARAMETERS: p_layout TYPE slis_vari.
*&---------------------------------------------------------------------*
*& Form SELECT_LAYOUT
*&---------------------------------------------------------------------*
FORM select_layout.
DATA:
ls_variant TYPE disvariant,
lv_exit TYPE char1.
ls_variant-report = sy-repid.
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
is_variant = ls_variant
i_save = 'A'
IMPORTING
e_exit = lv_exit
es_variant = ls_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc EQ 0.
IF lv_exit EQ 'X'. "Dialog cancelled by user
"Do nothing
ELSE.
p_layout = ls_variant-variant.
ENDIF.
ELSE.
MESSAGE ID sy-msgid
TYPE 'S'
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


ENDFORM. " SELECT_LAYOUT

*************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_layout. "EVENT ***
*************************************************************

PERFORM select_layout.

**************************
INITIALIZATION. "EVENT ***
**************************

gv_color = col_negative.

gs_layout-ctab_fname = 'COLOR'.

******************************
START-OF-SELECTION. "EVENT ***
******************************

* Grab some data
SELECT * FROM vbap APPENDING CORRESPONDING FIELDS OF TABLE t_data UP TO 10 ROWS WHERE vbeln IN s_vbeln.

* Set color in individual fields or data rows
LOOP AT t_data ASSIGNING FIELD-SYMBOL(<data>).
CASE sy-tabix.
WHEN 1.
<data>-color = VALUE #( ( fname = 'MANDT' color-col = gv_color ) ).
WHEN 2.
<data>-color = VALUE #( ( fname = 'VBELN' color-col = gv_color ) ).
WHEN 3.
<data>-color = VALUE #( ( fname = 'POSNR' color-col = gv_color ) ).
WHEN 4.
<data>-color = VALUE #( ( fname = 'MATNR' color-col = gv_color ) ).
ENDCASE.
ENDLOOP.

****************************
END-OF-SELECTION. "EVENT ***
****************************

TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = t_data ).
CATCH cx_salv_msg INTO lo_message ##NO_HANDLER.
ENDTRY.

lo_alv->get_columns( )->set_color_column( 'COLOR' ).

lo_alv->get_functions( )->set_all( abap_true ). "Set all standard functions of ALV
lo_alv->get_columns( )->set_optimize( abap_true ). "Optimize column length
lo_alv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ). "Line and Column Selection
lo_alv->get_display_settings( )->set_striped_pattern( cl_salv_display_settings=>true ). "zebra stripes

* Set up saving of layouts for this report
lo_alv->get_layout( )->set_key( VALUE salv_s_layout_key( report = sy-repid ) ).
lo_alv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_none ).
lo_alv->get_layout( )->set_default( if_salv_c_bool_sap=>true ). "Allow layout preset

* Use a layout, if supplied in selection screen
IF p_layout IS NOT INITIAL.
lo_alv->get_layout( )->set_initial_layout( p_layout ).
ENDIF.

lo_alv->display( ).

0 Kudos

Tnx, but is it possible to colorize only rows? And i don't have opportunity to add lvc_t_scol in structure with code, only with se11 structure edit. But this mistake happens when i do it. And i don't understand why it's happening what relation lvc_t_scol have to date type conversion?