本文转自http://scnblogs.techweb.com.cn/qq19851116/archives/3.html
ALV的颜色控制可以分3中级别:
1.数据LIST中某一行颜色的控制
2.数据LIST中某一列颜色的控制
3.数据LIST中某个单元格颜色的控制
如果在ALV中这三种颜色控制同时存在时,它们会有一个优先级的。单元格控制的优先级别最高,下来是行控制的优先级,最后是列控制的优先级。
效果如下
代码如下
TH_LAYOUT-coltab_fieldname
这个属性里面放的是内表中表示行颜色字段的CODE
TH_FIELDCAT-emphasize = ‘C110′.
这个属性里面放的是内表中表示列颜色字段的属性,在ALV的FIELDCAT属相设置时把颜色代码值(C110)填入即可.
TH_LAYOUT-info_fieldname = ‘LNCOLOR’.
这里面放的字段是内表中单元格颜色字段的CODE.
COLOR TYPE slis_t_specialcol_alv,
在内表中要定义一个字段(COLOR)参考这个内表,slis_t_specialcol_alv它里面有FIELDNAME,COL,INT,INV属性需要设置。分别是字段名字,颜色代码,背景深浅度,字体深浅度控制。
TH_COLOR-fieldname = ‘COUNTRYFR’.
TH_COLOR-color-col = ‘6′.
TH_COLOR-color-int = ‘1′.
TH_COLOR-color-inv = ‘1′.
REPORT ZTEST_SPFLI MESSAGE-ID zf.
INCLUDE
INCLUDE
TYPE-POOLS:slis.
TABLES:
SPFLI.
TYPES:
BEGIN OF TYP_SPFLI,
CARRID TYPE SPFLI-CARRID,
CONNID TYPE SPFLI-CONNID,
COUNTRYFR TYPE SPFLI-COUNTRYFR,
COUNTRYTO TYPE SPFLI-COUNTRYTO,
CITYFROM TYPE SPFLI-CITYFROM,
CITYTO TYPE SPFLI-CITYTO,
END OF TYP_SPFLI,
BEGIN OF TYP_OUTPUT,
CHECK TYPE C,
ICON TYPE icon_d,
SYMBOL TYPE icon_d,
CARRID TYPE SPFLI-CARRID,
CONNID TYPE SPFLI-CONNID,
COUNTRYFR TYPE SPFLI-COUNTRYFR,
COUNTRYTO TYPE SPFLI-COUNTRYTO,
CITYFROM TYPE SPFLI-CITYFROM,
CITYTO TYPE SPFLI-CITYTO,
COLOR TYPE slis_t_specialcol_alv,
LNCOLOR TYPE CHAR4,
HAND TYPE INT4,
LINK TYPE string,
END OF TYP_OUTPUT.
DATA:
TH_SPFLI TYPE TYP_SPFLI,
TD_SPFLI TYPE TABLE OF TYP_SPFLI,
BEGIN OF TYP_DATA.
INCLUDE STRUCTURE zcddi.
DATA: A1 TYPE C,
A3 TYPE N,
* zcddi
END OF TYP_DATA,
TH_COLOR TYPE slis_specialcol_alv,
TD_COLOR TYPE TABLE OF slis_specialcol_alv,
TH_LAYOUT TYPE SLIS_LAYOUT_ALV,
TH_FIELDCAT TYPE slis_fieldcat_alv,
TD_FIELDCAT TYPE TABLE OF slis_fieldcat_alv,
TH_OUTPUT TYPE TYP_OUTPUT,
TD_OUTPUT TYPE TABLE OF TYP_OUTPUT.
START-OF-SELECTION.
* PARAMETERS: gjahr LIKE glpca-RYEAR MEMORY ID gjr OBLIGATORY.
SELECT-OPTIONS:
S_CARRID FOR SPFLI-CARRID,
S_CONNID FOR SPFLI-CONNID.
PERFORM FRM_GET_DATA.
PERFORM FRM_EDIT_DATA.
PERFORM FRM_DISPLAY_DATA.
*&———————————————————————*
*& Form FRM_GET_DATA
*&———————————————————————*
* GET MAIN DATA
*———————————————————————-*
FORM FRM_GET_DATA .
SELECT CARRID
CONNID
COUNTRYFR
COUNTRYTO
CITYFROM
CITYTO
INTO TABLE TD_SPFLI
FROM SPFLI
WHERE CARRID IN S_CARRID
AND CONNID IN S_CONNID.
IF SY-SUBRC <> 0.
MESSAGE S001 DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. ” FRM_GET_DATA
*&———————————————————————*
*& Form FRM_DISPLAY_DATA
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_DISPLAY_DATA .
PERFORM FRM_SET_TITLE.
PERFORM FRM_SET_FIELDCAT.
PERFORM FRM_CALL_FUNCTION.
ENDFORM. ” FRM_DISPLAY_DATA
*&———————————————————————*
*& Form FRM_SET_TITLE
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_SET_TITLE .
TH_LAYOUT-colwidth_optimize &#61; ‘X’.
TH_LAYOUT-coltab_fieldname &#61; ‘COLOR’.
TH_LAYOUT-window_titlebar &#61; ’spfli data’.
TH_LAYOUT-detail_popup &#61; ‘X’.
TH_LAYOUT-COLWIDTH_OPTIMIZE &#61; ‘X’.
TH_LAYOUT-info_fieldname &#61; ‘LNCOLOR’.
ENDFORM. ” FRM_SET_TITLE
*&———————————————————————*
*& Form FRM_SET_FIELDCAT
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_SET_FIELDCAT .
PERFORM FRM_SET_VALUE USING ‘1′ ‘CHECK’ ‘CHECK’ ‘1′ ‘X’.
PERFORM FRM_SET_VALUE USING ‘2′ ‘ICON’ ‘ICON’ ‘5′ ‘ ‘.
PERFORM FRM_SET_VALUE USING ‘3′ ‘SYMBOL’ ‘SYMBOL’ ‘5′ ‘ ‘.
PERFORM FRM_SET_VALUE USING ‘4′ ‘CARRID’ ‘航线承运人ID’ ‘3′ ”.
PERFORM FRM_SET_VALUE USING ‘5′ ‘CONNID’ ‘航班连接 Id’ ‘4′ ”.
PERFORM FRM_SET_VALUE USING ‘6′ ‘COUNTRYFR’ ‘国家代码FROM’ ‘3′ ”.
PERFORM FRM_SET_VALUE USING ‘7′ ‘COUNTRYTO’ ‘国家代码TO’ ‘3′ ”.
PERFORM FRM_SET_VALUE USING ‘8′ ‘CITYFROM’ ‘起飞城市’ ‘20′ ”.
PERFORM FRM_SET_VALUE USING ‘9′ ‘CITYTO’ ‘目标城市’ ‘20′ ”.
ENDFORM. ” FRM_SET_FIELDCAT
*&———————————————————————*
*& Form FRM_SET_VALUE
*&———————————————————————*
* text
*———————————————————————-*
* –>P_COL_POS text NO_ROWMARK
* –>P_FIELDNAME text
* –>P_SELTEXT_L text
* –>P_OUTPUTLEN text
*———————————————————————-*
FORM FRM_SET_VALUE USING I_COL_POS
I_FIELDNAME
I_SELTEXT_L
I_OUTPUTLEN
I_CHECKBOX.
TH_FIELDCAT-COL_POS &#61; I_COL_POS.
TH_FIELDCAT-FIELDNAME &#61; I_FIELDNAME.
TH_FIELDCAT-SELTEXT_L &#61; I_SELTEXT_L.
TH_FIELDCAT-OUTPUTLEN &#61; I_OUTPUTLEN.
IF I_FIELDNAME &#61; ‘CARRID’.
ELSEIF I_FIELDNAME &#61; ‘CHECK’.
TH_FIELDCAT-SELTEXT_M &#61; ‘SELECT’.
TH_FIELDCAT-CHECKBOX &#61; ‘X’.
TH_FIELDCAT-OUTPUTLEN &#61; 1.
TH_FIELDCAT-EDIT &#61; ‘X’.
ELSEIF I_FIELDNAME &#61; ‘CITYFROM’ OR I_FIELDNAME &#61; ‘CITYTO’.
TH_FIELDCAT-emphasize &#61; ‘C110′.
ELSEIF I_FIELDNAME &#61; ‘ICON’.
TH_FIELDCAT-icon &#61; ‘X’.
ELSEIF I_FIELDNAME &#61; ‘SYMBOL’.
TH_FIELDCAT-symbol &#61; ‘X’.
ENDIF.
APPEND TH_FIELDCAT TO TD_FIELDCAT.
CLEAR TH_FIELDCAT.
ENDFORM. ” FRM_SET_VALUE
*&———————————————————————*
*& Form FRM_CALL_FUNCTION
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_CALL_FUNCTION .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
I_CALLBACK_USER_COMMAND &#61; ‘FRM_SET_COMMAND’
IS_LAYOUT &#61; TH_LAYOUT
IT_FIELDCAT &#61; TD_FIELDCAT
I_SAVE &#61; ‘A’
TABLES
T_OUTTAB &#61; TD_OUTPUT
EXCEPTIONS
PROGRAM_ERROR &#61; 1
OTHERS &#61; 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.
ENDFORM. ” FRM_CALL_FUNCTION
*&———————————————————————*
*& Form FRM_EDIT_DATA
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_EDIT_DATA .
DATA:
LW_INDEX TYPE SY-INDEX,
LW_MOD TYPE I.
LOOP AT TD_SPFLI INTO TH_SPFLI.
LW_INDEX &#61; SY-TABIX.
LW_MOD &#61; LW_INDEX MOD 3.
IF TH_SPFLI-CARRID IS INITIAL .
TH_COLOR-fieldname &#61; ‘CARRID’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
IF TH_SPFLI-CONNID IS INITIAL.
TH_COLOR-fieldname &#61; ‘CONNID’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
IF TH_SPFLI-COUNTRYFR IS INITIAL.
TH_COLOR-fieldname &#61; ‘COUNTRYFR’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
IF TH_SPFLI-COUNTRYTO IS INITIAL.
TH_COLOR-fieldname &#61; ‘COUNTRYTO’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ELSEIF TH_SPFLI-COUNTRYTO &#61; ‘MQ’.
TH_COLOR-fieldname &#61; ‘COUNTRYTO’.
TH_COLOR-color-col &#61; ‘5′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ELSEIF TH_SPFLI-COUNTRYTO &#61; ‘CA’.
TH_COLOR-fieldname &#61; ‘COUNTRYTO’.
TH_COLOR-color-col &#61; ‘7′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
IF TH_SPFLI-CITYFROM IS INITIAL.
TH_COLOR-fieldname &#61; ‘CITYFROM’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
IF TH_SPFLI-CITYTO IS INITIAL.
TH_COLOR-fieldname &#61; ‘CITYTO’.
TH_COLOR-color-col &#61; ‘6′.
TH_COLOR-color-int &#61; ‘1′.
TH_COLOR-color-inv &#61; ‘1′.
APPEND TH_COLOR TO TD_COLOR.
CLEAR TH_COLOR.
ENDIF.
MOVE-CORRESPONDING TH_SPFLI TO TH_OUTPUT .
CLEAR TH_SPFLI.
TH_OUTPUT-COLOR[] &#61; TD_COLOR[].
IF LW_INDEX >&#61; 3 AND LW_INDEX &#61;<13.
TH_OUTPUT-LNCOLOR &#61; ‘C700′.
ENDIF.
* TH_OUTPUT-HAND &#61; SY-TABIX.
* TH_OUTPUT-LINK &#61; ‘WWW.BAIDU.COM’.
IF LW_MOD <> 0.
TH_OUTPUT-ICON &#61; ICON_TIME.
TH_OUTPUT-SYMBOL &#61; SYM_LEFT_HAND.
ELSE.
TH_OUTPUT-ICON &#61; ICON_COLOR.
TH_OUTPUT-SYMBOL &#61; sym_folder.
ENDIF.
APPEND TH_OUTPUT TO TD_OUTPUT.
CLEAR:
LW_INDEX,
LW_MOD,
TH_OUTPUT.
REFRESH:
TD_COLOR.
ENDLOOP.
ENDFORM. ” FRM_EDIT_DATA
FORM FRM_SET_COMMAND USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN ‘&IC1′. “双击事件
* READ TABLE itab INTO wa INDEX rs_selfield-tabindex.
*
* SET PARAMETER ID ‘BLN’ FIELD wa-belnr.
* SET PARAMETER ID ‘BUK’ FIELD wa-bukrs.
* SET PARAMETER ID ‘GJR’ FIELD wa-gjahr.
*
* CALL TRANSACTION ‘FB03′ AND SKIP FIRST SCREEN.
ENDCASE.
ENDFORM.