做alv经常会要求带有header和footer信息,本文讲解如何制作带有header的alv,学会header之后,最后会贴出作者的代码。
插入header的关键: 在调用REUSE_ALV_GRID_DISPLAY 的时候传入一个subroutine给i_callback_top_of_page 参数,然后在该subroutine里面写代码。 要记住,想要显示header还必须传入当前程序名(sy-repid)给i_callback_program 参数,否则会无法显示。
以下是实例代码:
TYPE-POOLS: slis.
TABLES: sflight.
* Define internal table
DATA: gs_sflight TYPE sflight.
DATA: gt_sflight TYPE STANDARD TABLE OF sflight.
* Define ALV control data
DATA: gs_layout TYPE slis_layout_alv.
DATA: gs_field_cat TYPE slis_fieldcat_alv.
DATA: gt_field_cat TYPE slis_t_fieldcat_alv.
START-OF-SELECTION.
PERFORM sub_retrieve_data.
PERFORM sub_set_alv_properties.
PERFORM sub_alv_display.
*&---------------------------------------------------------------------*
*& Form SUB_RETRIEVE_DATA
*&---------------------------------------------------------------------*
* Retrieve data
*----------------------------------------------------------------------*
FORM sub_retrieve_data .
SELECT *
INTO TABLE gt_sflight
FROM sflight UP TO 20 ROWS.
ENDFORM. " SUB_RETRIEVE_DATA
*&---------------------------------------------------------------------*
*& Form SUB_ALV_DISPLAY
*&---------------------------------------------------------------------*
* Display data
*----------------------------------------------------------------------*
FORM sub_alv_display .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_top_of_page = 'SUB_TOP_OF_PAGE'
is_layout = gs_layout
it_fieldcat = gt_field_cat
i_save = 'X'
TABLES
t_outtab = gt_sflight.
ENDFORM. " SUB_ALV_DISPLAY
*&---------------------------------------------------------------------*
*& Form SUB_SET_ALV_PROPERTIES
*&---------------------------------------------------------------------*
* build field catelog and layout
*----------------------------------------------------------------------*
FORM sub_set_alv_properties .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = gt_field_cat.
gs_layout-colwidth_optimize = 'X'.
ENDFORM. " SUB_SET_ALV_PROPERTIES
*&---------------------------------------------------------------------*
*& Form sub_top_of_page
*&---------------------------------------------------------------------*
* subroutine to set header of ALV
*----------------------------------------------------------------------*
FORM sub_top_of_page.
DATA: lt_header TYPE slis_t_listheader.
DATA: ls_header TYPE slis_listheader.
ls_header-typ = 'H'.
ls_header-info = 'Flight Detailed Information'.
APPEND ls_header TO lt_header.
ls_header-typ = 'S'.
ls_header-key = 'Date:'.
ls_header-info = sy-datum.
APPEND ls_header TO lt_header.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = lt_header.
ENDFORM. "sub_top_of_page