ABAP FUNCTION ALV单元格单击/双击事件

为实现打印入库单时,勾选某个行项目,则同一个单子的行项都要自动勾上。需要用到单击事件。

1.为需要响应单击按钮的列设定:

       WA_FIELDCAT-HOTSPOT 'X'.  "单击    其中 WA_FIELDCAT     TYPE LINE OF SLIS_T_FIELDCAT_ALV,

2.在ALV事件里响应:

  CASE R_UCOMM.

    WHEN '&IC1'. "单击复选框 勾选(取消)后同一预留号都自动勾选(取消)
      READ TABLE ITAB_TOTAL INDEX R_SEL-tabindex INTO DATA(WA_SEL_TEMP).  "获取单击行
      IF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = ''.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL(<FS_TEMP>) WHERE RSNUM = WA_SEL_TEMP-RSNUM .
            <FS_TEMP>-SEL = 'X'.
          ENDLOOP.
        ELSEIF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = 'X'.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL(<FS_TEMP2>) WHERE RSNUM = WA_SEL_TEMP-RSNUM  .
            <FS_TEMP2>-SEL = ''.
          ENDLOOP.
      ENDIF.
     3.最后注意响应事件前加GET_GLOBALS_FROM_SLVC_FULLSCR,响应后刷新 CALL  METHOD  REF ->REFRESH_TABLE_DISPLAY.


*********************************************************************************
*  程序名:ZWMRP008
*  程序名称:入库单打印
*-------------------------------------------------
*  创建日期       程序员    SAP版本    程序类型
*  2016-09-22     yhy     RP
*-------------------------------------------------
*  描述:
*成本中心领用,线边退料,成品工废、料废订单消耗领用,成品返工领用,
*项目领用,废品消耗领用,备件消耗领用
*=================================================
*  修改日期   版本    修改人      修改描述
*
********************************************************************************
REPORT ZWMRP008.
*------------------------------------------------------------------------------*
*                             GLOBLE-DEFINATION                                *
*------------------------------------------------------------------------------*
TABLES:RESB.
*ALV输出用变量定义
*----ALV变量
TYPE-POOLS slis.
TYPE-POOLS: icon.
DATA: WA_FIELDCAT             TYPE LINE OF SLIS_T_FIELDCAT_ALV,
      ITAB_FIELDCAT           TYPE SLIS_T_FIELDCAT_ALV,
      GT_EVENTS               TYPE SLIS_T_EVENT,
      WA_LAYOUT               TYPE SLIS_LAYOUT_ALV,
      ITAB_LAYOUT             TYPE SLIS_LAYOUT_ALV,
      I_GRID_SETTINGS         TYPE  LVC_S_GLAY.

*----ALV宏
DEFINE ADD_FIELD.
  CLEAR WA_FIELDCAT.
  WA_FIELDCAT-FIELDNAME     = &1.
  WA_FIELDCAT-TABNAME       = &2.
  WA_FIELDCAT-SELTEXT_M     = &3.
  WA_FIELDCAT-CHECKBOX      = &4.
  WA_FIELDCAT-EDIT          = &5.
  WA_FIELDCAT-NO_ZERO       = 'X'. "无前导0
  WA_FIELDCAT-REF_TABNAME   = &6.
  WA_FIELDCAT-REF_FIELDNAME = &7.
  IF WA_FIELDCAT-FIELDNAME = 'SEL'.
    WA_FIELDCAT-HOTSPOT = 'X'.  "单击
  ENDIF.

  APPEND WA_FIELDCAT TO ITAB_FIELDCAT.
END-OF-DEFINITION.

