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: 

Conditional colouring of fields in an ALV grid

Former Member
0 Kudos

Hello friends,

I would like to set conditional colouring for the columns of ALV report for change in value depending on the threshold limit.I tried it using emphasize in a field catlouge but it is not working as i am using CASE with a variable not belonging to the field of the fieldcatlouge.

There is another option in field catlouge that is slis_coltypes.how is this used in case of ALV grid display to change colours of a particular field on the basis of a condition.

Regards

Ashish.

3 REPLIES 3

Former Member
0 Kudos

Hi Ashish,

Have a look at this.

&----


*& Report ZDEMO_ALVGRID *

*& *

&----


*& *

*& Example of a simple ALV Grid Report *

*& ................................... *

*& *

*& The basic ALV grid, Enhanced to display each row in a different *

*& colour *

&----


REPORT zdemo_alvgrid .

TABLES: ekko.

type-pools: slis. "ALV Declarations

*Data Declaration

*----


TYPES: BEGIN 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,

line_color(4) type c, "Used to store row color attributes

END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,

wa_ekko TYPE t_ekko.

*ALV data declarations

data: fieldcatalog 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.

************************************************************************

*Start-of-selection.

START-OF-SELECTION.

perform data_retrieval.

perform build_fieldcatalog.

perform build_layout.

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-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).

  • Set layout field for row attributes(i.e. color)

gd_layout-info_fieldname = 'LINE_COLOR'.

  • gd_layout-totals_only = 'X'.

  • gd_layout-f2code = 'DISP'. "Sets fcode for when double

  • "click(press f2)

  • gd_layout-zebra = 'X'.

  • gd_layout-group_change_edit = 'X'.

  • gd_layout-header_text = 'helllllo'.

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_XEVENTS

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.

data: ld_color(1) type c.

select ebeln ebelp statu aedat matnr menge meins netpr peinh

up to 10 rows

from ekpo

into table it_ekko.

*Populate field with color attributes

loop at it_ekko into wa_ekko.

  • Populate color variable with colour properties

  • Char 1 = C (This is a color property)

  • Char 2 = 3 (Color codes: 1 - 7)

  • Char 3 = Intensified on/off ( 1 or 0 )

  • Char 4 = Inverse display on/off ( 1 or 0 )

  • i.e. wa_ekko-line_color = 'C410'

ld_color = ld_color + 1.

  • Only 7 colours so need to reset color value

if ld_color = 8.

ld_color = 1.

endif.

concatenate 'C' ld_color '10' into wa_ekko-line_color.

  • wa_ekko-line_color = 'C410'.

modify it_ekko from wa_ekko.

endloop.

endform. " DATA_RETRIEVAL

Reward If Useful.

Regards,

Chitra

p291102
Active Contributor
0 Kudos

Hi,

Have a look on this:

REPORT YMS_COLOURALV NO STANDARD PAGE HEADING.

TYPE-POOLS: SLIS, ICON.

DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.

DATA: BEGIN OF IMARA OCCURS 0,

LIGHT(4) TYPE C,

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MAKTX TYPE MAKT-MAKTX,

COLOR_LINE(4) TYPE C,

TCOLOR TYPE SLIS_T_SPECIALCOL_ALV, "cell

END OF IMARA.

DATA: XCOLOR TYPE SLIS_SPECIALCOL_ALV.

START-OF-SELECTION.

PERFORM GET_DATA.

PERFORM WRITE_REPORT.

************************************************************************

  • Get_Data

************************************************************************

FORM GET_DATA.

WRITE ICON_GREEN_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'ABC'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for ABC'.

APPEND IMARA.

WRITE ICON_YELLOW_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'DEF'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for DEF'.

APPEND IMARA.

WRITE ICON_RED_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'GHI'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for GHI'.

APPEND IMARA.

LOOP AT IMARA.

IF SY-TABIX = 1.

IMARA-COLOR_LINE = 'C410'. " color line

ENDIF.

IF SY-TABIX = 2. " color CELL

CLEAR XCOLOR.

XCOLOR-FIELDNAME = 'MTART'.

XCOLOR-COLOR-COL = '3'.

XCOLOR-COLOR-INT = '1'. " Intensified on/off

XCOLOR-COLOR-INV = '0'.

APPEND XCOLOR TO IMARA-TCOLOR.

ENDIF.

MODIFY IMARA.

ENDLOOP.

ENDFORM. "get_data

************************************************************************

  • WRITE_REPORT

************************************************************************

FORM WRITE_REPORT.

DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.

LAYOUT-COLTAB_FIELDNAME = 'TCOLOR'.

LAYOUT-INFO_FIELDNAME = 'COLOR_LINE'.

