When designing an ALV report, you sometimes need to display a specific unit in a specific color based on certain conditions. The specific implementation steps are as follows:
1. Add a new field to the inner table to be displayed. The field type is lvc_t_scol.
2. Set color data based on the conditions;
3. Set the data for the layout-coltab_fieldname to specify the display color field.
The Code is as follows: (pay attention to the red part)
Report z_alv_color.
Type-pools: slis.
Data: Begin of it_flight occurs 0,
Carrid like sflight-carrid,
Connid like sflight-connid,
Fldate like sflight-fldate,
Seatsmax like sflight-seatsmax,
Seatsocc like sflight-seatsocc,
Color type lvc_t_scol,
End of it_flight.
Data: it_fieldcat type slis_t_fieldcat_alv,
Layout type slis_layout_alv.
Call function 'reuse _ alv_fieldcatalog_merge'
Exporting
I _program_name = sy-repid
I _internal_tabname = 'it _ flight'
I _inclname = sy-repid
Changing
Ct_fieldcat = it_fieldcat
Exceptions
Inconsistent_interface = 1
Program_error = 2.
Select carrid
Connid
Fldate
Seatsmax
Seatsocc
From sflight
Into corresponding fields of table it_flight
Up to 20 rows.
Data: Col type lvc_s_scol,
Coltab type lvc_t_scol,
Color type lvc_s_colo.
Color-Col = '6 '.
Color-Int = '0 '.
Color-inv = '0 '.
Loop at it_flight.
If it_flight-seatsocc EQ 0.
Col-fname = 'seatsocc '.
Col-color = color.
Append Col to coltab.
Clear Col.
It_flight-color = coltab.
Modify it_flight.
Endif.
Endloop.
Layout-coltab_fieldname = 'color '.
Call function 'reuse _ alv_grid_display'
Exporting
I _callback_program = sy-repid
Is_layout = Layout
It_fieldcat = it_fieldcat
Tables
T_outtab = it_flight
Exceptions
Program_error = 1.