TYPES: BEGIN OF TY_TOTAL,  "
      SEL TYPE C,
      RSNUM	TYPE RESB-RSNUM,"预留号
      RSPOS	TYPE RESB-RSPOS,"预留行项目
      WERKS	TYPE RESB-WERKS ,"工厂
      LGORT	TYPE RESB-LGORT ,"库存地
      MATNR	TYPE RESB-MATNR ,"物料代码
      BDTER	TYPE RESB-BDTER ,"需求日期
      BDMNG	TYPE RESB-BDMNG ,"数量
      MEINS	TYPE RESB-MEINS ,"单位
      BWART	TYPE RESB-BWART ,"移动类型
      BWART_C TYPE T156T-BTEXT,"移动类型文本
      UMWRK	TYPE RESB-UMWRK ,"接收工厂
      UMLGO	TYPE RESB-UMLGO ,"接收库存地
      AUFNR	TYPE RESB-AUFNR ,"订单
      PSPEL	TYPE RESB-PSPEL ,"WBS元素

      TPLNR TYPE VIAFKOS-TPLNR, "功能位置
      PLTXT TYPE IFLOTX-PLTXT,  "功能位置描述
      EQUNR TYPE VIAFKOS-EQUNR, "设备编号
      EQKTX  TYPE EQKT-EQKTX,    "设备描述
      MAKTX TYPE MAKT-MAKTX,"物料描述
      VERAK TYPE CSKS-VERAK,"成本中心描述---使用部门

 END OF TY_TOTAL.

DATA: ITAB_TOTAL TYPE TABLE OF TY_TOTAL,
      WA_TOTAL   TYPE TY_TOTAL.
DATA: ITAB_SF TYPE TABLE OF TY_TOTAL.
*-----------------------------------------------------------------------
* TYPE-POOLS
*-----------------------------------------------------------------------


*------------------------------------------------------------------------------*
*                             SELECTION-SCREEN                                 *
*------------------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_BWART TYPE RESB-BWART OBLIGATORY,
            P_WERKS TYPE RESB-WERKS OBLIGATORY DEFAULT '2000'.

SELECT-OPTIONS: S_MATNR FOR RESB-MATNR,
                S_RESNUM FOR RESB-RSNUM NO-EXTENSION NO INTERVALS ,
*                S_.                    NO-EXTENSION NO INTERVALS ,  "领用单号-自建表
                S_BDTER FOR RESB-BDTER.
SELECTION-SCREEN END OF BLOCK blk1.

*------------------------------------------------------------------------------*
*                             INITIALIZATION                                 *
*------------------------------------------------------------------------------*

*------------------------------------------------------------------------------*
*                             AT SELECTION-SCREEN                              *
*------------------------------------------------------------------------------*

*------------------------------------------------------------------------------*
*                             START-OF-SELECTION                               *
*------------------------------------------------------------------------------*
  PERFORM P_GET_DATA.
  PERFORM P_DISPLAY.

*------------------------------------------------------------------------------*
*                             SUBROUTINES                                      *
*------------------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  P_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_GET_DATA .

  CLEAR: ITAB_TOTAL,WA_TOTAL.
   "备件消耗领用--PM
  IF P_BWART = 'S61'.
     SELECT
         RESB~RSNUM	"预留号
         RESB~RSPOS	"预留行项目
         RESB~WERKS	"工厂
         RESB~LGORT	"库存地
         RESB~MATNR	"物料代码
         RESB~BDTER	"需求日期
         RESB~BDMNG	"数量
         RESB~MEINS	"单位
         RESB~BWART	"移动类型
         RESB~UMWRK	"接收工厂
         RESB~UMLGO	"接收库存地
         RESB~AUFNR	"订单
         RESB~PSPEL	"WBS元素
         VIAFKOS~TPLNR  "功能位置
         IFLOTX~PLTXT "功能位置描述
         VIAFKOS~EQUNR "设备编号
         EQKT~EQKTX    "设备描述
       INTO CORRESPONDING FIELDS OF TABLE ITAB_TOTAL
       FROM RESB INNER JOIN VIAFKOS ON RESB~AUFNR = VIAFKOS~AUFNR
       INNER JOIN IFLOTX ON VIAFKOS~TPLNR = IFLOTX~TPLNR AND IFLOTX~SPRAS = 1
       INNER JOIN EQKT ON VIAFKOS~EQUNR = EQKT~EQUNR  AND EQKT~SPRAS = 1
       WHERE  RESB~WERKS = P_WERKS
       AND RESB~BWART = P_BWART
       AND RESB~MATNR IN S_MATNR
       AND RESB~RSNUM IN S_RESNUM
       AND RESB~BDTER IN S_BDTER.

    ELSE.

     SELECT
        RSNUM	"预留号
        RSPOS	"预留行项目
        WERKS	"工厂
        LGORT	"库存地
        MATNR	"物料代码
        BDTER	"需求日期
        BDMNG	"数量
        MEINS	"单位
        BWART	"移动类型
        UMWRK	"接收工厂
        UMLGO	"接收库存地
        AUFNR	"订单
        PSPEL	"WBS元素
      INTO CORRESPONDING FIELDS OF TABLE ITAB_TOTAL
      FROM RESB
      WHERE  WERKS = P_WERKS
      AND BWART = P_BWART
      AND MATNR IN S_MATNR
      AND RSNUM IN S_RESNUM
      AND BDTER IN S_BDTER.
