动态内表展示

可根据输入的表名,动态获取数据,动态生成ALV。
主要的方法是根据输入的表或结构 获取字段目录生成动态内表。
ALV field也根据函数字段生成 根据条件将取到的数据赋值到动态内表中展示。
&---------------------------------------------------------------------
*& Report ZTEST001
&---------------------------------------------------------------------
*&
&---------------------------------------------------------------------
REPORT ZTEST001.

&---------------------------------

  • 自定义全局变量
    &---------------------------------
    TABLES: MARA,DD03L.
    TYPE-POOLS: SLIS.
    DATA: DYN_WA TYPE REF TO DATA.
    FIELD-SYMBOLS :

TYPE TABLE,

TYPE ANY,

TYPE ANY,

TYPE ANY.

DATA: LI_DFIES_TAB TYPE TABLE OF DFIES,
LS_DFIES_TAB TYPE DFIES.

DATA: LI_X031L TYPE TABLE OF X031L,
LS_X031L TYPE X031L.

DATA: D_REF TYPE REF TO DATA,
LT_ALV_CAT TYPE TABLE OF LVC_S_FCAT,
LS_ALV_CAT LIKE LINE OF LT_ALV_CAT.
"ALV相关
DATA: I_FIELDCAT TYPE LVC_T_FCAT, "内表-字段属性
WA_FIELDCAT TYPE LVC_S_FCAT, "工作区-字段属性
WA_LAYOUT TYPE LVC_S_LAYO. "工作区-布局

&---------------------------------

  • 选择画面
    &---------------------------------

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

SELECT-OPTIONS:
S_TABLE FOR DD03L-TABNAME NO-EXTENSION NO INTERVALS.

SELECTION-SCREEN END OF BLOCK B1.

&---------------------------------

  • 报表程序主逻辑
    &---------------------------------

START-OF-SELECTION.

CALL FUNCTION ‘DDIF_NAMETAB_GET’
EXPORTING
TABNAME = S_TABLE-LOW
ALL_TYPES = ’ ’
LFIELDNAME = ’ ’
GROUP_NAMES = ’ ’

  • uclen       = '00'
    STATUS      = 'A'
    
    TABLES
    X031L_TAB = LI_X031L
    DFIES_TAB = LI_DFIES_TAB
    EXCEPTIONS
    NOT_FOUND = 1
    OTHERS = 2.

*根据取出的字段目录生成参考字段目录
LOOP AT LI_DFIES_TAB INTO LS_DFIES_TAB.

LS_ALV_CAT-FIELDNAME = LS_DFIES_TAB-FIELDNAME.
LS_ALV_CAT-REF_TABLE = S_TABLE-LOW.
LS_ALV_CAT-REF_FIELD = LS_DFIES_TAB-FIELDNAME.
APPEND LS_ALV_CAT TO LT_ALV_CAT.
CLEAR LS_ALV_CAT.

ENDLOOP.

*创建动态内表
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = LT_ALV_CAT
IMPORTING
EP_TABLE = D_REF.

  • 指定生成的内表到字段符号
    ASSIGN D_REF->* TO .
  • 创建动态工作区结构
    CREATE DATA DYN_WA LIKE LINE OF .
  • 创建动态工作区
    ASSIGN DYN_WA->* TO .

SELECT * FROM (S_TABLE-LOW) INTO CORRESPONDING FIELDS OF TABLE .

*-设置alv格式
CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’
EXPORTING
I_STRUCTURE_NAME = S_TABLE-LOW
CHANGING
CT_FIELDCAT = I_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2.

WA_LAYOUT-CWIDTH_OPT = ‘X’. "优化列宽设置
WA_LAYOUT-ZEBRA = ‘X’. "斑马线

*-调用ALV
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID "程序名

  • I_CALLBACK_PF_STATUS_SET    = 'FRM_SET_STATUS' "状态
    
  •  I_CALLBACK_USER_COMMAND = 'FRM_USER_COMM' "双击事件
    
  • I_CALLBACK_HTML_TOP_OF_PAGE = 'FRM_TOP_OF_PAGE' "抬头设置
    
  • it_sort_lvc             = li_sort
    IS_LAYOUT_LVC           = WA_LAYOUT          "输出格式
    IT_FIELDCAT_LVC         = I_FIELDCAT         "项目名
    
    TABLES
    T_OUTTAB =
    EXCEPTIONS
    PROGRAM_ERROR = 1
    OTHERS = 2.

**GUI
*FORM FRM_SET_STATUS.
*

  • SET PF-STATUS ‘S1000’.
    *ENDFORM.

**用户按钮事件
FORM FRM_USER_COMM USING UCOMM LIKE SY-UCOMM
SELFIELD TYPE SLIS_SELFIELD…

ENDFORM.

*HTML抬头
FORM FRM_TOP_OF_PAGE USING P_CL_DD TYPE REF TO CL_DD_DOCUMENT…

ENDFORM.

你可能感兴趣的:(ABAP,动态内表,动态ALV)