实现ALV页眉页脚

1、文档介绍

在ALV中,可以通过增加页眉和页脚,丰富ALV的展示。除了基本的页眉和页脚,还可以通过插入HTML代码的方式展示更加丰富的页眉和页脚,本篇文章将介绍ALV和OOALV中页眉页脚的使用。

2、ALV页眉页脚

效果如下

2.1、显示内容

页眉和页脚,都通过函数REUSE_ALV_COMMENTARY_WRITE显示内容。

页眉

"--------------------@斌将军--------------------
FORM FRM_TOP_OF_PAGE .

  DATA:lt_listheader TYPE slis_t_listheader,
       ls_listheader TYPE slis_listheader.

  CLEAR:ls_listheader.
  ls_listheader-typ  = 'H'."标题
  ls_listheader-info = '页眉'.
  APPEND ls_listheader TO lt_listheader.

  CLEAR ls_listheader.
  ls_listheader-typ  = 'S'.
  ls_listheader-key  = '编制单位:'.
  ls_listheader-info = '1100'.
  APPEND ls_listheader TO lt_listheader.

  CLEAR ls_listheader.
  ls_listheader-typ  = 'S'.
  ls_listheader-info = '1200'.
  APPEND ls_listheader TO lt_listheader.

  CLEAR ls_listheader.
  ls_listheader-typ  = 'A'.
  ls_listheader-info = '单位:元'.
  APPEND ls_listheader TO lt_listheader.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = lt_listheader "显示的数据
      i_logo             = 'ENJOYSAP_LOGO' "显示的图片LOGO
*     I_END_OF_LIST_GRID =
*     I_ALV_FORM         =
    .
ENDFORM. " FRM_TOP_OF_PAGE
"--------------------@斌将军--------------------

页脚

"--------------------@斌将军--------------------
FORM FRM_END_OF_LIST .

  DATA:lt_listheader TYPE slis_t_listheader,
       ls_listheader TYPE slis_listheader.

  CLEAR:ls_listheader.
  ls_listheader-typ  = 'H'.
  ls_listheader-info = '页脚'.
  APPEND ls_listheader TO lt_listheader.

  CLEAR ls_listheader.
  ls_listheader-typ  = 'S'.
  ls_listheader-key  = '作者:'.
  ls_listheader-info = '斌将军'.
  APPEND ls_listheader TO lt_listheader.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = lt_listheader
      i_logo             = 'ENJOYSAP_LOGO'
*     I_END_OF_LIST_GRID =
*     I_ALV_FORM         =
    .
ENDFORM. " FRM_END_OF_LIST
"--------------------@斌将军--------------------

 其中的LOGO,可以通过事务代码OAER,输入对应信息

 找到对应LOGO

2.2、ALV事件方式实现

实现页眉和页脚,都可以通过注册ALV事件EVENT,调用对应的子例程,显示对应内容

 事件代码

"--------------------@斌将军--------------------

DATA:gt_event TYPE slis_t_event."事件


FORM frm_get_event.

  DATA:lv_frm_top_of_page TYPE slis_formname VALUE 'FRM_TOP_OF_PAGE',"页眉子例程
       lv_frm_end_of_list TYPE slis_formname VALUE 'FRM_END_OF_LIST',"页脚子例程
       ls_events          TYPE slis_alv_event.

  "获取事件
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type     = 0
    IMPORTING
      et_events       = gt_event
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty
        NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  "查找TOP_OF_PAGE事件,并将回调子例程传输给FORM
  READ TABLE gt_event ASSIGNING FIELD-SYMBOL(<fs_ls_event>) WITH KEY name = slis_ev_top_of_page.
  IF sy-subrc = 0.
    <fs_ls_event>-form = lv_frm_top_of_page.
  ELSE.
    CLEAR:ls_events.
    ls_events-form = lv_frm_top_of_page.
    ls_events-name = lv_frm_top_of_page.
    APPEND ls_events TO gt_event.
  ENDIF.

  "查找END_OF_LIST事件,并将回调子例程传输给FORM
  READ TABLE gt_event ASSIGNING <fs_ls_event> WITH KEY name = slis_ev_end_of_list.
  IF sy-subrc = 0.
    <fs_ls_event>-form = lv_frm_end_of_list.
  ELSE.
    CLEAR:ls_events.
    ls_events-form = lv_frm_end_of_list.
    ls_events-name = lv_frm_end_of_list.
    APPEND ls_events TO gt_event.
  ENDIF.

ENDFORM. " FRM_GET_EVENT
"--------------------@斌将军--------------------

在调用ALV时,传入EVENT

2.3、ALV参数实现页眉