*      AND 领用单号-自建表
  ENDIF.

   IF ITAB_TOTAL IS INITIAL.
     MESSAGE '无数据,请确认查询条件!' TYPE 'S'.
     STOP.
   ENDIF.



   LOOP AT ITAB_TOTAL INTO WA_TOTAL.
     SELECT SINGLE MAKTX
       INTO WA_TOTAL-MAKTX
       FROM MAKT
       WHERE MATNR = WA_TOTAL-MATNR.

     SELECT SINGLE
       BTEXT
       INTO WA_TOTAL-BWART_C
       FROM T156T
       WHERE SPRAS = 1
       AND BWART = WA_TOTAL-BWART.
*取成本中心描述
     SELECT SINGLE
       CSKS~VERAK
       INTO WA_TOTAL-VERAK
       FROM RKPF INNER JOIN CSKS ON RKPF~KOSTL = CSKS~KOSTL
       WHERE RKPF~RSNUM = WA_TOTAL-RSNUM.

     MODIFY ITAB_TOTAL FROM WA_TOTAL.
     CLEAR WA_TOTAL.
   ENDLOOP.

ENDFORM.












*&---------------------------------------------------------------------*
*&      Form  P_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_DISPLAY .
*        SEL TYPE C,

  ADD_FIELD   'SEL' 'ITAB_TOTAL' '选择' 'X' 'X' SPACE SPACE  .
*  ADD_FIELD   ''  'ITAB_TOTAL' '领用单号' SPACE SPACE .
  ADD_FIELD   'BWART_C' 'ITAB_TOTAL' '单据类型' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MATNR'  'ITAB_TOTAL' '物料代码' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MAKTX' 'ITAB_TOTAL' '物料描述' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'BDTER'  'ITAB_TOTAL' '日期' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'AUFNR' 'ITAB_TOTAL' '订单' SPACE SPACE SPACE SPACE.
  IF P_BWART = 'S61'.
    ADD_FIELD   'TPLNR' 'ITAB_TOTAL' '功能位置' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'PLTXT' 'ITAB_TOTAL' '功能位置描述' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'EQUNR' 'ITAB_TOTAL' '设备编号' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'EQKTX' 'ITAB_TOTAL' '设备描述' SPACE SPACE SPACE SPACE.
  ENDIF.
  ADD_FIELD   'PSPEL'  'ITAB_TOTAL' 'WBS元素' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'RSNUM'  'ITAB_TOTAL' '预留号' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'RSPOS' 'ITAB_TOTAL' '行项号' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'BDMNG'  'ITAB_TOTAL' '数量' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MEINS'  'ITAB_TOTAL' '单位' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'LGORT'  'ITAB_TOTAL' '发出库存地' SPACE SPACE 'RESB' 'LGORT'.
  ADD_FIELD   'UMLGO'  'ITAB_TOTAL' '接收库存地' SPACE SPACE 'RESB' 'UMLGO'.
