sexta-feira, 11 de junho de 2010

Inclusão de COR em determinada Celula do ALV

Este é um exemplo de código onde conseguimos definir cor em uma única celula de um ALV por função.
A dica é basicamente incluir na tabela de saida o elemento de dados tabcolor type lvc_t_scol e colocar os valores neles referentes a cada linha...

*&---------------------------------------------------------------------*
*& Report Z_ALV_CELL_COLOR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report Z_ALV_CELL_COLOR.


*---------------------------------------------------------------------*
* Example of ALV with Cell color *
*---------------------------------------------------------------------*
* Author : Michel PIOUD *
*---------------------------------------------------------------------*
* Macro definition
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. "#EC NEEDED
parameters p_max(2) type n default '30' obligatory. "#EC *
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,

* Data displayed
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,
* Data displayed
gt_vbak type table of ty_vbak.

*---------------------------------------------------------------------*
initialization.

v_1 = 'Maximum of records to read'. "#EC NOTEXT

*---------------------------------------------------------------------*
start-of-selection.
break abaptista.
perform f_read_data.

perform f_fill_color.

perform f_display_data.

*---------------------------------------------------------------------*
* Form f_read_data_vbak
*---------------------------------------------------------------------*
form f_read_data.

select * into corresponding fields of table gt_data
from vbak up to p_max rows.

endform. " F_READ_DATA
*--------------------------------------------------------------------*
* Form f_fill_color
*--------------------------------------------------------------------*
form f_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. " F_FILL_COLOR
*---------------------------------------------------------------------*
* Form F_modify_color
*---------------------------------------------------------------------*
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. " F_MODIFY_COLOR
*---------------------------------------------------------------------*
* Form f_display_data
*---------------------------------------------------------------------*
form f_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
***************** END OF PROGRAM Z_ALV_CELL_COLOR *********************