如果单独实现页眉效果,就不需要这么复杂,可以直接将页眉子例程传入ALV函数REUSE_ALV_GRID_DISPLAY_LVC的I_CALLBACK_TOP_OF_PAGE参数中,调用子例程

3、ALV展示HTML页眉页脚

效果如下

ALV

页眉和页脚的子例程代码类似

"--------------------@斌将军--------------------
FORM frm_html_top_of_page USING document TYPE REF TO cl_dd_document.

  DATA:lv_pos    TYPE i,
       lv_text   TYPE sdydo_text_element,
       lv_buffer TYPE string.

  lv_buffer = '<HTML><CENTER><H1>' && '页眉' && '</H1></CENTER></HTML>'.

  CALL METHOD document->html_insert
    EXPORTING
      contents = lv_buffer
    CHANGING
      position = lv_pos.

  lv_text = '标题'.
  CALL METHOD document->add_text
    EXPORTING
      text      = lv_text
      sap_style = 'HEADING'.

  "插入两行空行
  CALL METHOD document->new_line.

  CALL METHOD document->add_icon
    EXPORTING
      sap_icon = 'ICON_CUSTOMER'.

  "STRONG是加粗
  lv_text = '用户名:'.
  CALL METHOD document->add_text
    EXPORTING
      text         = lv_text
      sap_emphasis = 'Strong'.

  "间隔
  CALL METHOD document->add_gap
    EXPORTING
      width = 2.

  lv_text = '斌将军'.
  CALL METHOD document->add_text
    EXPORTING
      text          = lv_text
      sap_fontsize  = cl_dd_document=>large
      sap_fontstyle = cl_dd_document=>serif
      sap_emphasis  = cl_dd_document=>emphasis.

  "间隔
  CALL METHOD document->add_gap
    EXPORTING
      width = 10.

  CALL METHOD document->add_icon
    EXPORTING
      sap_icon = 'ICON_DATE'.

  lv_text = '日期:'.
  CALL METHOD document->add_text
    EXPORTING
      text         = lv_text
      sap_emphasis = 'Strong'.

  CALL METHOD document->add_gap
    EXPORTING
      width = 2.

  lv_text = '20240420'.
  CALL METHOD document->add_text
    EXPORTING
      text      = lv_text
      sap_style = 'Key'.

  CALL METHOD document->add_gap
    EXPORTING
      width = 10.

  CALL METHOD document->add_icon
    EXPORTING
      sap_icon = 'ICON_INFORMATION'.

  lv_text = '链接:'.
  CALL METHOD document->add_text
    EXPORTING
      text         = lv_text
      sap_emphasis = 'Strong'.

  CALL METHOD document->add_link
    EXPORTING
      url     = 'http://www.baidu.com'
      tooltip = '百度' "小手指的提示信息
      text    = 'Baidu'.

  "横线
  CALL METHOD document->underline.

ENDFORM.                    "HTML_TOP_OF_PAGE
"--------------------@斌将军--------------------

4、OOALV页眉页脚

效果如下

首先,如上图将界面分为上、中、下三部分。关于屏幕分割,也可以参考《OOALV 分割屏幕 

"--------------------@斌将军--------------------
*&---------------------------------------------------------------------*
*&      Form  FRM_CREATE_CONTAINER_9000
*&      创建容器
*&---------------------------------------------------------------------*
FORM frm_create_container_9000.

  "创建 容器
  CREATE OBJECT g_container_9000
    EXPORTING
      repid                       = sy-repid
      dynnr                       = '9000'
      side                        = cl_gui_docking_container=>dock_at_top "ALV贴屏幕左边,从左边算屏幕宽度,
      extension                   = 1000"屏幕宽度
      style                       = cl_gui_control=>ws_child "可选参数,设置ALV是否可用手动拖动大小
    EXCEPTIONS
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5
      OTHERS                      = 6.

  IF sy-subrc <> 0.
    MESSAGE s001(00) WITH '屏幕初始化失败'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  "拆分成3行1列部分
  CREATE OBJECT g_splitter
    EXPORTING
      parent  = g_container_9000
      rows    = 3
      columns = 1.

  "第一行第一列  "用作页眉
  CALL METHOD g_splitter->get_container
    EXPORTING
      row       = 1
      column    = 1
    RECEIVING
      container = g_container_t.

  CALL METHOD g_splitter->set_row_height   "设置行高
    EXPORTING
      id     = 1        "第几行
      height = 20.

  "第二行第一列 用作展示主要数据
  CALL METHOD g_splitter->get_container
    EXPORTING
      row       = 2
      column    = 1
    RECEIVING
      container = g_container_m.

  "第三行第一列   "用作页脚
  CALL METHOD g_splitter->get_container
    EXPORTING
      row       = 3
      column    = 1
    RECEIVING
      container = g_container_e.

  CALL METHOD g_splitter->set_row_height   "设置行高
    EXPORTING
      id     = 3        "第几行
      height = 20.

  CREATE OBJECT g_grid "中间部分为展示主数据
    EXPORTING
      i_parent = g_container_m.
