ABAP - SALV教程05 添加页眉和页脚

  • 先看看效果叭

  • 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( ).
    
相关推荐
冰魄禾5 天前
SAP ABAP开发学习——BAPI
运维·开发语言·笔记·运维开发·sap·abap
Lisson 315 天前
The ABAP program lines are wider than the internal table.
abap
1314lay_10072 个月前
FUNCTION_ALV 下拉框的实现
abap
Fireworks_me2 个月前
SAP 有趣的‘bug‘ 选择屏幕输入框没了
abap
李安迪是大神3 个月前
上传PDF、DOC文件到SAP HCM系统中案例
pdf·word·sap·abap·sap erp
荀彧原名苟或3 个月前
SAP MIGO屏幕增强的具体实施步骤介绍(SE19:MB_MIGO_BADI) <转载>
java·数据库·缓存·sap·abap
LilySesy3 个月前
ABAP小白开发操作手册+(九)ABAP调用http
开发语言·网络·网络协议·http·sap·abap
LilySesy3 个月前
ABAP+从SAP发出去的PDF文件在第三方系统出现乱码
开发语言·程序人生·pdf·sap·abap
酔墨诗欣4 个月前
第一弹:基于ABAP OLE技术实现对服务器文件进行读写操作
sap·abap·bw/4hana·ole
Fireworks_me4 个月前
ABAP 生成word文档
word·abap