PERFORM BUILD_FIELD_CATALOG.

  • CALL ABAP LIST VIEWER (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IS_LAYOUT = LAYOUT

IT_FIELDCAT = FIELDCAT

TABLES

T_OUTTAB = IMARA.

ENDFORM. "write_report

************************************************************************

  • BUILD_FIELD_CATALOG

************************************************************************

FORM BUILD_FIELD_CATALOG.

DATA: FC_TMP TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.

CLEAR: FIELDCAT. REFRESH: FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Status'.

FC_TMP-FIELDNAME = 'LIGHT'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '4'.

FC_TMP-ICON = 'X'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material Number'.

FC_TMP-FIELDNAME = 'MATNR'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '18'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material Type'.

FC_TMP-FIELDNAME = 'MTART'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '10'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material'.

FC_TMP-FIELDNAME = 'MAKTX'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '40'.

FC_TMP-EMPHASIZE = 'C610'. " color column

APPEND FC_TMP TO FIELDCAT.

ENDFORM. "build_field_catalog

Thanks,

Sankar M

Former Member
0 Kudos

Hi Anish,

Use the following code for your Query:-

*Example of how to color a column value in ALV Report.

REPORT z_colour NO STANDARD PAGE HEADING .

TABLES :vbak , BNKA.

TYPE-POOLS: slis. "ALV Declarations

DATA : BEGIN OF it_temp OCCURS 0,

vbeln LIKE vbak-vbeln,

vbtyp LIKE vbak-vbtyp,

END OF it_temp.

DATA : BEGIN OF it OCCURS 0,

vbeln LIKE vbak-vbeln,

vbtyp LIKE vbak-vbtyp,

cell_colour TYPE lvc_t_scol, "Cell colour

END OF it.

SELECTION-SCREEN BEGIN OF BLOCK main WITH FRAME.

SELECT-OPTIONS :s_vbeln FOR vbak-vbeln .

SELECTION-SCREEN END OF BLOCK main.

*ALV data declarations

DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH

HEADER LINE,

gd_layout TYPE slis_layout_alv,

gd_repid LIKE sy-repid,

gt_sort TYPE slis_t_sortinfo_alv.

  • To colour a cell.

DATA ls_cellcolour TYPE lvc_s_scol.

types: begin of loc_x_bnka,

BANKA like bnka-banka,

STRAS like bnka-stras,

ORT01 like bnka-ort01,

PROVZ like bnka-provz,

SWIFT like bnka-swift,

end of loc_x_bnka .

data: loc_wa_bnka type loc_x_bnka.

START-OF-SELECTION.

select single BANKA STRAS ORT01 PROVZ SWIFT

from BNKA into loc_wa_bnka

where banks = 'AN'

and bankl = 'ABN'.

PERFORM data_retrieval.

PERFORM build_fieldcatalog.

PERFORM build_layout.

PERFORM display_alv_report.

END-OF-SELECTION.

FREE : it.

FORM build_fieldcatalog .

fieldcatalog-fieldname = 'VBELN'.

fieldcatalog-seltext_m = 'Document'.

fieldcatalog-col_pos = 0.

APPEND fieldcatalog TO fieldcatalog.

CLEAR fieldcatalog.

fieldcatalog-fieldname = 'VBTYP'.

fieldcatalog-seltext_m = 'Type'.

fieldcatalog-col_pos = 0.

APPEND fieldcatalog TO fieldcatalog.

CLEAR fieldcatalog.

ENDFORM.

FORM build_layout .

gd_layout-no_input = 'X'.

gd_layout-colwidth_optimize = 'X'.

gd_layout-totals_text = 'Totals'(256).

gd_layout-coltab_fieldname = 'CELL_COLOUR'.

ENDFORM. " build_layout

FORM display_alv_report .

gd_repid = sy-repid.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = gd_repid

is_layout = gd_layout

it_fieldcat = fieldcatalog[]

i_save = 'A'

TABLES

t_outtab = it

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

ENDFORM. " display_alv_report

FORM data_retrieval .

select vbeln vbtyp from vbak into corresponding

fields of table it_temp where vbeln in s_vbeln.

loop at it_temp.

it-vbeln = it_temp-vbeln .

it-vbtyp = it_temp-vbtyp .

append it .

endloop .

LOOP AT it.

*Now based on the value of the field pernr we can

*change the cell colour of the field rufnm or pernr.

IF it-vbtyp eq 'K' .

ls_cellcolour-fname = 'VBTYP'.

ls_cellcolour-color-col = '5'.

ls_cellcolour-color-int = '1'.

ls_cellcolour-color-inv = '0'.

append ls_cellcolour TO it-cell_colour.

ENDIF.

modify it .

ENDLOOP.

Reward if useful.

Regards,

Shilpi