ALV DataChange EVENT
在CX项目中,根据需求,自定义一个表,维护供应商的银行账号信息,当输入供应商编号时,自动在供应商名称列里自动填写供应商名称,用到了ALV DataChange 事件 ,下面是源代码:
*&---------------------------------------------------------------------*
*& REPORT ZMM_LIFNR_BANK
*&
*&---------------------------------------------------------------------*
*& PROGRAM NAME : ZMM_LIFNR_BANK
*& DESCRIPTION :
*& AUTHOR :
*& DATE WRITTEN :
*& TCODE :
*& NOTE :
*&---------------------------------------------------------------------*
REPORT ZMM_LIFNR_BANK.
*INCLUDE ZMMLIFNRBANKTOP.
TABLES:ZXK001.
TYPE-POOLS:SLIS.
TYPES BEGIN OF TY_ITAB.
TYPES CHK.
INCLUDE TYPE ZXK001.
TYPES END OF TY_ITAB.
DATA: IT_ZXK001 TYPE TABLE OF TY_ITAB WITH HEADER LINE.
DATA: WA_IT_ZXK001 TYPE TY_ITAB.
DATA: ITZXK001 LIKE TABLE OF ZXK001 WITH HEADER LINE.
DATA: GT_FIELDCAT TYPE LVC_T_FCAT WITH HEADER LINE.
DATA: GC_GLAY TYPE LVC_S_GLAY.
DATA: GS_LAYOUT TYPE LVC_S_LAYO, "SLIS_LAYOUT_ALV,
WK_REPID LIKE SY-REPID.
DATA GS_GRID TYPE LVC_S_GLAY.
DATA TEM_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA GT_EVENTS TYPE SLIS_T_EVENT.
DATA GS_EVENTS LIKE LINE OF GT_EVENTS.
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION. "定义类 捕捉各种事件
PUBLIC SECTION.
METHODS HANDLE_MODIFY "回车
FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID
IMPORTING E_MODIFIED ET_GOOD_CELLS.
METHODS HANDLE_ONF4 "F4
FOR EVENT ONF4 OF CL_GUI_ALV_GRID
IMPORTING E_FIELDNAME ES_ROW_NO ER_EVENT_DATA.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
DATA GT_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER .
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-B01.
SELECT-OPTIONS:
S_LIFNR FOR ZXK001-LIFNR.
SELECTION-SCREEN END OF BLOCK B1.
START-OF-SELECTION.
PERFORM GET_DATA.
PERFORM DISPLAY_DATA.
*&---------------------------------------------------------------------*
*& FORM GET_DATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
FORM GET_DATA.
SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_ZXK001 FROM ZXK001 WHERE LIFNR IN S_LIFNR.
ENDFORM. "GET_DATA
*&---------------------------------------------------------------------*
*& FORM DISPLAY_DATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM DISPLAY_DATA .
DEFINE FILL_ALV.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = &1.
GT_FIELDCAT-SCRTEXT_M = &2 .
* GT_FIELDCAT-OUTPUTLEN = &3.
GT_FIELDCAT-NO_ZERO = &3.
GT_FIELDCAT-DECIMALS_O = &4.
APPEND GT_FIELDCAT.
END-OF-DEFINITION.
FILL_ALV 'LIFNR' '供应商编号 ' '' ''.
FILL_ALV 'NAME1' '供应商名称 ' '' ''.
FILL_ALV 'BANKN' '开户银行 ' ' ' ''.
FILL_ALV 'IBAN' '银行账号 ' '' ''.
LOOP AT GT_FIELDCAT WHERE FIELDNAME = 'LIFNR'
OR FIELDNAME = 'BANKN'
OR FIELDNAME = 'NAME1'
OR FIELDNAME = 'IBAN'.
IF GT_FIELDCAT-FIELDNAME <> 'NAME1'.
GT_FIELDCAT-EDIT = 'X'.
ENDIF.
IF GT_FIELDCAT-FIELDNAME = 'NAME1'.
GT_FIELDCAT-OUTPUTLEN = '30'.
ENDIF.
IF GT_FIELDCAT-FIELDNAME = 'LIFNR'.
* GT_FIELDCAT-REF_FIELD = 'LIFNR'.
* GT_FIELDCAT-REF_TABLE = 'LFA1'.
GT_FIELDCAT-OUTPUTLEN = '20'.
ENDIF.
IF GT_FIELDCAT-FIELDNAME = 'BANKN'.
GT_FIELDCAT-REF_FIELD = 'BANKN'.
GT_FIELDCAT-REF_TABLE = 'TIBAN'.
GT_FIELDCAT-OUTPUTLEN = '35'.
ENDIF.
IF GT_FIELDCAT-FIELDNAME = 'IBAN'.
GT_FIELDCAT-REF_FIELD = 'IBAN'.
GT_FIELDCAT-REF_TABLE = 'TIBAN'.
GT_FIELDCAT-OUTPUTLEN = '34'.
ENDIF.
MODIFY GT_FIELDCAT.
ENDLOOP.
GS_LAYOUT-STYLEFNAME = 'FSTYLE'.
GS_LAYOUT-ZEBRA = 'X'.
* GS_LAYOUT-CWIDTH_OPT = 'X'.
GS_LAYOUT-BOX_FNAME = 'CHK'.
GS_GRID-EDT_CLL_CB = 'X'.
* **************定义事件FORM名称*********************
GS_EVENTS-NAME = 'CALLER_EXIT'.
GS_EVENTS-FORM = 'CALLER_EXIT'.
APPEND GS_EVENTS TO GT_EVENTS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
* I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_PAGE'
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
IT_FIELDCAT_LVC = GT_FIELDCAT[]
I_GRID_SETTINGS = GS_GRID
IS_LAYOUT_LVC = GS_LAYOUT
I_SAVE = 'X'
IT_EVENTS = GT_EVENTS
TABLES
T_OUTTAB = IT_ZXK001.
ENDFORM. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& FORM SET_PF_STATUS
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->EXTAB TEXT
*----------------------------------------------------------------------*
FORM SET_PF_STATUS USING EXTAB TYPE SLIS_T_EXTAB. "#EC CALLED
SET PF-STATUS '1000' .
ENDFORM. "WHOLE_TOOLBAR
*&---------------------------------------------------------------------*
*& FORM USER_COMMAND
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->RF_UCOMM TEXT
* -->RS TEXT
*----------------------------------------------------------------------*
FORM USER_COMMAND USING RF_UCOMM LIKE SY-UCOMM
RS TYPE SLIS_SELFIELD.
DATA:P_COUNT TYPE I.
DATA: P_ANSWER(1).
DATA: LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA LV_ANSWER.
DATA LS_STYLEROW TYPE LVC_S_STYL .
DATA LT_STYLETAB TYPE LVC_T_STYL .
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = LR_GRID.
CALL METHOD LR_GRID->CHECK_CHANGED_DATA.
RS-REFRESH = 'X'. "自动刷新
CASE RF_UCOMM.
WHEN '&NEW'.
IT_ZXK001-LIFNR = ''.
IT_ZXK001-NAME1 = ''.
IT_ZXK001-BANKN = ''.
IT_ZXK001-IBAN = ''.
APPEND IT_ZXK001.
CLEAR IT_ZXK001.
WHEN '&DEL'.
CLEAR P_COUNT.
LOOP AT IT_ZXK001 WHERE CHK = 'X'.
P_COUNT = 1.
EXIT.
ENDLOOP.
IF P_COUNT <= 0.
MESSAGE '请选择要删除的数据!' TYPE 'I'.
RETURN.
ELSE.
CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
TEXTLINE1 = '确认要删除所选记录吗?'
TITEL = '确认提示'
CANCEL_DISPLAY = ''
IMPORTING
ANSWER = P_ANSWER.
CHECK P_ANSWER = 'J'.
IF P_ANSWER = 'J'.
LOOP AT IT_ZXK001 WHERE CHK = 'X'.
DELETE FROM ZXK001 WHERE LIFNR = IT_ZXK001-LIFNR.
DELETE IT_ZXK001.
ENDLOOP.
ENDIF.
ENDIF.
WHEN '&SAVE'.
CLEAR P_COUNT.
LOOP AT IT_ZXK001 WHERE LIFNR = ''.
P_COUNT = 1.
EXIT.
ENDLOOP.
IF P_COUNT > 0.
MESSAGE '供应商字段不能为空!' TYPE 'I'.
RETURN.
ELSE.
REFRESH ITZXK001.
CLEAR ITZXK001.
LOOP AT IT_ZXK001.
MOVE-CORRESPONDING IT_ZXK001 TO ITZXK001.
APPEND ITZXK001.
CLEAR ITZXK001.
ENDLOOP.
MODIFY ZXK001 FROM TABLE ITZXK001.
IF SY-SUBRC EQ 0.
COMMIT WORK.
MESSAGE S000(ZMM001).
ELSE.
ROLLBACK WORK.
MESSAGE S001(ZMM001).
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDFORM. "USER_COMMAND
*&---------------------------------------------------------------------*
*& FORM CALLER_EXIT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->E_GRID TEXT
*----------------------------------------------------------------------*
FORM CALLER_EXIT USING E_GRID TYPE SLIS_DATA_CALLER_EXIT.
DATA: L_FIELD TYPE LVC_FNAME,
LT_F4 TYPE LVC_T_F4,
LS_F4 TYPE LVC_S_F4.
* LS_F4-FIELDNAME = 'UMSKZ'.
* LS_F4-REGISTER = 'X'.
"LS_F4-GETBEFORE = 'X'. "打'X'会在弹出窗体之前先刷新ALV数据
"LS_F4-CHNGEAFTER = 'X'. "打'X'会在关闭窗体之后刷新ALV数据
"LS_F4-INTERNAL = ''.
INSERT LS_F4 INTO TABLE LT_F4.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = TEM_GRID.
CALL METHOD TEM_GRID->REGISTER_EDIT_EVENT "注册GRID事件
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER"事件:回车
EXCEPTIONS
ERROR = 1
OTHERS = 2.
* CALL METHOD TEM_GRID->REGISTER_F4_FOR_FIELDS
* EXPORTING
* IT_F4 = LT_F4.
CREATE OBJECT GT_EVENT_RECEIVER.
SET HANDLER GT_EVENT_RECEIVER->HANDLE_MODIFY FOR TEM_GRID.
SET HANDLER GT_EVENT_RECEIVER->HANDLE_ONF4 FOR TEM_GRID.
ENDFORM. "CALLER_EXIT
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION. "实现类 处理回车事件
METHOD HANDLE_MODIFY.
DATA LS_STYLEROW TYPE LVC_S_STYL .
DATA LT_STYLETAB TYPE LVC_T_STYL .
DATA STBL TYPE LVC_S_STBL.
LOOP AT IT_ZXK001 INTO WA_IT_ZXK001.
SELECT SINGLE
NAME1
INTO WA_IT_ZXK001-NAME1
FROM LFA1
WHERE LIFNR = WA_IT_ZXK001-LIFNR.
MODIFY IT_ZXK001 FROM WA_IT_ZXK001.
ENDLOOP.
STBL-ROW = 'X'." 基于行的稳定刷新
STBL-COL = 'X'." 基于列稳定刷新
CALL METHOD TEM_GRID->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = STBL.
ENDMETHOD. "HANDLE_MODIFY
METHOD HANDLE_ONF4.
DATA :PT_RET TYPE TABLE OF DDSHRETVAL ,
PS_RET TYPE DDSHRETVAL ,
LS_MODI TYPE LVC_S_MODI.
FIELD-SYMBOLS <MODTAB> TYPE LVC_T_MODI.
* SELECT SHBKZ AS UMSKZ
* LTEXT
* INTO TABLE ITHELP
* FROM T074T
* WHERE KOART = 'D'
* AND SPRAS = '1'.
*
* CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
* EXPORTING
* RETFIELD = 'UMSKZ' "内表的关键字段名
* VALUE_ORG = 'S' "'S'
* DYNPPROG = SY-REPID "文本框PROGRAM
* DYNPNR = SY-DYNNR "文本框所在屏幕
* DYNPROFIELD = '特殊总账标识' "文本框名
* WINDOW_TITLE = '标题' "窗体标题
* TABLES
* VALUE_TAB = ITHELP "内表
* "FIELD_TAB = T_FIELD
* RETURN_TAB = PT_RET "返回选择的数据
* EXCEPTIONS
* PARAMETER_ERROR = 1
* NO_VALUES_FOUND = 2
* OTHERS = 3.
** 处理 PT_RET 返回的数据,更新ALV CELL即可
READ TABLE PT_RET INTO PS_RET INDEX 1 .
IF SY-SUBRC = 0.
LS_MODI-ROW_ID = ES_ROW_NO-ROW_ID.
LS_MODI-FIELDNAME = E_FIELDNAME.
LS_MODI-VALUE = PS_RET-FIELDVAL.
ASSIGN ER_EVENT_DATA->M_DATA->* TO <MODTAB>.
APPEND LS_MODI TO <MODTAB>.
ENDIF.
ER_EVENT_DATA->M_EVENT_HANDLED = 'X'. "通知系统搜索事件处理完毕,停止调用系统标准的SEARCH HELP。
ENDMETHOD. "HANDLE_ONF4
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION