ALV示例---Dyoprn實現,一個容器中嵌入ALV報表

mmccking發表於2009-10-27
容器中嵌入ALV報表,並且可以實現修改編輯。[@more@]

注意點:1,螢幕上需要定義一個CoustomerControl

2,如果在CoustomerControl中設定一個按鈕,要響應這個按鈕,需要在ElementList中指定它的FunctionCode。

程式:

*&---------------------------------------------------------------------*
*& Report ZTEST_DYNPRO
*&
*&---------------------------------------------------------------------*
*& Test by Ray.Zhang
*&---------------------------------------------------------------------*

REPORT ZTEST_DYNPRO.

TABLES VBAK.

TYPES:
BEGIN OF TYP_VBAK,
VBELN TYPE VBAK-VBELN, "Sales Document
ERDAT TYPE VBAK-ERDAT, "Date on which the record was created
ERNAM TYPE VBAK-ERNAM, "Name of Person who Created the Object
AUART TYPE VBAK-AUART, "Sales Document Type
WAERK TYPE VBAK-WAERK, "SD document currency
END OF TYP_VBAK.

DATA:
TG_FIELDCAT TYPE LVC_T_FCAT,
TG_VBAK TYPE TABLE OF TYP_VBAK.

DATA:
OK_CODE TYPE SY-UCOMM,
VG_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
VG_GRID TYPE REF TO CL_GUI_ALV_GRID,
WG_LAYOUT TYPE LVC_S_LAYO.

SELECT-OPTIONS:
S_ERDAT FOR VBAK-ERDAT.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.

PERFORM F_GET_DATA.

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.

PERFORM F_OUTPUT_RESULT.

*----------------------------------------------------------------------*
* FORMS
*----------------------------------------------------------------------
*&---------------------------------------------------------------------*
*& Form F_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM F_GET_DATA .

SELECT VBELN
ERDAT
ERNAM
AUART "Sales Document Type
WAERK "SD document currency
FROM VBAK
INTO TABLE TG_VBAK
WHERE ERDAT IN S_ERDAT.

ENDFORM. " F_GET_DATA
*
*&---------------------------------------------------------------------*
*& Form F_OUTPUT_RESULT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM F_OUTPUT_RESULT .

PERFORM F_SET_ALV.

CALL SCREEN 9001.

ENDFORM. " F_OUTPUT_RESULT
*&---------------------------------------------------------------------*
*& Form F_SET_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM F_SET_ALV .

PERFORM F_FIELDCAT_EDIT USING '1' 'VBELN' 'Sales Document'.
PERFORM F_FIELDCAT_EDIT USING '2' 'ERDAT' 'Create Date'.
PERFORM F_FIELDCAT_EDIT USING '3' 'ERDAT' 'Create By'.
PERFORM F_FIELDCAT_EDIT USING '4' 'AUART' 'Sales Document Type'.
PERFORM F_FIELDCAT_EDIT USING '5' 'WAERK' 'Document currency'.

ENDFORM. " F_SET_ALV

*&---------------------------------------------------------------------*
*& Form F_FIELDCAT_EDIT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
FORM F_FIELDCAT_EDIT USING U_COL_POS TYPE LVC_S_FCAT-COL_POS
U_FIELDNAME TYPE LVC_S_FCAT-FIELDNAME
U_SELTEXT_L TYPE LVC_S_FCAT-SCRTEXT_L.

DATA:
WL_FIELDCAT TYPE LVC_S_FCAT.

WL_FIELDCAT-COL_POS = U_COL_POS.
WL_FIELDCAT-FIELDNAME = U_FIELDNAME.
WL_FIELDCAT-SCRTEXT_L = U_SELTEXT_L.

APPEND WL_FIELDCAT TO TG_FIELDCAT.

ENDFORM. " F_FIELDCAT_EDIT
*&---------------------------------------------------------------------*
*& Module M_STATUS_9001 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE M_STATUS_9001 OUTPUT.

SET PF-STATUS 'ST_9001'.

IF VG_CONTAINER IS INITIAL.

CREATE OBJECT VG_CONTAINER
EXPORTING
CONTAINER_NAME = 'CONTAINER'.

CREATE OBJECT VG_GRID
EXPORTING
I_PARENT = VG_CONTAINER.

* LAYOUT SET
WG_LAYOUT-NO_TOOLBAR = 'X'. "NO TOOLBAR
WG_LAYOUT-CWIDTH_OPT = 'X'.
WG_LAYOUT-SEL_MODE = 'A'.
WG_LAYOUT-EDIT = 'X'. "Set status of all cells to editable

* ALV OUTPUT
CALL METHOD VG_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = WG_LAYOUT
CHANGING
IT_FIELDCATALOG = TG_FIELDCAT
IT_OUTTAB = TG_VBAK.

CALL METHOD VG_GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 0.

ENDIF.

ENDMODULE. " M_STATUS_9001 OUTPUT
*&---------------------------------------------------------------------*
*& Module M_USER_COMMAND_9001 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE M_USER_COMMAND_9001 INPUT.

DATA:
LV_VALID(1) TYPE C,
LV_OK_CODE TYPE SY-UCOMM.

LV_OK_CODE = OK_CODE.

CASE LV_OK_CODE.
WHEN 'SAVE'.
CALL METHOD VG_GRID->CHECK_CHANGED_DATA
IMPORTING
E_VALID = LV_VALID.
IF LV_VALID = 'X'.
MESSAGE I000(0K) WITH 'Data can be saved now'.
ELSE.
MESSAGE I000(0K) WITH 'Errors still exist'.
ENDIF.
WHEN 'CHANGE'.

PERFORM F_SWITCH_EDIT_MODE.

WHEN 'BACK'.
SET SCREEN 0.
LEAVE SCREEN.
ENDCASE.
CLEAR OK_CODE.
ENDMODULE. " M_USER_COMMAND_9001 INPUT
*&---------------------------------------------------------------------*
*& Form SWITCH_EDIT_MODE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM F_SWITCH_EDIT_MODE.
*§3.Use IS_READY_FOR_INPUT to fetch current substate of editable cells.
IF VG_GRID->IS_READY_FOR_INPUT( ) EQ 0.
*§4.Use SET_READY_FOR_INPUT to switch between the substates.
CALL METHOD VG_GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.
ELSE.
CALL METHOD VG_GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 0.
ENDIF.
ENDFORM. " SWITCH_EDIT_MODE

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-1028187/,如需轉載,請註明出處,否則將追究法律責任。

相關文章