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