HR350中的Unit 4介绍了使用ALV(ABAP list viewer)来格式化report的数据输出,使输出的数据具有排序,打印,搜索,过滤,隐藏/显示特定字段等功能。使用ALV最基本的步骤包括:
1. 把要显示的数据都放到一个内表中
2. 定义一个field catalog,用于确定要显示的列
3. 调用函数 REUSE_ALV_GRID_DISPLAY
下面是一个完整的例子。*&---------------------------------------------------------------------*
*& Report ZHR350_alv
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zhr350_alv.
TABLES: pernr.
INFOTYPES: 0041.
TYPE-POOLS: slis.
DATA: alv_fieldcat TYPE slis_t_fieldcat_alv,
alv_layout TYPE slis_layout_alv,
stru_disvar TYPE disvariant.
DATA: v_dtext TYPE t548t-dtext.
DATA: dar LIKE p0041-dar01,
dat LIKE p0041-dat01.
DATA: BEGIN OF wa,
pernr TYPE p0041-pernr,
ename TYPE pernr-ename,
dar TYPE p0041-dar01,
dtext TYPE t548t-dtext,
dat TYPE p0041-dat01,
END OF wa,
i_tab LIKE TABLE OF wa.
START-OF-SELECTION.
GET pernr.
PROVIDE * FROM p0041
BETWEEN p0041-begda AND p0041-endda.
MOVE p0041-pernr TO wa-pernr.
MOVE pernr-ename TO wa-ename.
DO 12 TIMES VARYING dar FROM p0041-dar01
NEXT p0041-dar02
VARYING dat FROM p0041-dat01
NEXT p0041-dat02.
SELECT SINGLE dtext FROM t548t INTO v_dtext
WHERE datar = dar
AND sprsl = sy-langu.
IF dar IS INITIAL.
EXIT.
ELSE.
MOVE dar TO wa-dar.
MOVE v_dtext TO wa-dtext.
MOVE dat TO wa-dat.
APPEND wa TO i_tab.
ENDIF.
CLEAR: v_dtext, dar, dat.
ENDDO.
ENDPROVIDE.
END-OF-SELECTION.
PERFORM fieldcat_init USING alv_fieldcat.
alv_layout-colwidth_optimize = 'X'.
alv_layout-zebra = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
i_callback_program = sy-cprog
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
i_grid_title = 'Person Infos'
* I_GRID_SETTINGS =
is_layout = alv_layout
it_fieldcat = alv_fieldcat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
i_save = 'X'
is_variant = stru_disvar
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = i_tab
EXCEPTIONS
program_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
WRITE: / 'Fehler bei Listeanzeige'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FIELDCAT_INIT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_ALV_FIELDCAT text
*----------------------------------------------------------------------*
FORM fieldcat_init USING p_fieldcat
TYPE slis_t_fieldcat_alv.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'PERNR'.
ls_fieldcat-ref_tabname = 'PERNR'.
ls_fieldcat-key = 'X'.
APPEND ls_fieldcat TO p_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'ENAME'.
ls_fieldcat-ref_tabname = 'PERNR'.
APPEND ls_fieldcat TO p_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'DAR'.
ls_fieldcat-ref_fieldname = 'DAR01'.
ls_fieldcat-ref_tabname = 'P0041'.
APPEND ls_fieldcat TO p_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'DTEXT'.
ls_fieldcat-ref_tabname = 'T548T'.
APPEND ls_fieldcat TO p_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'DAT'.
ls_fieldcat-ref_fieldname = 'DAT01'.
ls_fieldcat-ref_tabname = 'P0041'.
APPEND ls_fieldcat TO p_fieldcat.
CLEAR ls_fieldcat.
ENDFORM. " FIELDCAT_INIT
要注意的是在定义field catalog时,ls_fieldcat-ref_fieldname 和ls_fieldcat-ref_tabname 的值必须为大写!否则输出的字段名为空。
没有评论:
发表评论