感谢狂刀 感谢tyy.yang
【转】
ALV中能不能只改变字体的颜色,而不改变背景颜色,已经搞定了
方法1
ls_fieldcat-emphasize = 'C611'. "red
REPORT ztest NO STANDARD PAGE HEADING.
* ALV
TYPE-POOLS: slis.
TYPES: BEGIN OF ty_data,
key(10),
2 type string,
text TYPE c LENGTH 50,
END OF ty_data.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
gs_layout TYPE slis_layout_alv.
* Data to be displayed
DATA: gt_data TYPE TABLE OF ty_data WITH KEY key WITH HEADER LINE.
*---------------------------------------------------------------------*
INITIALIZATION.
PERFORM e01_fieldcat_init USING gt_fieldcat[].
.
*---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM selection.
*---------------------------------------------------------------------*
END-OF-SELECTION.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
* CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = gs_layout
it_fieldcat = gt_fieldcat[]
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
*---------------------------------------------------------------------*
* --> E01_LT_FIELDCAT *
*---------------------------------------------------------------------*
FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
* Key
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'KEY'.
ls_fieldcat-tabname = 'GT_DATA'.
ls_fieldcat-reptext_ddic = 'KEY'.
ls_fieldcat-outputlen = 10.
APPEND ls_fieldcat TO e01_lt_fieldcat.
* Test number
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = '2'.
ls_fieldcat-tabname = 'GT_DATA'.
ls_fieldcat-reptext_ddic = 'Ord#'.
ls_fieldcat-emphasize = 'C611'. " red
ls_fieldcat-outputlen = 5.
APPEND ls_fieldcat TO e01_lt_fieldcat.
* Text
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'TEXT'.
ls_fieldcat-tabname = 'GT_DATA'.
* ls_fieldcat-emphasize = 'C681'.
ls_fieldcat-reptext_ddic = 'Text'.
ls_fieldcat-outputlen = 40.
APPEND ls_fieldcat TO e01_lt_fieldcat.
ENDFORM. "e01_fieldcat_init
*---------------------------------------------------------------------*
* FORM SELECTION *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM selection.
gt_data-key = 'Group 1'. gt_data-2 = '19734'. gt_data-text = 'This is group 1'.
APPEND gt_data.
gt_data-key = 'Group 2'. gt_data-text = 'This is group 2'.
APPEND gt_data.
ENDFORM. "selection
****************
方法2
来自狂刀
DATA: it_events TYPE slis_t_event,
x_events TYPE slis_alv_event.
x_events-name = 'END_OF_LIST'.
x_events-form = 'LIST_MODIFY_OUPUT'.
APPEND x_events TO it_events.
声明这个
显示的时候加入EVENTS参数
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = it_fieldcat
it_events = it_events
TABLES
t_outtab = it_vbap
EXCEPTIONS
program_error = 1
OTHERS = 2.
再添加对应事件的代码
完整的代码:
REPORT z7258_alv_color.
*REPORT ZTESTALV.
TYPE-POOLS: slis.
INCLUDE <icon>.
*- Fieldcatalog
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
it_fieldcat1 TYPE slis_t_fieldcat_alv..
DATA: x_fieldcat TYPE slis_fieldcat_alv,
x_fieldcat1 TYPE slis_fieldcat_alv.
DATA: it_events TYPE slis_t_event,
x_events TYPE slis_alv_event.
x_events-name = 'END_OF_LIST'.
x_events-form = 'LIST_MODIFY_OUPUT'.
APPEND x_events TO it_events.
DATA: BEGIN OF it_vbap OCCURS 0,
vbeln LIKE vbap-vbeln,
posnr LIKE vbap-posnr,
flag(1),
END OF it_vbap.
SELECT vbeln
posnr
UP TO 10 ROWS
INTO TABLE it_vbap
FROM vbap.
LOOP AT it_vbap.
IF sy-tabix = 1 OR sy-tabix = 5.
it_vbap-flag = 'X'.
MODIFY it_vbap INDEX sy-tabix.
ENDIF.
ENDLOOP.
DATA:l_pos TYPE i VALUE 1.
CLEAR: l_pos.l_pos = l_pos + 1.
x_fieldcat-seltext_m = 'VBELN'.
x_fieldcat-fieldname = 'VBELN'.
x_fieldcat-tabname = 'IT_VBAP'.
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_m = 'POSNR'.
x_fieldcat-fieldname = 'POSNR'.
x_fieldcat-tabname = 'IT_VBAP'.
x_fieldcat-col_pos = l_pos.
x_fieldcat-outputlen = '5'.
APPEND x_fieldcat TO it_fieldcat.
CLEAR x_fieldcat.l_pos = l_pos + 1.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = it_fieldcat
it_events = it_events
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.
*&--------------------------------------------------------------------*
*& FormLIST_MODIFY_OUPUT
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM list_modify_ouput.
DATA: l_vbeln LIKE vbak-vbeln,
l_posnr LIKE vbap-posnr,
l_index TYPE sy-index.
CLEAR it_vbap.
DO 20 TIMES.
CLEAR: l_vbeln, l_posnr.
READ LINE sy-index INDEX sy-lsind
FIELD VALUE it_vbap-vbeln INTO l_vbeln
it_vbap-posnr INTO l_posnr .
"3lines are reserved for alv headings , so i am reading it form 4th line
"so 4th line is equal to 1st line of itab
IF sy-subrc = 0 AND sy-index GE 4.
l_index = sy-index - 3.
READ TABLE it_vbap INDEX l_index.
IF sy-subrc = 0 AND it_vbap-flag = 'X'.
*-Modifying current list
MODIFY LINE sy-index INDEX sy-lsind
FIELD FORMAT it_vbap-posnr COLOR 6 INVERSE.
MODIFY LINE sy-index INDEX sy-lsind
FIELD FORMAT it_vbap-vbeln COLOR 6 INVERSE.
ENDIF.
ENDIF.
ENDDO.
ENDFORM. "LIST_MODIFY_OUPUT