首页 > 其他分享 >实现ALV页眉页脚

实现ALV页眉页脚

时间:2024-04-24 17:44:42浏览次数:23  
标签:listheader 页脚 -------------------- 页眉 ls EXPORTING ALV CALL document

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
"--------------------@斌将军--------------------

 

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合并单元格的介绍,希望对您有所帮助。

定期更文,欢迎关注

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:listheader,页脚,--------------------,页眉,ls,EXPORTING,ALV,CALL,document
From: https://www.cnblogs.com/BinGeneral/p/18155984

相关文章

  • GraalVM-云原生时代的JVM(Java)
    一、GraalVM是什么?GraalVM是Oracle开源的一款通用虚拟机产品,官方称之为UniversalGraalVM,是新一代的通用多语言高性能虚拟机。它可以运行多种编程语言,如Java、JavaScript、Python等,并提供了即时编译(JIT)和AOT编译(AOT)的支持。GraalVM还支持在不同语言之间互相调用,以及嵌入到其他......
  • graalvmjs 24.0.1 nodejs 镜像
    graalvmjs已经支持jdk22了,基于官方提供的更新了新版本的docker镜像,方便使用DockerfileFROMdebian:bullseye-backportsLABELauthor="rongfengliang"LABELemail="[email protected]"WORKDIR/opt/RUN/bin/cp/usr/share/zoneinfo/Asia/Shanghai/etc......
  • ALV合并单元格
    1、文章说明 在开发一些报表时,需要显示双层的标题,或者合并单元格的数据,归根结底就是要实现类似EXCEL合并单元格的需求。如图所示网上的资料,很多根据国外某大神的方法实现:https://tricktresor.de/blog/zellen-verbinden/本文章在测试该方法时,发现了一些问题,并根据个人实现习惯......
  • function ALV 获取OO ALV event ID
    SAPABAPALV(LVC)的一个自定义事件(F4帮助事件,回车ENTER按钮事件)的一个实例https://blog.csdn.net/zhongguomao/article/details/51775112 1.定义和注册事件接受器类*----------------------------------------------------------------------**CLASSLCL_EVENT_RECE......
  • OO ALV 强制触发 data_change事件
    参考博客https://community.sap.com/t5/application-development-blog-posts/trigger-alv-data-change-event-manual/ba-p/13003658https://blog.csdn.net/qq_40977705/article/details/104599677https://blog.csdn.net/qq_40977705/article/details/108880331data_change写l......
  • spring-LocalVariableTableParameterNameDiscoverer
    记录一下后期整理注:此工具类是解析class文件从class文件获取,而不是通过元空间的class对象的method获取 /***在jdk8以前java源码编译后通过反射是无法获得形参名的,在Java8及之后,编译的时候可以通过-parameters为反射生成元信息,可以获取到方法的参数名,但这......
  • OO ALV滚动条问题
    问题界面 调整效果  注意需要开垂直水平滚动 垂直行不宜过大,可以自己试 我这边是垂直参数20-24行即可  ......
  • VisualVM v2.1.8汉化预览版
     下载地址:v2.1.8-1预览版·Yfeil/VisualVM汉化-Gitee.com......
  • 学习Source Generators之IncrementalValueProvider
    前面我们使用了IIncrementalGenerator来生成代码,接下来我们来详细了解下IIncrementalGenerator的核心部分IncrementalValueProvider。介绍IncrementalValueProvider是基于管道的模式,将我们需要的数据进行处理转换后传递给SourceOutput。目前官方提供可用的Providers有如下几种:......
  • ABAP 简单ALV 按钮+隐藏和显示
    结果代码REPORT ZGUO_TEST.*调用数据表TABLES:SPFLI.*定义、申明变量TYPE-POOLS:SLIS.DATA:GT_FIELDCATTYPELVC_T_FCAT,GS_FIELDCATTYPELVC_S_FCAT,ITEMTYPEI,   LS_LAYOUT TYPELVC_S_LAYO,   GT_EVENT  TYPESLIS_T_EVENTWITHHEADER......