Abap/4控制ALV表格(4)

*&---------------------------------------------------------------------*
*& Report  Z_CPT9_4                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&    ALV控件对数据库进行CRUD                                          *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  Z_CPT9_4                                                    .

DATA: OK_CODE TYPE SY-UCOMM,
      SAVE_OK TYPE SY-UCOMM.
DATA: WA_SPFLI TYPE TABLE OF SPFLI,
      A_SPFLI TYPE TABLE OF SPFLI WITH HEADER LINE.
TABLES SPFLI.
DATA LS_SPFLI TYPE SPFLI.
DATA WADEL_SPFLI TYPE TABLE OF SPFLI.

*定义ALV控件类型
DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'CONTAINER1',
     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.
    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,
            UPDATE_DELTA_TABLES
                IMPORTING
                    PR_DATA_CHANGED TYPE
                       REF TO CL_ALV_CHANGED_DATA_PROTOCOL,
            GET_DELETED_ROWS
                EXPORTING
                    DELETED_ROWS TYPE DEL_ROWS.
ENDCLASS.

CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
    METHOD HANDLE_DATA_CHANGED.
        CALL METHOD UPDATE_DELTA_TABLES( ER_DATA_CHANGED ).
    ENDMETHOD.
*   有数据更改时,就把更改的数据统一放入到内表中。
    METHOD UPDATE_DELTA_TABLES.
        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.
    METHOD GET_DELETED_ROWS.
        DELETED_ROWS = ME->DDEL_ROWS.
    ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
"执行代码
SELECT * FROM SPFLI INTO TABLE WA_SPFLI.
CALL SCREEN 100.
WRITE / '删除内表的记录'.
ULINE.
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 / '更新后的内表记录'.
ULINE.
LOOP AT WA_SPFLI INTO SPFLI.
    WRITE: / SPFLI-CARRID,SPFLI-CONNID,SPFLI-CITYFROM.

ENDLOOP.

*用内表更新数据库
MODIFY SPFLI FROM TABLE WA_SPFLI.

 

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.
IF WA_CUSTOM_CONTAINER IS INITIAL.
    CREATE OBJECT WA_CUSTOM_CONTAINER
        EXPORTING
            CONTAINER_NAME = WA_CONTAINER.
    CREATE OBJECT ALV_GRID
        EXPORTING
            I_PARENT = WA_CUSTOM_CONTAINER.
    CREATE OBJECT EVENT_RECEIVER.
    SET HANDLER EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR ALV_GRID.

    CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
        EXPORTING
            I_STRUCTURE_NAME = 'SPFLI'
            IS_LAYOUT = WA_LAYOUT
        CHANGING
            IT_OUTTAB = WA_SPFLI.
ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
    SAVE_OK = OK_CODE.
    CLEAR OK_CODE.
    CASE SAVE_OK.
        WHEN 'EXIT'.
            DATA L_RET VALUE 'X'.
            CALL METHOD ALV_GRID->CHECK_CHANGED_DATA
                IMPORTING
                    E_VALID = L_RET.
            LEAVE TO SCREEN 0.
        WHEN 'SAVE'.
            MODIFY SPFLI FROM TABLE WA_SPFLI.
    ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  CANCEL  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE CANCEL INPUT.
    LEAVE PROGRAM.
ENDMODULE.                 " CANCEL  INPUT

你可能感兴趣的:(object,command,Module,layout,table,input)