Friday, September 12, 2008

Change color of a cell in ALV report

By Joyjit Ghosh,
Kolkata, India.

To change the color of a particular cell at runtime ,we have to perform the following steps:


Step1:

Define an internal table of TYPE slis_specialcol_alv as a field into the output table which has to pass into the ‘REUSE_ALV_GRID_DISPLAY’ Function module.

TYPES: BEGIN OF ty_user_output,

BELNR like RBKP-BELNR, "Doc. Number

BUDAT like RBKP-BUDAT, "Posting Date

BUKRS like RBKP-BUKRS, "Company Code

LIFNR like RBKP-LIFNR, "Invoicing Party

NAME1 like LFA1-NAME1, "Name

USNAM like RBKP-USNAM, "User

BUZEI like RSEG-BUZEI, "Invoice Item

RBWWR like RSEG-RBWWR, "Amount

EBELN like RSEG-EBELN, "Purchase Document

EBELP like RSEG-EBELP, "Purchase Order Item

WERKS like RSEG-WERKS, "Plant

EKGRP like EKKO-EKGRP, "Purchasing Group

TBTKX like RSEG-TBTKZ, "Subsequent Debit/Credit

SPGRG like RSEG-SPGRG, "Order Price Quantity

SPGRM like RSEG-SPGRM, "Blocking Reason:Quantity

SPGRP like RSEG-SPGRP, "Blocking Reason:Price

SPGRC like RSEG-SPGRC, "Blocking Reason:Quality

SPGRS like RSEG-SPGRS, "Blocking Reason:Amount

SPGRT like RSEG-SPGRT, "Blocking Reason:Date

SPGRQ like RSEG-SPGRQ, "Blocking Reason:Manual

parked(1) type c , "Indicates Parked Invoice

Note(1) type c, "Indicates Blocking Notes

GJAHR like RBKP-GJAHR, "Fiscal year

color TYPE slis_specialcol_alv OCCURS 2,

"For cell color

END OF ty_user_output.

Step2:

When building the layout set for ALV ,specify the color field name.

w_layout-coltab_fieldname = 'COLOR'.

Step3:

If we want to give a color to any column when the report is first displayed,

then we have to populate the internal table COLOR.

DATA: w_color TYPE slis_specialcol_alv. " Color

w_color-fieldname = p_field.

w_color-color-col = p_color.

w_color-color-int = p_int.

w_color-color-inv = p_inv.

w_color-nokeycol = p_key.

APPEND w_color TO w_user_output-color.

CLEAR w_color.

Step4:

If the user press any custom button or double click on any particular field,

then we have to track the sy-ucomm value .To do this we have to create a subroutine,

which has two parameters ,one is like sy-ucomm and another is type slis_selfield,

and we have to assign the subroutine name in the ‘i_callback_user_command ‘ parameter of the function module REUSE_ALV_GRID_DISPLAY.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = c_repid

i_callback_user_command = 'USER_COMMAND'

i_structure_name = 'i_tab'

is_layout = w_layout

it_fieldcat = i_fieldcat[]

I_SAVE = 'A'

is_variant = w_variant

it_events = i_levents

is_print = w_print

TABLES

t_outtab = i_tab

EXCEPTIONS

program_error = 1

OTHERS = 2.





Form USER_COMMAND USING r_ucomm LIKE sy-ucomm

rs_selfield TYPE slis_selfield.



ENDFORM. "USER_COMMAND


Now when ever user generates a particular function code then we have to first check the field name then modify the color table depending upon field name and lastly modify the output table. This logic is written in to the above subroutine.

IF r_ucomm = V_F2CODE.

if rs_selfield-fieldname = c_doc_name.

*Work area for color table.

DATA: w_color TYPE slis_specialcol_alv.

clear w_user_output.

*Read internal table for proper value.

read table i_user_output

index rs_selfield-tabindex(Contains index of the current record selected by the user)

into w_user_output.

if sy-subrc = 0.

read table w_user_output-color into w_color

with key fieldname = rs_selfield-fieldname(Contains current field name selected by the user).

if sy-subrc = 0.

w_color-color-col = '4'.

w_color-color-int = 1.

w_color-color-inv = 0.

* Modify the internal table Color

MODIFY w_user_output-color FROM w_color

INDEX sy-tabix TRANSPORTING color.

CLEAR w_color.

* Modify the internal table i_user_output

MODIFY i_user_output FROM w_user_output

INDEX p_index TRANSPORTING color.

endif.

endif.

Endif.

Endif.

Now to refresh the ALV grid, set the refresh field of the rs_selfield parameter.

rs_selfield-refresh = ‘X’.


Report Output:

First time when the report is displayed:



After successive processing:

No comments: