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: 

Highlighting a field in red color in ALV

Former Member
0 Kudos

Hi all,

I want to highlight one particular field in red/green color dynamically based on some condition in ALV display. Please suggest how to proceed.

Balaji

9 REPLIES 9

Former Member
0 Kudos
REPORT ZTESTALV.
 
TYPE-POOLS: SLIS.
INCLUDE <ICON>.
*- Fieldcatalog
DATA: IT_FIELDCAT  TYPE LVC_T_FCAT,
      IT_FIELDCAT1  TYPE SLIS_T_FIELDCAT_ALV..
*- For Events
DATA:IT_EVENTS TYPE SLIS_T_EVENT.
 
DATA:  X_FIELDCAT  TYPE LVC_S_FCAT,
        X_FIELDCAT1  TYPE SLIS_FIELDCAT_ALV.
DATA:X_LAYOUT TYPE LVC_S_LAYO.
TABLES: LIPS.
DATA: BEGIN OF IT_VBAP OCCURS 0,
      VBELN LIKE VBAP-VBELN,
      POSNR LIKE VBAP-POSNR,
      CELLCOLOR TYPE LVC_T_SCOL,
     END OF IT_VBAP.
 
SELECT VBELN
       POSNR
       UP TO 10 ROWS
      INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
      FROM VBAP.
 
 
DATA:L_POS TYPE I VALUE 1.
CLEAR: L_POS.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT = 'VBELN'.
X_FIELDCAT-FIELDNAME = 'VBELN'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '10'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.
 
X_FIELDCAT-SELTEXT = 'POSNR'.
X_FIELDCAT-FIELDNAME = 'POSNR'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '5'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.
X_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.
DATA: LS_CELLCOLOR TYPE LVC_S_SCOL.
DATA: L_INDEX TYPE SY-TABIX.
LOOP AT IT_VBAP.
  L_INDEX = SY-TABIX.
  LS_CELLCOLOR-FNAME = 'VBELN'.
  LS_CELLCOLOR-COLOR-COL = '6'.
  LS_CELLCOLOR-COLOR-INT = '1'.
  APPEND LS_CELLCOLOR TO IT_VBAP-CELLCOLOR.
  MODIFY IT_VBAP INDEX L_INDEX TRANSPORTING CELLCOLOR.
 
ENDLOOP.
 
 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    I_CALLBACK_PROGRAM       = SY-REPID
    IS_LAYOUT_LVC            = X_LAYOUT
    IT_FIELDCAT_LVC          = IT_FIELDCAT
  TABLES
    T_OUTTAB                 = IT_VBAP[]
  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.

Former Member
0 Kudos

Hi Balaji,

  • To color a line the structure of the table must include a Char 4 field for color properties

TYPES: BEGIN OF st_sflight.

INCLUDE STRUCTURE zsflight.

  • Field for line color

types: line_color(4) type c.

TYPES: END OF st_sflight.

TYPES: tt_sflight TYPE STANDARD TABLE OF st_sflight.

DATA: gi_sflight TYPE tt_sflight.

  • Loop trough the table to set the color properties of each line. The color properties field is

  • Char 4 and the characters is set as follows:

  • Char 1 = C = This is a color property

  • Char 2 = 6 = Color code (1 - 7)

  • Char 3 = Intensified on/of = 1 = on

  • Char 4 = Inverse display = 0 = of

LOOP AT gi_sflight INTO g_wa_sflight.

IF g_wa_sflight-paymentsum < 100000.

g_wa_sflight-line_color = 'C610'.

ENDIF.

MODIFY gi_sflight FROM g_wa_sflight.

ENDLOOP.

  • Name of the color field

gs_layout-info_fname = 'LINE_COLOR'.

  • Grid setup for first display

CALL METHOD go_grid->set_table_for_first_display

EXPORTING i_structure_name = 'SFLIGHT'

is_layout = gs_layout

CHANGING it_outtab = gi_sflight.

Former Member
0 Kudos

Hi,

Look at the below program

REPORT ZWA_ALV_COLORS .
 
tables : BKPF.
 
types : begin of ty_BKPF,
  belnr like BKPF-belnr,
  bukrs like BKPF-bukrs,
  counter(4) type n,
  color_line(4) type c, " Line color
  color_cell type lvc_t_scol, " Cell color
end of ty_BKPF.
 
* Structures
data : wa_BKPF type ty_BKPF,
wa_fieldcat type lvc_s_fcat,
is_layout type lvc_s_layo,
wa_color type lvc_s_scol.
 
* Internal table
data : it_BKPF type standard table of ty_BKPF,
       it_fieldcat type standard table of lvc_s_fcat,
       it_color type table of lvc_s_scol.
 
* Variables
data : okcode like sy-ucomm,
       w_alv_grid type ref to cl_gui_alv_grid,
       w_docking_container type ref to cl_gui_docking_container.
 
 
parameters : p_column as checkbox DEFAULT 'X',
             p_line as checkbox DEFAULT 'X',
             p_cell as checkbox DEFAULT 'X'.
 
 
at selection-screen output.
 
  perform get_data.
  perform fill_catalog.
 
  if w_docking_container is initial.
    perform create_objects.
  endif.
 
*&--------------------------------------------------------------*
*& Form create_objects
*&--------------------------------------------------------------*
form create_objects.
 
  create object w_docking_container
  exporting
  ratio = 40
  exceptions
  cntl_error = 1
  cntl_system_error = 2
  create_error = 3
  lifetime_error = 4
  lifetime_dynpro_dynpro_link = 5
  others = 6.
 
  create object w_alv_grid
  exporting
  i_parent = w_docking_container.
 
* Field that identify color line in internal table
  move 'COLOR_LINE' to is_layout-info_fname.
 
* Field that identify cell color in inetrnal table
  move 'COLOR_CELL' to is_layout-ctab_fname.
 
  call method w_alv_grid->set_table_for_first_display
  exporting
  is_layout = is_layout
  changing
  it_outtab = it_BKPF
  it_fieldcatalog = it_fieldcat
  exceptions
  invalid_parameter_combination = 1
  program_error = 2
  too_many_lines = 3
  others = 4.
 
endform.
*&--------------------------------------------------------------*
*& Form get_data
*&--------------------------------------------------------------*
form get_data.
 
  select * from BKPF up to 50 rows.
    clear : wa_BKPF-color_line, wa_BKPF-color_cell.
 
    move-corresponding BKPF to wa_BKPF.
    add 1 to wa_BKPF-counter.
 
    if wa_BKPF-COUNTER = '2' AND p_line = 'X'.
* Color line
      move 'C610' to wa_BKPF-color_line.
    elseif wa_BKPF-counter = '0004'
    and p_cell = 'X'.
* Color cell
      move 'BELNR' to wa_color-fname.
      move '1' to wa_color-color-col.
      move '1' to wa_color-color-int.
      move '1' to wa_color-color-inv.
      append wa_color to it_color.
      wa_BKPF-color_cell[] = it_color[].
    endif.
 
    append wa_BKPF to it_BKPF.
  endselect.
 
endform.
*&--------------------------------------------------------------*
*& Form fill_catalog
*&--------------------------------------------------------------*
form fill_catalog.
 
*****************************************************************
* Colour code : *
* Colour is a 4-char field where : *
* - 1st char = C (color property) *
* - 2nd char = color code (from 0 to 7) *
* 0 = background color *
* 1 = blue *
* 2 = gray *
* 3 = yellow *
* 4 = blue/gray *
* 5 = green *
* 6 = red *
* 7 = orange *
* - 3rd char = intensified (0=off, 1=on) *
* - 4th char = inverse display (0=off, 1=on) *
* *
* Colour overwriting priority : *
* 1. Line *
* 2. Cell *
* 3. Column *
*****************************************************************
  data : w_position type i value '1'.
 
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'BELNR' to wa_fieldcat-fieldname.
  move 'BKPF' to wa_fieldcat-ref_table.
  move 'BELNR' to wa_fieldcat-ref_field.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'BUKRS' to wa_fieldcat-fieldname.
  move 'BKPF' to wa_fieldcat-ref_table.
  move 'BUKRS' to wa_fieldcat-ref_field.
* Color column
  if p_column = 'X'.
    move 'C510' to wa_fieldcat-emphasize.
  endif.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'COUNTER' to wa_fieldcat-fieldname.
  move 'N' to wa_fieldcat-inttype.
  move '4' to wa_fieldcat-intlen.
  move 'Counter' to wa_fieldcat-coltext.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
*  clear wa_fieldcat.
*  move w_position to wa_fieldcat-col_pos.
*  move 'FREE_TEXT' to wa_fieldcat-fieldname.
*  move 'C' to wa_fieldcat-inttype.
*  move '20' to wa_fieldcat-intlen.
*  move 'Text' to wa_fieldcat-coltext.
*  append wa_fieldcat to it_fieldcat.
ENDFORM.

Regards

Sudheer

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

Coloring the particular cell is possible.

Step 1:

  • Type of final output table

TYPES : BEGIN OF ty_output.

INCLUDE STRUCTURE zjay_alv.

*TYPES : * For cell coloring

cellcolor TYPE lvc_t_scol,

END OF ty_output.

  • Internal Table declaration

DATA : itab1 TYPE STANDARD TABLE OF zjay_alv,

  • Final output table

itab2 TYPE STANDARD TABLE OF ty_output,

wa1 TYPE zjay_alv,

wa2 TYPE ty_output,

w_cellcolor TYPE lvc_s_scol. "For cell color

SELECT matnr ersda ernam FROM mara INTO CORRESPONDING FIELDS OF TABLE

itab1

UP TO 10 ROWS WHERE matnr IN s_matnr.

LOOP AT itab1 INTO wa1.

MOVE-CORRESPONDING wa1 TO wa2.

APPEND wa2 TO itab2.

ENDLOOP.

2. In PBO of the screen

*Colouring a cell

CLEAR wa2.

<b>*Here according to the condition you colour it using loop</b>

READ TABLE itab2 INTO wa2 INDEX 7.

IF sy-subrc EQ 0.

w_cellcolor-fname = 'ERSDA'.

w_cellcolor-color-col = '7'.

w_cellcolor-color-int = '1'.

w_cellcolor-color-inv = '1'.

APPEND w_cellcolor TO wa2-cellcolor.

MODIFY itab2 FROM wa2 TRANSPORTING cellcolor WHERE matnr = wa2-matnr.

ENDIF.

3. w_layout-ctab_fname = 'CELLCOLOR'."For cell coloring

4. CALL METHOD o_grid->set_table_for_first_display

EXPORTING

  • I_STRUCTURE_NAME = 'ZJAY_ALV'

IS_VARIANT = w_variant

I_SAVE = 'A'

it_toolbar_excluding = i_exclude

is_layout = w_layout

CHANGING

it_outtab = itab2

it_fieldcatalog = i_fieldcat.

Former Member
0 Kudos

Hi all,

I am using the FM 'REUSE_ALV_FIELDCATALOG_MERGE' for creating field catalog. Please let me know hw to proceed.

FORM alv_display.

  • IF p_stprs-low <> ''.

  • DELETE it_result WHERE stprs <> p_stprs-low.

  • ENDIF.

SORT it_result BY matnr.

DELETE ADJACENT DUPLICATES FROM it_result

COMPARING matnr maktx werks dispo fevor beskz sobsl disgr mmsta

xchpf charg stprs lplpr infnr stlan stlnr stlal stlst plnty

plnnr plnal.

IF p_avibat = 'X' AND p_batch <> 'X'.

DELETE it_result WHERE charg = ''.

ENDIF.

t_structure = 'ZUS_PP13_ALV'.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = t_structure

CHANGING

ct_fieldcat = t_fieldcat.

v_report = sy-repid.

      • Deciding the layout.

  • IF p_bom = 'X' OR p_avbom = 'X'.

  • variant1 = 'BO'.

  • ENDIF.

*

  • IF p_rout = 'X' OR p_avrout = 'X'.

  • variant2 = 'RO'.

  • ENDIF.

  • IF p_info = 'X' OR p_avinfo = 'X'.

  • variant3 = 'VN'.

  • ENDIF.

  • IF p_batch = 'X' OR p_avibat = 'X'.

  • variant4 = 'BT'.

  • ENDIF.

*

  • CONCATENATE '/' variant1 variant2 variant3 variant4

  • INTO s_variant-variant.

IF s_variant-variant IS INITIAL.

s_variant-variant = alv_def.

ENDIF.

      • ----------

CLEAR lns.

DESCRIBE TABLE it_result LINES lns.

IF lns > 0.

IF p_entry <> ''.

p_entry = p_entry + 1.

DELETE it_result FROM p_entry TO lns.

ENDIF.

ENDIF.

CLEAR lns.

DESCRIBE TABLE it_result LINES lns.

CLEAR lines.

lines = lns.

CONCATENATE 'Total no. of records =' lines INTO title.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = v_report

i_callback_pf_status_set = 'STATUS_SET'

i_callback_user_command = 'FUNCTION_EXECUTE'

i_grid_title = title

is_layout = s_layout

is_variant = s_variant

is_print = alv_print

it_fieldcat = t_fieldcat

i_save = 'A'

TABLES

t_outtab = it_result.

ENDFORM. " alv_display

0 Kudos

Hi,

It should be similar to OOPS concept for cell color.

Check this link.

http://www.geocities.com/mpioud/Abap_programs.html

Jayanthi Jayaraman

Former Member
0 Kudos

Hi Jayanthi,

Is it possible for you to give the code.??

Balaji

Former Member
0 Kudos

I ahve changed the color of complete row in alv as follows:-

based on you condition you can change color of one perticular field.

data:g_ls_good type lvc_s_modi,

class lcl_event_receiver implementation.

method handle_data_changed.

loop at er_data_changed->mt_good_cells into g_ls_good.

perform color_change using g_ls_good-row_id

endloop.

endmethod. "handle_data_changed

endclass. "lcl_event_receiver IMPLEMENTATION

form color_change using row_id.

read table ITAB_output index row_id.

if sy-subrc = 0.

If condition satisfied.

move 'C300' to ITAB_output-color_line.

else.

move 'C365' to ITAB_output-color_line.

endif.

modify ITAB_output index row_id.

endif.

endform. " color_change

ITAB is internal table used in dispyaing ALV.