方法1,也是大家常见的方法通过函数 REUSE_ALV_GRID_DISPLAY
TYPES: BEGIN OF ty_spfli, "定义结构
carrid like spfli-carrid,
connid like spfli-connid,
END OF ty_spfli.
DATA: it_spfli TYPE STANDARD TABLE OF ty_spfli. "定义内表
DATA:
wa_fieldcat TYPE slis_fieldcat_alv , " 相当于工作区
i_fieldcat TYPE slis_t_fieldcat_alv , " 存放输出栏位名称的列表
i_layout TYPE slis_layout_alv . " 负责整个ALV的全局属性
SELECT carrid connid INTO TABLE it_spfli "获取内表数据
FROM spfli
UP TO 10 ROWS.
PERFORM getdata.
PERFORM islayout.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "调用函数
EXPORTING
I_CALLBACK_PROGRAM = sy-repid
IS_LAYOUT = i_layout
IT_FIELDCAT = i_fieldcat[]
I_SAVE = 'X'
TABLES
t_outtab = it_spfli
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
FORM getdata.
wa_fieldcat-fieldname = 'CARRID'. " 需要输出的内表的字段名
wa_fieldcat-seltext_l = '航班号'. " 字段的描述-长字段标签
wa_fieldcat-seltext_m = '航班号'. " 字段描述-中字段标签
wa_fieldcat-seltext_s = '航班号'. " 字段描述-短字段标签
wa_fieldcat-fix_column = 'X' . " 是否是固定列
APPEND wa_fieldcat TO i_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'CONNID'. " 需要输出的内表的字段名
wa_fieldcat-seltext_l = '航班连接号'. " 字段的描述-长字段标签
wa_fieldcat-seltext_m = '航班连接号'. " 字段描述-中字段标签
wa_fieldcat-seltext_s = '航班连接号'. " 字段描述-短字段标签
wa_fieldcat-fix_column = 'X' . " 是否是固定列
APPEND wa_fieldcat TO i_fieldcat.
CLEAR wa_fieldcat.
ENDFORM.
FORM islayout.
i_layout-zebra = 'X' . " 使ALV界面呈现颜色交替
i_layout-colwidth_optimize = 'X' . " 自动优化列宽
i_layout-detail_initial_lines = 'X' . " 是否出现细节屏幕
ENDFORM.
方法2,也是大家常见的方法通过函数 REUSE_ALV_GRID_DISPLAY_LVC 在使用上会比第一种方法功能实现上多些
TYPES: BEGIN OF ty_spfli, "定义结构
carrid like spfli-carrid,
connid like spfli-connid,
END OF ty_spfli.
DATA: it_spfli TYPE STANDARD TABLE OF ty_spfli. "定义内表
DATA:
wa_fieldcat TYPE lvc_s_fcat , " 相当于工作区
i_fieldcat TYPE lvc_t_fcat , " 存放输出栏位名称的列表
i_layout TYPE lvc_s_layo . " 负责整个ALV的全局属性
SELECT carrid connid INTO TABLE it_spfli "获取内表数据
FROM spfli
UP TO 10 ROWS.
PERFORM getdata.
PERFORM islayout.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = sy-repid
IS_LAYOUT_LVC = i_layout
IT_FIELDCAT_LVC = i_fieldcat[]
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
t_outtab = it_spfli
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
FORM getdata.
wa_fieldcat-fieldname = 'CARRID'. " 需要输出的内表的字段名
wa_fieldcat-scrtext_l = '航班号'. " 字段的描述-长字段标签
wa_fieldcat-scrtext_m