【实用】ABAP ALV单元格修改信息提示

586 篇文章 238 订阅
353 篇文章 89 订阅

ABAP日常开发中经常会用到ALV OO(CL_GUI_ALV_GRID)进行清单显示,其中对指定单元格进行操作也是日常必备技能,比如下图中的需求,对指定单元格的“原因代码”列进行修改,校验值的有效性,给出如下所示的提示框,若校验不通过,恢复原值。

类的定义与实现

CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS : handle_data_changed
                 FOR EVENT data_changed OF cl_gui_alv_grid
                 IMPORTING er_data_changed.
ENDCLASS.  

CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
    PERFORM data_changed  USING er_data_changed.
  ENDMETHOD.                    "handle_data_changed
ENDCLASS.  

校验代码

FORM DATA_CHANGED  USING    P_ER_DATA_CHANGED
                   TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
  DATA : LS_MODI        TYPE LVC_S_MODI. 
  DATA : L_MSSG TYPE CHAR100.    


*CELL MODIFY
  LOOP AT P_ER_DATA_CHANGED->MT_GOOD_CELLS INTO LS_MODI.
    CASE LS_MODI-FIELDNAME.


      WHEN 'ZCAUSE'.
        CLEAR GT_LIST.
        READ TABLE GT_LIST INDEX LS_MODI-ROW_ID.
        IF LS_MODI-VALUE IS NOT INITIAL .
          SELECT SINGLE *
            FROM ZMM001
            WHERE ZCAUSE = LS_MODI-VALUE.
          IF SY-SUBRC NE 0.
            CLEAR L_MSSG.
            CONCATENATE LS_MODI-VALUE  '原因代码不存在.'  INTO L_MSSG.
            PERFORM WS_MSG USING 'E' L_MSSG TEXT-001 .
            CALL METHOD P_ER_DATA_CHANGED->MODIFY_CELL
              EXPORTING
                I_ROW_ID    = LS_MODI-ROW_ID
                I_FIELDNAME = LS_MODI-FIELDNAME
                I_VALUE     = GT_LIST-ZCAUSE.

            CONTINUE.
          ELSE.
            GT_LIST-ZCAUSE = LS_MODI-VALUE.
          ENDIF.
        ELSE.
          CLEAR L_MSSG.
          CONCATENATE LS_MODI-VALUE '原因代码不能为空.'  INTO L_MSSG.
          PERFORM WS_MSG USING 'E' L_MSSG TEXT-002 .
          CALL METHOD P_ER_DATA_CHANGED->MODIFY_CELL
            EXPORTING
              I_ROW_ID    = LS_MODI-ROW_ID
              I_FIELDNAME = LS_MODI-FIELDNAME
              I_VALUE     = GT_LIST-ZCAUSE.

          CONTINUE.
        ENDIF.

    ENDCASE.

    MODIFY GT_LIST INDEX LS_MODI-ROW_ID.
  ENDLOOP.

*"Alv Style
  PERFORM MAKE_ALV_STYLE.

*Refresh.
  PERFORM REFRESH_TABLE_DISPLAY USING G_GRID.

*Refresh
  CALL METHOD CL_GUI_CFW=>FLUSH.
ENDFORM.                    " data_changed

消息提示框

*&---------------------------------------------------------------------*
*&      Form  WS_MSG
*&---------------------------------------------------------------------*
FORM WS_MSG  USING  P_TYPE  P_TEXT  P_TITLE.
  CALL FUNCTION 'WS_MSG'
    EXPORTING
      MSG_TYPE = P_TYPE
      TEXT     = P_TEXT
      TITL     = P_TITLE.
ENDFORM.                    " WS_MSG

 

 

*&---------------------------------------------------------------------* *& Report YTEST31 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT YTEST31. DATA: OK_CODE TYPE SY-UCOMM, SAVE_OK TYPE SY-UCOMM. TABLES SPFLI. DATA LS_SPFLI TYPE SPFLI. DATA WA_SPFLI TYPE TABLE OF SPFLI. DATA WADEL_SPFLI TYPE TABLE OF SPFLI. SELECT * INTO TABLE WA_SPFLI FROM SPFLI. DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA', ALV_GRID TYPE REF TO CL_GUI_ALV_GRID, WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER. DATA WA_LAYOUT TYPE LVC_S_LAYO. WA_LAYOUT-EDIT = 'X'. CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED. DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER. ************************************************************************ * CLASS LCL_EVENT_RECEIVER DEFINITION * * * ************************************************************************ CLASS LCL_EVENT_RECEIVER DEFINITION. PUBLIC SECTION. TYPES: DEL_ROWS TYPE STANDARD TABLE OF SPFLI. DATA: DDEL_ROWS TYPE STANDARD TABLE OF SPFLI. METHODS: HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID IMPORTING ER_DATA_CHANGED. METHODS: UPDATE_DELTA_TABLES IMPORTING PR_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL. METHODS: GET_DELETED_ROWS EXPORTING DELETED_ROWS TYPE DEL_ROWS. ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION *&-------------------------------------------------------------------------- *& CLASS LCL_EVENT_RECEIVER IMPLENTMENTATION *& *&-------------------------------------------------------------------------- CLASS LCL_EVENT_RECEIVER IMPLEMENTATION. METHOD HANDLE_DATA_CHANGED. CALL METHOD UPDATE_DELTA_TABLES( ER_DATA_CHANGED ). ENDMETHOD. "HANDLE_DATA_CHANGED METHOD UPDATE_DELTA_TABLES. "HANDLE_DATA_CHANGED DATA: L_DEL_ROW TYPE LVC_S_MOCE. LOOP AT PR_DATA_CHANGED->MT_DELETED_ROWS INTO L_DEL_ROW. READ TABLE WA_SPFLI INTO LS_SPFLI INDEX L_DEL_ROW-ROW_ID. IF SY-SUBRC NE 0. MESSAGE E208(00) WITH '?理??'. ELSE. APPEND LS_SPFLI TO DDEL_ROWS. ENDIF. ENDLOOP. ENDMETHOD. "UPDATE_DELTA_TABLES METHOD GET_DELETED_ROWS. "update_delta_tables DELETED_ROWS = ME->DDEL_ROWS. ENDMETHOD. "GET_DELETED_ROWS ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION START-OF-SELECTION. CALL SCREEN 100. WRITE / '?除的内表??'. "get_deleted_rows WRITE / '______________'. "LCL_EVENT_RECEIVER DEFINITION CALL METHOD EVENT_RECEIVER->GET_DELETED_ROWS IMPORTING DELETED_ROWS = WADEL_SPFLI. LOOP AT WADEL_SPFLI INTO SPFLI. WRITE: / SPFLI-CARRID,SPFLI-CONNID,SPFLI-CITYFROM. ENDLOOP. WRITE: / '更新后的内表??:'. WRITE: / '__________________'. LOOP AT WA_SPFLI INTO SPFLI. WRITE : / SPFLI-CARRID,SPFLI-CONNID,SPFLI-CITYFROM. ENDLOOP. *此?是更新到数据?的操作 *WHEN 'SAVE'. * MODIFY SPFLI FORM TABLE WA_SPFLI. * IF SY-SUBRC NE 0. *MESSAGE I005(YMESS) WITH '更新数据??!'. EXIT. * ELSE. * MESSAGE I005(YMESS) WITH '更新数据OK!'. * ENDIF. * DELETE SPFLI FROM TABLE DELA_SPFLI. * IF SY-SUBRC NE 0. * MESSAGE I005(YMESS) WITH '更新数据??!'. * ELSE. * MESSAGE I005(YMESS) WITH '更新数据OK!'. * ENDIF. END-OF-SELECTION. INCLUDE YTEST31_STATUS_100. INCLUDE YTEST31_USER_COMMAND_100.
ABAP中,可以通过使用ALV列表视图(ALV Grid)来为特定的单元格添加颜色提示。下面是一个简单的示例代码,演示如何在ALV Grid中添加颜色提示: 1. 首先,你需要在程序的顶部包含以下两个库: ```abap TYPE-POOLS: slis. INCLUDE: slis. ``` 2. 然后,在程序中定义一个内部表来存储显示的数据: ```abap DATA: lt_data TYPE TABLE OF spfli. ``` 3. 接下来,使用函数模块REUSE_ALV_GRID_DISPLAY来显示ALV Grid: ```abap CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid TABLES t_outtab = lt_data. ``` 4. 然后,使用函数模块REUSE_ALV_FIELDCATALOG_MERGE来定义字段目录: ```abap CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING i_program_name = sy-repid i_internal_tabname = 'SPFLI' CHANGING ct_fieldcat = gt_fieldcat[] EXCEPTIONS inconsistent_interface = 1 program_error = 2 OTHERS = 3. ``` 5. 接下来,在字段目录中定义一个颜色字段,并设置其属性: ```abap LOOP AT gt_fieldcat ASSIGNING FIELD-SYMBOL(<fs_fieldcat>). IF <fs_fieldcat>-fieldname = 'CARRID'. <fs_fieldcat>-color-col = 'C'. <fs_fieldcat>-emphasize = 'X'. ENDIF. ENDLOOP. ``` 在上述代码中,我们为字段'CARRID'定义了一个颜色字段,并设置其颜色属性为'C',以及强调属性为'X'。 6. 最后,通过调用函数模块REUSE_ALV_GRID_DISPLAY,显示带有颜色提示ALV Grid: ```abap CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid TABLES t_outtab = lt_data fieldcat = gt_fieldcat. ``` 以上就是一个简单的示例,演示了如何在ABAP中使用ALV Grid来添加颜色提示。你可以根据自己的需求进行进一步的定制和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAP剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值