1、基础案例
*&---------------------------------------------------------------------*
*& Report ZWX_TEST12
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZWX_TEST12.
TYPES:BEGIN OF GTY_DATA,
COL1 TYPE CHAR10,
COL2 TYPE CHAR10,
COL3 TYPE CHAR10,
COL4 TYPE CHAR10,
COL5 TYPE CHAR10,
COL6 TYPE CHAR10,
COL7 TYPE CHAR10,
COL8 TYPE CHAR10,
COL9 TYPE CHAR10,
COL10 TYPE CHAR10,
COL11 TYPE CHAR10,
COL12 TYPE CHAR10,
END OF GTY_DATA.
DATA: GT_DATA TYPE TABLE OF GTY_DATA,
GS_DATA TYPE GTY_DATA.
FIELD-SYMBOLS: <FS_RESULT> TYPE STANDARD TABLE,
<FS_TABLE> TYPE STANDARD TABLE,
<FS_LINE> TYPE ANY,
<FS_DATA> TYPE GTY_DATA.
DATA: LR_TABLE TYPE REF TO DATA,
LR_LINE TYPE REF TO DATA.
*ALV字段列表。
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
GS_FIELDCAT TYPE LVC_S_FCAT,
GS_LAYOUT TYPE LVC_S_LAYO,
GT_EVENTS TYPE SLIS_T_EVENT,
GS_EVENTS TYPE SLIS_ALV_EVENT,
GS_VARIANT TYPE DISVARIANT.
PARAMETERS: P_ROW TYPE I DEFAULT 3,
P_COL TYPE I DEFAULT 3.
START-OF-SELECTION.
PERFORM FRM_INIT_DATA.
PERFORM FRM_FETCH_DATA.
END-OF-SELECTION.
PERFORM FRM_DISPLAY_ALV.
*&---------------------------------------------------------------------*
*& Form FRM_INIT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_INIT_DATA .
" 初始化原始数据
DO P_ROW TIMES.
DATA(LV_INDEX) = SY-INDEX.
APPEND INITIAL LINE TO GT_DATA ASSIGNING <FS_DATA>.
IF <FS_DATA> IS ASSIGNED.
DO 12 TIMES.
DATA(LV_COMP) = |COL{ SY-INDEX }|.
ASSIGN COMPONENT LV_COMP OF STRUCTURE <FS_DATA> TO FIELD-SYMBOL(<FS_COMP>).
IF SY-SUBRC = 0.
<FS_COMP> = |{ LV_COMP }--{ LV_INDEX }|.
ENDIF.
ENDDO.
ENDIF.
ENDDO.
" 初始化列
DO P_COL TIMES.
LV_COMP = |COL{ SY-INDEX }|.
DATA(LV_TEXT) = |列{ SY-INDEX }|.
PERFORM FRM_FILL_FIELDCAT USING SY-INDEX LV_COMP LV_TEXT.
ENDDO.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILL_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_FILL_FIELDCAT USING P_COL P_FIELDNAME P_TEXT.
APPEND INITIAL LINE TO GT_FIELDCAT ASSIGNING FIELD-SYMBOL(<FS_FIELD>).
IF <FS_FIELD> IS ASSIGNED.
<FS_FIELD>-COL_POS = P_COL.
<FS_FIELD>-FIELDNAME = P_FIELDNAME.
<FS_FIELD>-SCRTEXT_M = P_TEXT.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FETCH_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_FETCH_DATA .
SORT GT_FIELDCAT BY COL_POS.
" 根据列创建动态内表
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = GT_FIELDCAT
IMPORTING
EP_TABLE = LR_TABLE.
" 将表对象分配给指针
ASSIGN LR_TABLE->* TO <FS_TABLE>.
" 根据表对象创建行对象
CREATE DATA LR_LINE LIKE LINE OF <FS_TABLE>.
" 将行对象分配给指针
ASSIGN LR_LINE->* TO <FS_LINE>.
<FS_TABLE> = CORRESPONDING #( GT_DATA ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-CPROG
IT_FIELDCAT_LVC = GT_FIELDCAT
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = <FS_TABLE>.
ENDFORM.
|