ENDFORM.
"--------------------@斌将军--------------------

定义类和方法,实现页眉和页脚

"--------------------@斌将军-------------------- 
 CLASS alv_event_9000 DEFINITION DEFERRED."

CLASS alv_event_9000 DEFINITION.

  PUBLIC SECTION.
    METHODS handle_top_of_page FOR EVENT top_of_page OF cl_gui_alv_grid
      IMPORTING e_dyndoc_id.
    METHODS handle_end_of_list FOR EVENT end_of_list OF cl_gui_alv_grid
      IMPORTING e_dyndoc_id.
ENDCLASS. "alv_event_9000 DEFINITION

*----------------------------------------------------------------------*
*   CLASS BELOW_ALV_EVENT IMPLEMENTATION 定义的事件类对应的事件响应模块
*----------------------------------------------------------------------*
CLASS alv_event_9000 IMPLEMENTATION.

  METHOD handle_top_of_page.
    PERFORM frm_event_top_of_page USING g_dyndoc_t.
  ENDMETHOD.

  METHOD handle_end_of_list.
    PERFORM frm_event_end_of_list USING g_dyndoc_e.
  ENDMETHOD.

ENDCLASS. "alv_event_9000 IMPLEMENTATION
"--------------------@斌将军--------------------

在事件中注册页眉和页脚方法

"--------------------@斌将军--------------------
FORM frm_alv_event_9000.
  DATA: lr_event_handler TYPE REF TO alv_event_9000.

  IF lr_event_handler IS INITIAL.
    CREATE OBJECT lr_event_handler.
  ENDIF.

  SET HANDLER lr_event_handler->handle_top_of_page FOR g_grid.
  SET HANDLER lr_event_handler->handle_end_of_list FOR g_grid.

ENDFORM.
"--------------------@斌将军--------------------

显示ALV

"--------------------@斌将军--------------------
CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_save                        = 'A'
      i_default                     = 'X' "允许定义默认布局
      is_layout                     = gs_layout
    CHANGING
      it_outtab                     = gt_alv
      it_fieldcatalog               = gt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
  ENDIF.

  CREATE OBJECT g_dyndoc_t
    EXPORTING
      style = 'ALV_GRID'.
*  CALL METHOD g_dyndoc_t->initialize_document
*    EXPORTING
*      background_color = cl_dd_area=>col_textarea.
  "对于页眉使用TOP_OF_PAGE事件
  CALL METHOD g_grid->list_processing_events
    EXPORTING
      i_event_name = 'TOP_OF_PAGE'
      i_dyndoc_id  = g_dyndoc_t.

  CREATE OBJECT g_dyndoc_e
    EXPORTING
      style = 'ALV_GRID'.
  "对于页脚使用END_OF_LIST事件
  CALL METHOD g_grid->list_processing_events
    EXPORTING
      i_event_name = 'END_OF_LIST'
      i_dyndoc_id  = g_dyndoc_e.
"--------------------@斌将军--------------------

页眉和页脚的子例程类似,页脚子例程如下

"--------------------@斌将军--------------------
FORM frm_event_end_of_list USING document TYPE REF TO cl_dd_document.
  DATA:lv_pos    TYPE i,
       lv_text   TYPE sdydo_text_element,
       lv_buffer TYPE string.

  lv_buffer = '<HTML><CENTER><H1>' && '页脚' && '</H1></CENTER></HTML>'.

  CALL METHOD document->html_insert
    EXPORTING
      contents = lv_buffer
    CHANGING
      position = lv_pos.

  "页脚HTML视图
  IF g_html_e IS INITIAL.
    CREATE OBJECT g_html_e
      EXPORTING
        parent = g_container_e.
  ENDIF.
  CALL METHOD document->merge_document.
  document->html_control = g_html_e.

  "显示
  CALL METHOD document->display_document
    EXPORTING
      reuse_control      = 'X'
      parent             = g_container_e
    EXCEPTIONS
      html_display_error = 1.
ENDFORM.
"--------------------@斌将军--------------------

参考代码,可通过链接获取

链接:https://pan.baidu.com/s/18bNmfmC9zSh1Si8W6AhQCQ?pwd=c0oj
提取码:c0oj

5、总结

以上就是关于ALV合并单元格的介绍,希望对您有所帮助。

定期更文,欢迎关注

 

 
 
posted @ 2024-04-24 17:37  斌将军  阅读(69)  评论(0编辑  收藏  举报