******build layout
  CLEAR ITAB_LAYOUT.
  WA_LAYOUT-ZEBRA             = 'X'.
*  LW_LAYOUT-INFO_FIELDNAME    = 'COLOR'.
*  WA_LAYOUT-BOX_FIELDNAME = 'SEL'.
  WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  MOVE-CORRESPONDING WA_LAYOUT TO ITAB_LAYOUT.

*****ALV DISPLY
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM       = SY-REPID
      IS_LAYOUT                = ITAB_LAYOUT
      IT_FIELDCAT              = ITAB_FIELDCAT[]
*     IT_EVENTS                = GT_EVENTS[]
      I_CALLBACK_PF_STATUS_SET = 'P_SET_STATUS'
*      I_SAVE                   = 'A'
      I_CALLBACK_USER_COMMAND  = 'P_USER_COMMAND'
    TABLES
      T_OUTTAB                 = ITAB_TOTAL[]
    EXCEPTIONS
      PROGRAM_ERROR            = 1
      OTHERS                   = 2.
  IF SY-SUBRC <> 0.
    MESSAGE '采用ALV显示报表出错' TYPE 'S'.
    EXIT.

  ENDIF.
ENDFORM.

FORM P_SET_STATUS USING extab TYPE slis_t_extab.
   SET  PF-STATUS  'Z_ST08' .
ENDFORM.

FORM P_USER_COMMAND USING R_UCOMM TYPE SY-UCOMM
                             R_SEL   TYPE SLIS_SELFIELD.  "响应ALV点击自定义按钮之后的事件
  DATA: LV_ANSWER        TYPE C.
  DATA: REF TYPE REF TO CL_GUI_ALV_GRID.
  DATA : LV_STABLE TYPE LVC_S_STBL.
  LV_STABLE-ROW = '1'.
  LV_STABLE-COL = '1'.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID = REF.
  CALL METHOD REF->CHECK_CHANGED_DATA.

  CASE R_UCOMM.

    WHEN '&IC1'. "单击复选框 勾选(取消)后同一预留号都自动勾选(取消)
      READ TABLE ITAB_TOTAL INDEX R_SEL-tabindex INTO DATA(WA_SEL_TEMP).  "获取单击行
      IF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = ''.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL(<FS_TEMP>) WHERE RSNUM = WA_SEL_TEMP-RSNUM .
            <FS_TEMP>-SEL = 'X'.
          ENDLOOP.
        ELSEIF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = 'X'.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL(<FS_TEMP2>) WHERE RSNUM = WA_SEL_TEMP-RSNUM  .
            <FS_TEMP2>-SEL = ''.
          ENDLOOP.
      ENDIF.



    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN '&F12'.
    WHEN 'SEL_ALL'.
      CLEAR WA_TOTAL.
      LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = ''.
        WA_TOTAL-SEL = 'X'.
        MODIFY ITAB_TOTAL FROM WA_TOTAL.
      ENDLOOP.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE ..
    WHEN 'DESEL_ALL'.
      CLEAR WA_TOTAL.
      LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
        WA_TOTAL-SEL = ''.
        MODIFY ITAB_TOTAL FROM WA_TOTAL.
      ENDLOOP.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE .
    WHEN 'PRINT'.
      PERFORM P_PRINT.
   ENDCASE.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE .
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  P_PRINT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_PRINT .
  DATA: FM_NAME TYPE FPNAME.
 DATA:L_CONTROL_PARAMETERS TYPE SSFCTRLOP.
  CLEAR WA_TOTAL.

