方法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 在使用上會比第一種方法功能實現上多些ui
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 ='航班號'