2006 Oct 13 2:29 PM
How to color a particular cell in ALV display by using methods sET_TABLE_FOR_FIRST_DISPLAY ?
Regards,
johnn.
How to color a particular cell in ALV display by using methods sET_TABLE_FOR_FIRST_DISPLAY ?
Regards,
johnn.
2006 Oct 13 2:33 PM
Here is a short example program which shows how to color a row, column, or cell.
report zrich_0002 .
*****************************************************************
* Use of colours in ALV grid (cell, line and column) *
*****************************************************************
* Table
tables : mara.
* Type
types : begin of ty_mara,
matnr like mara-matnr,
matkl like mara-matkl,
counter(4) type n,
free_text(15) type c,
color_line(4) type c, " Line color
color_cell type lvc_t_scol, " Cell color
end of ty_mara.
* Structures
data : wa_mara type ty_mara,
wa_fieldcat type lvc_s_fcat,
is_layout type lvc_s_layo,
wa_color type lvc_s_scol.
* Internal table
data : it_mara type standard table of ty_mara,
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,
p_line as checkbox,
p_cell as checkbox.
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 = 60
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_mara
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 mara up to 5 rows.
clear : wa_mara-color_line, wa_mara-color_cell.
move-corresponding mara to wa_mara.
add 1 to wa_mara-counter.
move 'Blabla' to wa_mara-free_text.
if wa_mara-counter = '0002'
and p_line = 'X'.
* Color line
move 'C410' to wa_mara-color_line.
elseif wa_mara-counter = '0004'
and p_cell = 'X'.
* Color cell
move 'FREE_TEXT' to wa_color-fname.
move '6' 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_mara-color_cell[] = it_color[].
endif.
append wa_mara to it_mara.
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 'MATNR' to wa_fieldcat-fieldname.
move 'MARA' to wa_fieldcat-ref_table.
move 'MATNR' 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 'MATKL' to wa_fieldcat-fieldname.
move 'MARA' to wa_fieldcat-ref_table.
move 'MATKL' to wa_fieldcat-ref_field.
* Color column
if p_column = 'X'.
move 'C610' 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,
Rich Heilman
2007 Jul 24 1:07 AM
This is Great Program but could you modify this for following Hirarchial ALV. I tried but i am not getting color changed for the Cell.
call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
exporting
is_layout = gd_layout
it_fieldcat = g_fieldcat
i_tabname_header = 'WA_HEADER'
i_tabname_item = 'WA_ITEM'
is_keyinfo = wa_keyinfo
is_variant = l_variant
tables
t_outtab_header = it_header1
t_outtab_item = it_item1
exceptions
program_error = 1
others = 2.
Thanks
2006 Oct 13 2:43 PM
Hi,
Some simple steps.
1)add a field to your output table
line_color(4) type c,
2) Do programmatically what you need to do and specify the colour you want for each row.
e.g
if <condition>.
itab-line_colour = 'C210'
endif.
how you get this is
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 )
3 Define the layout with
ws_layout-info_fieldname = 'LINE_COLOR'.
this will enable you to set the colour for various conditions.
2006 Oct 13 2:52 PM
Assign a variable celltab TYPE lvc_t_styl, in the internal table which u dispaly the data.
In the layout give, wa_layout-stylefname = 'CELLTAB'.
In the final table before showing the data do like the below code.
DATA : lt_celltab TYPE lvc_t_styl,
ls_celltab TYPE lvc_s_styl.
READ TABLE i_final_data INTO wa_final_data INDEX 1.
ls_celltab-fieldname = 'LNG_TYPES'.
ls_celltab-style = '00000060'.
INSERT ls_celltab INTO TABLE lt_celltab.
CLEAR ls_celltab.
ls_celltab-fieldname = 'LNG_QUANTITY'.
ls_celltab-style = '00003060'.
INSERT ls_celltab INTO TABLE lt_celltab.
CLEAR ls_celltab.
ls_celltab-fieldname = 'NG_TYPES'.
ls_celltab-style = '00000666'.
INSERT ls_celltab INTO TABLE lt_celltab.
CLEAR ls_celltab.
ls_celltab-fieldname = 'NG_QUANTITY'.
ls_celltab-style = '00000066'.
INSERT ls_celltab INTO TABLE lt_celltab.
CLEAR ls_celltab.
wa_final_data-celltab[] = lt_celltab[].
MODIFY i_final_data FROM wa_final_data INDEX 1.
CLEAR: ls_celltab, lt_celltab, wa_final_data.
Here ls_celltab-style = '00000066' or '00000060' gives the color for a particular cell.
Read the row which u want to make cell colored. For that row, append the field name and style into ls_celltab.
ls_celltab-fieldname = 'LNG_TYPES'.
ls_celltab-style = '00003040'.
INSERT ls_celltab INTO TABLE lt_celltab.
wa_final_data-celltab[] = lt_celltab[].
So that the particular cell will become colored.
Another way:
Do as follows
1. declare one field say color in the output table of type SLIS_T_SPECIALCOL_ALV.
2. Also declare a table color like
DATA COLOR TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE.
3. After you have filled the field catalog do like this,
LOOP AT IT_ALV_DATA.
IF SY-TABIX = 2.
COLOR-FIELDNAME = 'POSNR'.
COLOR-COLOR-COL = 6.
COLOR-COLOR-INT = 0.
APPEND COLOR.
IT_ALV_DATA-COLOR = COLOR[].
MODIFY IT_ALV_DATA.
ENDIF.
ENDLOOP.
Regards,
Prakash.
2011 Jul 20 12:08 PM
Hi John similar to your requirement only i also got the requirement after working some times i got the solutions the demo report of that one i have written down if you need you can go through it.
TYPE-POOLS : slis .
TYPES : BEGIN OF type_vbrk ,
vbeln TYPE vbrk-vbeln ,
netwr TYPE vbrk-netwr ,
cellcolor TYPE lvc_t_scol ,
END OF type_vbrk .
TYPES : type_t_vbrk TYPE STANDARD TABLE OF type_vbrk .
DATA : wa_vbrk TYPE type_vbrk ,
it_vbrk TYPE type_t_vbrk ,
it_fieldcat TYPE slis_t_fieldcat_alv ,
wa_fieldcat TYPE slis_fieldcat_alv ,
wa_layout TYPE slis_layout_alv .
START-OF-SELECTION .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '121212' .
wa_vbrk-netwr = '12.22' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '121213' .
wa_vbrk-netwr = '-12.22' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '10001' .
wa_vbrk-netwr = '-16.22' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '10002' .
wa_vbrk-netwr = '36.22' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '101012' .
wa_vbrk-netwr = '-32.22' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '121219' .
wa_vbrk-netwr = '-45.00' .
APPEND wa_vbrk TO it_vbrk .
CLEAR wa_vbrk .
wa_vbrk-vbeln = '1125212' .
wa_vbrk-netwr = '92.22' .
APPEND wa_vbrk TO it_vbrk .
*-- SETTING THE CELL COLOR
DATA: wa_cellcolor TYPE lvc_s_scol ,
ld_index TYPE sy-tabix.
LOOP AT it_vbrk INTO wa_vbrk .
ld_index = sy-tabix.
IF wa_vbrk-netwr < 0 .
wa_cellcolor-fname = 'NETWR'.
wa_cellcolor-color-col = 6 . "color code 1-7, if outside rage defaults to 7
wa_cellcolor-color-int = '1' . "1 = Intensified on, 0 = Intensified off
wa_cellcolor-color-inv = '0' . "1 = text colour, 0 = background colour
APPEND wa_cellcolor TO wa_vbrk-cellcolor.
MODIFY it_vbrk FROM wa_vbrk INDEX ld_index TRANSPORTING cellcolor.
ENDIF.
ENDLOOP .
CLEAR wa_fieldcat .
wa_fieldcat-col_pos = 1 .
wa_fieldcat-fieldname = 'VBELN' .
wa_fieldcat-tabname = 'IT_VBRK' .
wa_fieldcat-seltext_m = 'Invoice Number' .
APPEND wa_fieldcat TO it_fieldcat .
CLEAR wa_fieldcat .
wa_fieldcat-col_pos = 2 .
wa_fieldcat-fieldname = 'NETWR' .
wa_fieldcat-tabname = 'IT_VBRK' .
wa_fieldcat-seltext_m = 'Net Value' .
APPEND wa_fieldcat TO it_fieldcat .
wa_layout-zebra = 'X' .
wa_layout-colwidth_optimize = 'X' .
wa_layout-coltab_fieldname = 'CELLCOLOR'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_grid_title = 'Supress Negative Values'
I_GRID_SETTINGS =
is_layout = wa_layout
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_vbrk
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.
2011 Sep 26 1:23 PM
Hi ALL,
bellow you can see a simple code about how to change a cell color in ALV .
TYPES: BEGIN OF wa_out .
INCLUDE STRUCTURE ZPP_BARCODE .
TYPES : cellcolors TYPE lvc_t_scol ,
SERNR TYPE objk-SERNR ,
VORNR TYPE afru-VORNR ,
ROWNO TYPE i ,
end of wa_out.
data: w_cellcolor TYPE lvc_s_scol. "For cell color .
data: gt_out TYPE STANDARD TABLE OF wa_out , " table for ALV output
wa_out1 type wa_out ,
P_INDX type I .
*--- first : append data to gt_out table
...
...
...
*----
now , you can modify table for coloring cell
CLEAR W_CELLCOLOR.
P_INDX = 10 ."-----for coloring a cell in row 10
W_CELLCOLOR-FNAME = FNAME . "field name for example cell for material ->matnr
W_CELLCOLOR-COLOR-COL = '6'. "color
W_CELLCOLOR-COLOR-INT = '1'.
W_CELLCOLOR-COLOR-INV = '1'.
APPEND W_CELLCOLOR TO WA_OUT1-CELLCOLORS.
MODIFY GT_OUT FROM WA_OUT1 INDEX P_INDX TRANSPORTING CELLCOLORS.
. ...
..
,,
,,,,,
CALL METHOD gi_grid->set_table_for_first_display
EXPORTING
is_variant = ls_variant
i_save = lv_save
is_layout = ls_layout
it_toolbar_excluding = lt_exclude
CHANGING
it_outtab = gt_out
it_fieldcatalog = lt_fcat
EXCEPTIONS
OTHERS = 1.
Regards ,
REZA ROSTAMI / ABAP / MAPNA
2011.09 .26
2011 Sep 28 2:01 PM
See the following example:
DEFINE m_fieldcat.
add 1 to ls_fieldcat-col_pos.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
append ls_fieldcat to lt_fieldcat.
END-OF-DEFINITION.
TYPE-POOLS: slis. " ALV Global types
SELECTION-SCREEN :
SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max.
PARAMETERS p_max(2) TYPE n DEFAULT '30' OBLIGATORY.
SELECTION-SCREEN END OF LINE.
TYPES :
BEGIN OF ty_data,
vkorg TYPE vbak-vkorg, " Sales organization
kunnr TYPE vbak-kunnr, " Sold-to party
vbeln TYPE vbak-vbeln, " Sales document
netwr TYPE vbak-netwr, " Net Value of the Sales Order
END OF ty_data,
BEGIN OF ty_vbak,
vkorg TYPE vbak-vkorg, " Sales organization
kunnr TYPE vbak-kunnr, " Sold-to party
vbeln TYPE vbak-vbeln, " Sales document
netwr TYPE vbak-netwr, " Net Value of the Sales Order
tabcolor TYPE lvc_t_scol, " Cell Color
END OF ty_vbak.
DATA:
gt_data TYPE TABLE OF ty_data,
gt_vbak TYPE TABLE OF ty_vbak.
INITIALIZATION.
v_1 = 'Maximum of records to read'.
2011 Sep 28 2:03 PM
Continue........
START-OF-SELECTION.
PERFORM read_data.
PERFORM fill_color.
PERFORM display_data.
FORMread_data.
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM vbak UP TO p_max ROWS.
ENDFORM.
FORM fill_color.
DATA :
ls_data TYPE ty_data,
ls_vbak TYPE ty_vbak.
LOOP AT gt_data INTO ls_data.
CLEAR ls_vbak.
MOVE-CORRESPONDING ls_data TO ls_vbak.
PERFORM f_modify_color USING 'NETWR' CHANGING ls_vbak.
PERFORM f_modify_color USING 'VBELN' CHANGING ls_vbak.
* Fill gt_vbak
APPEND ls_vbak TO gt_vbak.
ENDLOOP.
ENDFORM.
FORM f_modify_color USING u_fieldname TYPE lvc_fname
CHANGING us_vbak TYPE ty_vbak.
DATA :
l_rnd_value TYPE integer2,
ls_tabcolor TYPE lvc_s_scol.
* Random value
CALL FUNCTION 'RANDOM_I2'
EXPORTING
rnd_min = 0
rnd_max = 3
IMPORTING
rnd_value = l_rnd_value.
CLEAR ls_tabcolor.
ls_tabcolor-fname = u_fieldname.
CASE l_rnd_value.
WHEN 0.
ls_tabcolor-color-col = 1. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
WHEN 1.
ls_tabcolor-color-col = 3. " Yellow.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
WHEN 2.
ls_tabcolor-color-col = 5. " Green.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
WHEN 3.
ls_tabcolor-color-col = 6. " Red.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
ENDCASE.
INSERT ls_tabcolor INTO TABLE us_vbak-tabcolor.
ENDFORM.
2011 Sep 28 2:04 PM
Continue........
FORM display_data.
DATA:
ls_layout TYPE slis_layout_alv,
ls_fieldcat TYPE slis_fieldcat_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv.
* Build the field catalog
m_fieldcat 'VKORG' 'VBAK'.
m_fieldcat 'KUNNR' 'VBAK'.
m_fieldcat 'VBELN' 'VBAK'.
m_fieldcat 'NETWR' 'VBAK'.
* Fill Layout
ls_layout-coltab_fieldname = 'TABCOLOR'.
* Display the list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout = ls_layout
it_fieldcat = lt_fieldcat
TABLES
t_outtab = gt_vbak.
ENDFORM. " F_DISPLAY_DATA
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |