- 先看看效果叭
- CL_SALV_TABLE提供了SET_TOP_OF_LIST方法设置页眉显示和SET_TOP_OF_LIST_PRINT方法设置页眉打印来实现添加页眉的目的。
- CL_SALV_TABLE提供了SET_END_OF_LIST方法设置页脚显示和SET_END_OF_LIST_PRINT方法设置页脚打印来实现添加页脚的目的。
- 这个四个方法的传入参数为CL_SALV_FORM_LAYOUT_GRID类型,这是个类。
- 实现步骤
- 定义两个私有方法;SET_TOPPAGE:添加页眉,SET_ENDPAGE:添加页脚
PRIVATE SECTION. METHODS: set_toppage CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: set_endpage CHANGING co_alv TYPE REF TO cl_salv_table.
METHOD set_toppage. DATA: lo_h_label TYPE REF TO cl_salv_form_label, lo_h_flow TYPE REF TO cl_salv_form_layout_flow. DATA(lo_header) = NEW cl_salv_form_layout_grid( ). * 通过Row 和 column字段定位在top of page的文本输出位置 * 粗体 第一行第一列 lo_h_label = lo_header->create_label( row = 1 column = 1 ). lo_h_label->set_text( 'Sale Orders of Report' ). * 第二行第一列 lo_h_flow = lo_header->create_flow( row = 2 column = 1 ). lo_h_flow->create_text( text = 'Create By:' && sy-uname ). * 第三行第一列 lo_h_flow = lo_header->create_flow( row = 3 column = 1 ). lo_h_flow->create_text( text = 'Number of Records in the output' ). * 第三行第二列 lo_h_flow = lo_header->create_flow( row = 3 column = 2 ). lo_h_flow->create_text( text = '20' ). * 页眉显示 co_alv->set_top_of_list( lo_header ). * 页眉打印 co_alv->set_top_of_list_print( lo_header ). ENDMETHOD. METHOD set_endpage. DATA: lo_f_label TYPE REF TO cl_salv_form_label, lo_f_flow TYPE REF TO cl_salv_form_layout_flow. DATA(lo_footer) = NEW cl_salv_form_layout_grid( ). * 通过Row 和 column字段定位在top of page的文本输出位置 * 粗体 第一行第一列 lo_f_label = lo_footer->create_label( row = 1 column = 1 ). lo_f_label->set_text( 'Bilibili University' ). * 第二行第一列 lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ). lo_f_flow->create_text( text = 'Date:' && sy-datum ). * lo_f_flow = lo_footer->create_flow( row = 3 column = 1 ). lo_f_flow->create_text( text = 'Footer number' ). * lo_f_flow = lo_footer->create_flow( row = 3 column = 2 ). lo_f_flow->create_text( text = 1 ). * 页眉打印 co_alv->set_end_of_list( lo_footer ). * 页脚打印 co_alv->set_end_of_list_print( lo_footer ). ENDMETHOD.
- 同样的也是在创建SALV实例对象后调用方法
TRY. cl_salv_table=>factory( IMPORTING r_salv_table = mo_salv CHANGING t_table = mt_data ). CATCH cx_salv_msg INTO DATA(lx_msg). ENDTRY. me->set_toppage( CHANGING co_alv = mo_salv ). me->set_endpage( CHANGING co_alv = mo_salv ).
- 完整代码
CLASS lcl_model DEFINITION. PUBLIC SECTION. TYPES:BEGIN OF TY_dATA, vbeln TYPE vbak-vbeln, erdat TYPE erdat, erzet TYPE erzet, ernam TYPE ernam, auart TYPE auart. TYPES:END OF ty_data. DATA mt_data TYPE TABLE OF ty_data. DATA mo_salv TYPE REF TO cl_salv_table. "获取数据的方法 METHODS get_data. "展示SALV的方法 METHODS alv_show. PRIVATE SECTION. METHODS: set_status CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: set_toppage CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: set_endpage CHANGING co_alv TYPE REF TO cl_salv_table. ENDCLASS. CLASS lcl_model IMPLEMENTATION. METHOD get_data. SELECT vbeln erdat erzet ernam auart INTO TABLE mt_data FROM vbak UP TO 20 ROWS. ENDMETHOD. METHOD alv_show. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = mo_salv CHANGING t_table = mt_data ). CATCH cx_salv_msg INTO DATA(lx_msg). ENDTRY. me->set_status( CHANGING co_alv = mo_salv ). me->set_toppage( CHANGING co_alv = mo_salv ). me->set_endpage( CHANGING co_alv = mo_salv ). mo_salv->display( ). ENDMETHOD. METHOD set_status. DATA(lo_functions) = co_alv->get_functions( ). "cl_salv_functions_list类型的实例对象的添加状态方法 lo_functions->set_all( abap_true ). "lo_functions->set_default( abap_true ). ENDMETHOD. METHOD set_toppage. DATA: lo_h_label TYPE REF TO cl_salv_form_label, lo_h_flow TYPE REF TO cl_salv_form_layout_flow. DATA(lo_header) = NEW cl_salv_form_layout_grid( ). * 通过Row 和 column字段定位在top of page的文本输出位置 * 粗体 第一行第一列 lo_h_label = lo_header->create_label( row = 1 column = 1 ). lo_h_label->set_text( 'Sale Orders of Report' ). * 第二行第一列 lo_h_flow = lo_header->create_flow( row = 2 column = 1 ). lo_h_flow->create_text( text = 'Create By:' && sy-uname ). * 第三行第一列 lo_h_flow = lo_header->create_flow( row = 3 column = 1 ). lo_h_flow->create_text( text = 'Number of Records in the output' ). * 第三行第二列 lo_h_flow = lo_header->create_flow( row = 3 column = 2 ). lo_h_flow->create_text( text = '20' ). * * 页眉显示 co_alv->set_top_of_list( lo_header ). * * 页眉打印 co_alv->set_top_of_list_print( lo_header ). ENDMETHOD. METHOD set_endpage. DATA: lo_f_label TYPE REF TO cl_salv_form_label, lo_f_flow TYPE REF TO cl_salv_form_layout_flow. DATA(lo_footer) = NEW cl_salv_form_layout_grid( ). * 通过Row 和 column字段定位在top of page的文本输出位置 * 粗体 第一行第一列 lo_f_label = lo_footer->create_label( row = 1 column = 1 ). lo_f_label->set_text( 'Bilibili University' ). * 第二行第一列 lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ). lo_f_flow->create_text( text = 'Date:' && sy-datum ). * lo_f_flow = lo_footer->create_flow( row = 3 column = 1 ). lo_f_flow->create_text( text = 'Footer number' ). * lo_f_flow = lo_footer->create_flow( row = 3 column = 2 ). lo_f_flow->create_text( text = 1 ). co_alv->set_end_of_list( lo_footer ). * 页脚打印 co_alv->set_end_of_list_print( lo_footer ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_model) = NEW lcl_model( ). lo_model->get_data( ). lo_model->alv_show( ).