除了可以用REUSE_ALV_FIELDCATALOG_MERGE函数来获取FIELDCAT之外,还可以自定义FIELDCAT参数,不过这种方式需要自定义字段名称。
REPORT ZZWEI_ALV_DEMO.
TYPE-POOLS:SLIS."引用类型池
TABLES:SPFLI.
DATA:fieldCat TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
layout TYPE SLIS_LAYOUT_ALV,
w_repid LIKE SY-REPID.
DATA:lspfli LIKE STANDARD TABLE OF SPFLI WITH HEADER LINE.
*屏幕开始事件
START-OF-SELECTION.
PERFORM GETDATA.
PERFORM CATALOG.
PERFORM ALVSHOW.
FORM GETDATA.
SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF TABLE lspfli.
ENDFORM.
FORM CATALOG.
w_repid = SY-REPID.
CLEAR fieldCat.
"定义宏来设置FIELDAT属性 &1 &2 &3分别为参数
DEFINE fieldcatset.
fieldCat-REF_TABNAME ='LSPFLI'.
fieldCat-FIELDNAME = &1.
fieldCat-SELTEXT_L = &2.
fieldCat-COL_POS = &3.
APPEND fieldCat.
END-OF-DEFINITION.
fieldcatset 'CARRID' '航线承运人' SY-TABIX.
fieldcatset 'CONNID' '航班数量' SY-TABIX.
fieldcatset 'COUNTRYFR' '起飞国家' SY-TABIX.
fieldcatset 'CITYFROM' '起飞城市' SY-TABIX.
fieldcatset 'AIRPFROM' '部门机场' SY-TABIX.
fieldcatset 'COUNTRYTO' '目的国家' SY-TABIX.
fieldcatset 'AIRPTO' '目的机场' SY-TABIX.
fieldcatset 'FLTIME' '航班时间' SY-TABIX.
fieldcatset 'DEPTIME' '离开' SY-TABIX.
fieldcatset 'ARRTIME' '到达时间' SY-TABIX.
fieldcatset 'DISTANCE' '距离' SY-TABIX.
fieldcatset 'DISTID' '位于' SY-TABIX.
fieldcatset 'FLTYPE' '图解' SY-TABIX.
fieldcatset 'PERIOD' '周期' SY-TABIX.
layout-COLWIDTH_OPTIMIZE = 'x'. "设置LAYOUT输出格式最优化
ENDFORM.
FORM ALVSHOW.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = w_repid
"I_SAVE = ''
IS_LAYOUT = layout
I_GRID_TITLE = 'AlV DEMO'
IT_FIELDCAT = fieldCat[]
TABLES
T_OUTTAB = lspfli
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.
运行界面如下: