除了可以通过类CL_SALV_EVENTS_TABLE的事件设置ALV的页眉页脚之外,还可以通过类CL_SALV_TABLE的方法SET_TOP_OF_LIST,SET_TOP_OF_LIST_PRINT,
SET_END_OF_LIST,SET_END_OF_LIST_PRINT这四个方法。页眉页脚的显示可以设置显示和打印时不一样,打印时的页眉页脚通过方法SET_TOP_OF_LIST_PRINT和SET_END_OF_LIST_PRINT。
1. 声明设置页眉页脚所需要的Layout的实例和变量。
DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid,
lr_grid2 TYPE REF TO cl_salv_form_layout_grid,
lr_text TYPE REF TO cl_salv_form_text,
lr_action TYPE REF TO cl_salv_form_action_info,
lr_header TYPE REF TO cl_salv_form_header_info,
lr_label TYPE REF TO cl_salv_form_label.
2. 添加页眉页脚所需要的文本到Layout里。
lr_header = lr_grid->create_header_information(
row = 1
column = 1
text = 'Header Information'
).
lr_label = lr_grid->create_label(
row = 2
column = 1
text = 'Label'
).
lr_action = lr_grid->create_action_information(
row = 3
column = 1
text = 'Action Information'
).
lr_text = lr_grid->create_text(
text = 'text'
row = 4
column = 1
).
gr_table->set_top_of_list( lr_grid ).
lr_text = lr_grid2->create_text(
text = 'end of page'
row = 1
column = 1
).
3. 使用方法SET_TOP_OF_LIST设置页眉
gr_table->set_top_of_list( lr_grid ).
下面是效果图
下面是程序的所有代码:
REPORT y_xin_002.
*变量定义
TYPES: BEGIN OF gs_spfli,
mandt TYPE spfli-mandt,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
countryto TYPE spfli-countryto,
cityto TYPE spfli-cityto,
airpto TYPE spfli-airpto,
fltime TYPE spfli-fltime,
deptime TYPE spfli-deptime,
arrtime TYPE spfli-arrtime,
distance TYPE spfli-distance,
distid TYPE spfli-distid,
fltype TYPE spfli-fltype,
period TYPE spfli-period,
END OF gs_spfli.
TYPES ty_spfli TYPE gs_spfli OCCURS 0.
*----------------------------------------------------------------------*
* CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
* ALV操作类(定义)
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
PUBLIC SECTION.
METHODS: getdata "取得要显示的数据
RETURNING value(lt_tab) TYPE ty_spfli,
alv_full "全屏Grid列表处理方法
IMPORTING value(lt_tab) TYPE ty_spfli.
PRIVATE SECTION.
DATA: gr_table TYPE REF TO cl_salv_table.
ENDCLASS. "lcl_alv DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
* ALV操作类(实现)
*----------------------------------------------------------------------*
CLASS lcl_alv IMPLEMENTATION.
*取得要显示的数据
METHOD getdata.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_tab FROM spfli.
ENDMETHOD. "getdata
*输出全屏网格列表的方法
METHOD alv_full.
DATA: lr_functions TYPE REF TO cl_salv_functions_list,
lr_grid TYPE REF TO cl_salv_form_layout_grid,
lr_grid2 TYPE REF TO cl_salv_form_layout_grid,
lr_text TYPE REF TO cl_salv_form_text,
lr_action TYPE REF TO cl_salv_form_action_info,
lr_header TYPE REF TO cl_salv_form_header_info,
lr_label TYPE REF TO cl_salv_form_label.
"创建实例
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = gr_table
CHANGING
t_table = lt_tab
).
CATCH cx_salv_msg.
ENDTRY.
"ALV标准功能
lr_functions = gr_table->get_functions( ).
lr_functions->set_all( 'X' ).
"页眉页脚
CREATE OBJECT: lr_grid,lr_grid2.
lr_header = lr_grid->create_header_information(
row = 1
column = 1
text = 'Header Information'
).
lr_label = lr_grid->create_label(
row = 2
column = 1
text = 'Label'
).
lr_action = lr_grid->create_action_information(
row = 3
column = 1
text = 'Action Information'
).
lr_text = lr_grid->create_text(
text = 'text'
row = 4
column = 1
).
gr_table->set_top_of_list( lr_grid ).
lr_text = lr_grid2->create_text(
text = 'end of page'
row = 1
column = 1
).
gr_table->set_end_of_list( lr_grid2 ).
"显示列表
gr_table->display( ).
ENDMETHOD. "alv_full
ENDCLASS. "lcl_alv IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Form f_main
*&---------------------------------------------------------------------*
* 整合数据,执行
*----------------------------------------------------------------------*
FORM f_main.
DATA: lt_tab TYPE ty_spfli,
lr_alv TYPE REF TO lcl_alv.
CREATE OBJECT lr_alv.
"取得要显示的数据
lt_tab = lr_alv->getdata( ).
lr_alv->alv_full( lt_tab ).
ENDFORM. "f_main
*执行动作
START-OF-SELECTION.
PERFORM f_main.