ABAP顾问
分类:
2009-03-11 19:18:26
*&---------------------------------------------------------------------*
*& Report Z_BARRY_ALV_DATACHANGED
*&
*&---------------------------------------------------------------------*
*& 更多参考程序:BCALV_EDIT_08
*&
*&---------------------------------------------------------------------*
REPORT Z_BARRY_ALV_DATACHANGED.
DATA: BEGIN OF ITAB OCCURS 0 ,
MBLNR LIKE MSEG-MBLNR ,
KOSTL LIKE MSEG-KOSTL ,
END OF ITAB .
DATA: OK_CODE LIKE SY-UCOMM .
DATA: L_VALID(1) TYPE C.
DATA: GO_GRID TYPE REF TO CL_GUI_ALV_GRID,
GO_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
GS_FIELDCAT TYPE LVC_S_FCAT,
GS_VARIANT TYPE DISVARIANT ,
GT_FILT TYPE LVC_T_FILT,
GS_FILT TYPE LVC_S_FILT,
SLA TYPE LVC_S_LAYO .
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.
METHODS HANDLE_DATA_CHANGED
FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
ENDCLASS. "lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_DATA_CHANGED.
PERFORM HANDLE_DATA_CHANGED USING ER_DATA_CHANGED.
ENDMETHOD. "handle_data_changed
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
INITIALIZATION.
* gd_repid = sy-repid.
PERFORM FIELDCAT_INIT USING GT_FIELDCAT[].
START-OF-SELECTION.
CALL SCREEN 0100 .
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE OK_CODE.
WHEN 'BACK'.
LEAVE PROGRAM.
WHEN 'ENTE'.
PERFORM ENTER.
WHEN 'CHANGE'.
PERFORM CHANGE.
ENDCASE.
CLEAR OK_CODE .
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form load_data_into_grid
*&---------------------------------------------------------------------*
FORM LOAD_DATA_INTO_GRID.
SELECT * FROM MSEG INTO CORRESPONDING FIELDS OF TABLE ITAB
UP TO 20 ROWS .
SLA-CWIDTH_OPT = 'X'.
SLA-ZEBRA = 'X'.
CALL METHOD GO_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_VARIANT = GS_VARIANT
I_SAVE = 'A'
IS_LAYOUT = SLA
CHANGING
IT_OUTTAB = ITAB[]
IT_FIELDCATALOG = GT_FIELDCAT[]
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
CALL METHOD GO_GRID->SET_READY_FOR_INPUT "处于编辑状态
EXPORTING
I_READY_FOR_INPUT = 1.
CALL METHOD CL_GUI_CONTROL=>SET_FOCUS "设置焦点在go_grid 上
EXPORTING
CONTROL = GO_GRID .
ENDFORM. " load_data_into_grid
*&--------------------------------------------------------------------*
*& Form fieldcat_init
*&--------------------------------------------------------------------*
FORM FIELDCAT_INIT USING RT_FIELDCAT TYPE LVC_T_FCAT.
DATA: LS_FIELDCAT TYPE LVC_S_FCAT.
PERFORM FRM_CATLG_SET USING:
'MBLNR' 'X' '凭证' '' 'MBLNR' 'MSEG' RT_FIELDCAT,
'KOSTL' '' '成本中心' 'X' '' '' RT_FIELDCAT.
ENDFORM. "fieldcat_init
*---------------------------------------------------------------------*
* FORM frm_catlg_set *
*---------------------------------------------------------------------*
FORM FRM_CATLG_SET USING P_FIELD P_KEY P_TEXT P_EDIT REF_F REF_T
RT_FIELDCAT TYPE LVC_T_FCAT .
DATA: TMP_FIELDCAT TYPE LVC_S_FCAT.
TMP_FIELDCAT-FIELDNAME = P_FIELD.
TMP_FIELDCAT-KEY = P_KEY .
TMP_FIELDCAT-SCRTEXT_L = P_TEXT.
TMP_FIELDCAT-EDIT = P_EDIT.
TMP_FIELDCAT-REF_FIELD = REF_F.
TMP_FIELDCAT-REF_TABLE = REF_T.
APPEND TMP_FIELDCAT TO RT_FIELDCAT .
CLEAR TMP_FIELDCAT .
ENDFORM. " FRM_CATLG_SET
*&---------------------------------------------------------------------*
*& Form change
*&---------------------------------------------------------------------*
FORM CHANGE .
DATA: LS_STABLE TYPE LVC_S_STBL.
LS_STABLE-ROW = 'X'.
LS_STABLE-COL = 'X'.
IF GO_GRID->IS_READY_FOR_INPUT( ) = 0.
CALL METHOD GO_GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.
ELSE.
CALL METHOD GO_GRID->CHECK_CHANGED_DATA "把修改的内容更改到内表里
IMPORTING
E_VALID = L_VALID.
CALL METHOD GO_GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 0.
* CALL METHOD go_grid->refresh_table_display.
CALL METHOD GO_GRID->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = LS_STABLE
EXCEPTIONS
FINISHED = 1
OTHERS = 2.
ENDIF.
ENDFORM. " change
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'MAIN100'.
GS_VARIANT-REPORT = SY-REPID.
IF GO_GRID IS INITIAL.
CREATE OBJECT GO_GRID
EXPORTING
I_PARENT = CL_GUI_CONTAINER=>SCREEN0.
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR GO_GRID.
IF SY-BATCH IS INITIAL.
CALL METHOD GO_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER. "回车时触发
* i_event_id = cl_gui_alv_grid=>mc_evt_modified. "单元格更改触发
ENDIF.
PERFORM LOAD_DATA_INTO_GRID.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form ENTER
*&---------------------------------------------------------------------*
FORM ENTER .
DATA: UCOMM LIKE SY-UCOMM.
UCOMM = '&DATA_SAVE'." '&REFRESH'.
CALL METHOD GO_GRID->SET_FUNCTION_CODE
CHANGING
C_UCOMM = UCOMM.
ENDFORM. " ENTER
*&---------------------------------------------------------------------*
*& Form HANDLE_DATA_CHANGED
*&---------------------------------------------------------------------*
FORM HANDLE_DATA_CHANGED USING DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL..
FIELD-SYMBOLS:
IF NOT DATA_CHANGED->MP_MOD_ROWS IS INITIAL.
ASSIGN DATA_CHANGED->MP_MOD_ROWS->* TO
ASSIGN DATA_CHANGED->MT_MOD_CELLS TO
LOOP AT
IF
READ TABLE
CALL METHOD DATA_CHANGED->ADD_PROTOCOL_ENTRY
EXPORTING
I_MSGID = 'OO'
I_MSGTY = 'E'
I_MSGNO = '000'
I_MSGV1 = '不可以为ABC'
I_FIELDNAME = 'KOSTL'
I_ROW_ID =
* i_tabix = tabix
.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM. " HANDLE_DATA_CHANGED
*CL_ALV_CHANGED_DATA_PROTOCOL参数的常用方法和属性
*
*方法 用途
*MODIFY_CELL 修改指定位置单元格的值
*GET_CELL_VALUE 获取单元格的值
*ADD_PROTOCOL_ENTRY 弹出一个消息,必须使用系统标准的消息类
*MODIFY_PROTOCOL_ENTRY 修改消息
*PROTOCOL_IS_VISIBLE 是否显示错误信息提示
*REFRESH_PROTOCOL 删除错误消息
*
*属性 用途
*MT_PROTOCOL LOGs
*MT_MOD_CELLS 更新的单元格信息
*MP_MOD_ROWS 更新了的行的信息
*MT_GOOD_CELLS 正确的CELL的值
*MT_DELETED_ROWS 被删除的行信息
*MT_INSERTED_ROWS 新插入的行信息