*  ---------------------------------------------------待完成-根据分类调用不同类型表单
  REFRESH ITAB_SF.
  DATA ITAB_SF_TEMP TYPE TABLE OF TY_TOTAL.
  LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
    APPEND WA_TOTAL TO ITAB_SF_TEMP.
  ENDLOOP.
  IF ITAB_SF_TEMP IS INITIAL.
    MESSAGE '请勾选需要打印的项目' TYPE 'E'.
  ENDIF.
  DESCRIBE TABLE ITAB_SF_TEMP LINES DATA(SF_LINES).
  LOOP AT ITAB_SF_TEMP INTO DATA(WA_SF_TEMP).
    APPEND WA_SF_TEMP TO ITAB_SF.

    AT END OF RSNUM.
*        IF SY-TABIX = 1.
*
*           L_CONTROL_PARAMETERS-NO_OPEN = SPACE."首次运行时打开打印对话框
*           L_CONTROL_PARAMETERS-NO_CLOSE = 'X'."并且不关闭假脱机请求
*
*          ELSEIF SY-TABIX = SF_LINES .
*
*           L_CONTROL_PARAMETERS-NO_OPEN = 'X'.
*           L_CONTROL_PARAMETERS-NO_CLOSE = SPACE."最后关闭假脱机准备打印
*
*          ELSE.
*           L_CONTROL_PARAMETERS-NO_OPEN = 'X'.
*           L_CONTROL_PARAMETERS-NO_CLOSE = 'X'."
*        ENDIF.


       CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
        EXPORTING
          FORMNAME = 'ZWMFM005'
        IMPORTING
          FM_NAME  = FM_NAME.
        IF SY-SUBRC <> 0.
          MESSAGE '无法找到FUNCTION NAME' TYPE 'S' DISPLAY LIKE 'E'.
          EXIT.
        ENDIF.

        "CALL THE FUNCTION
        CALL FUNCTION FM_NAME
*        EXPORTING
*            CONTROL_PARAMETERS = L_CONTROL_PARAMETERS
          EXCEPTIONS
            FORMATTING_ERROR = 1
            INTERNAL_ERROR   = 2
            SEND_ERROR       = 3
            USER_CANCELED    = 4
            OTHERS           = 5.
          IF SY-SUBRC <> 0.
            MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 .
          ENDIF.

          CLEAR ITAB_SF.

      ENDAT.


  ENDLOOP.

*  LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
*    APPEND WA_TOTAL TO ITAB_SF.
*  ENDLOOP.
*
*  IF ITAB_SF IS INITIAL.
*    MESSAGE '请勾选需要打印的项目' TYPE 'E'.
*  ENDIF.
*
*  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
*  EXPORTING
*    FORMNAME = 'ZWMFM005'
*  IMPORTING
*    FM_NAME  = FM_NAME.
*  IF SY-SUBRC <> 0.
*    MESSAGE '无法找到function name' TYPE 'S' DISPLAY LIKE 'E'.
*    EXIT.
*  ENDIF.
*
*  "call the function
*  CALL FUNCTION FM_NAME
*    EXCEPTIONS
*      FORMATTING_ERROR = 1
*      INTERNAL_ERROR   = 2
*      SEND_ERROR       = 3
*      USER_CANCELED    = 4
*      OTHERS           = 5.
*    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.



<think>好的,我现在需要帮助用户解决在ABAP中设置ALV单元格颜色的问题。用户已经提供了一些相关的引用资料,我需要结合这些资料来生成示例代码和解释。 首先,我需要确认用户的需求。用户想知道如何设置ALV单元格的颜色,并且需要示例代码。根据引用[1]和[2],设置单元格颜色涉及到在内表中添加一个类型为LVC_T_SCOL的字段,比如CELL_COLOR。每个颜色信息需要包括字段名(FNAME)、颜色代码(COLOR-COL)、强度(COLOR-INT)和反转(COLOR-INV)。 接下来,我需要考虑如何构造这个结构。例如,如果用户想设置某个特定字段的颜色,比如MATNR字段为红色,那么需要在CELL_COLOR中添加相应的条目。引用[1]中提到需要将颜色代码赋值给对应的结构体字段,并且FNAME需要指定哪个字段需要着色。 然后,需要处理字段目录(Field Catalog)。根据引用[1]和[4],在字段目录中,需要将控制参数如CFIELDNAME、CTABNAME等设置为颜色结构的相关信息。例如,CT_FNAME指定颜色字段的名称,这里应为'CELL_COLOR'。 另外,引用[3]提到了事件处理,但用户的问题主要集中在颜色设置,可能暂时不需要涉及事件。不过,代码中可能需要包含ALV的显示方法,比如调用CL_SALV_TABLE或其他ALV类。 现在,我需要整合这些步骤,编写一个示例代码。首先定义内表结构,包含数据字段和CELL_COLOR字段。然后填充数据,并为需要设置颜色的单元格添加颜色条目。接着配置字段目录,指定颜色字段。最后,显示ALV表格。 需要注意语法正确性,比如结构体类型是否正确,字段目录参数是否设置正确。例如,在字段目录中,CT_FNAME应指向颜色字段的名称,而颜色结构中的每个条目必须正确填充FNAME和颜色属性。 可能遇到的问题包括颜色代码是否正确,例如引用[2]提供的链接中有颜色代码,但用户可能需要知道具体的颜色数值,比如红色对应的COL值。通常,颜色代码如1代表蓝色,2代表灰色,3代表黄色,4代表蓝色,5代表绿色,6代表红色,7代表橙色。但需要确认ABAP中的标准颜色代码,可能用户需要参考引用[2]的链接。 最后,生成相关问题,比如如何设置行或列颜色,或者如何根据条件动态设置颜色,以及如何处理单元格的其他样式,如小数位数(引用[4]提到的方法)。这样可以帮助用户进一步了解ALV的其他设置。</think>### ABAP ALV单元格颜色设置步骤及示例代码 #### 一、实现原理 1. 在ALV内表中添加颜色字段$cell_color$,类型为$LVC_T_SCOL$,存储颜色配置信息[^1] 2. 通过字段目录(FIELD CATALOG)绑定颜色控制字段 3. 颜色参数包含: - $FNAME$:需要着色的字段名 - $COLOR-COL$:颜色编号(0-7) - $COLOR-INT$:强度(0=普通,1=高亮) - $COLOR-INV$:反转显示(0=关闭,1=开启) #### 二、完整示例代码 ```abap REPORT z_alv_cell_color. TYPES: BEGIN OF ty_data, matnr TYPE matnr, "物料号 maktx TYPE maktx, "物料描述 menge TYPE menge_d, "数量 cell_color TYPE lvc_t_scol, "颜色字段 END OF ty_data. DATA: lt_data TYPE TABLE OF ty_data, lo_alv TYPE REF TO cl_salv_table. START-OF-SELECTION. "填充测试数据 lt_data = VALUE #( ( matnr = 'MAT001' maktx = '物料A' menge = 100 ) ( matnr = 'MAT002' maktx = '物料B' menge = 200 ) ). "设置第二行MATNR字段为红色 lt_data[2]-cell_color = VALUE #( ( fname = 'MATNR' color-col = 6 color-int = 0 color-inv = 0 ) ). "创建ALV对象 TRY. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = lt_data ). "设置字段目录 DATA(lo_columns) = lo_alv->get_columns( ). lo_columns->set_optimize( abap_true ). "绑定颜色控制字段 DATA(lo_column) = lo_columns->get_column( 'CELL_COLOR' ). lo_column->set_technical( abap_true ). "隐藏颜色字段 "显示ALV lo_alv->display( ). CATCH cx_root INTO DATA(lx_error). MESSAGE lx_error TYPE 'I'. ENDTRY. ``` #### 三、关键配置说明 1. 颜色代码对应表: - 1: 蓝色 - 2: 灰色 - 3: 黄色 - 4: 蓝色 - 5: 绿色 - 6: 红色 - 7: 橙色[^2] 2. 高级用法: - 动态颜色设置:根据业务逻辑动态添加$cell_color$条目 - 多颜色叠加:单个字段可设置多个颜色条件 - 整行颜色:设置$FNAME$为空白字段
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值