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'EXPORTINGit_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'EXPORTINGit_list_commentary = lt_listheaderi_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'EXPORTINGi_list_type = 0IMPORTINGet_events = gt_eventEXCEPTIONSlist_type_wrong = 1OTHERS = 2.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgtyNUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF."查找TOP_OF_PAGE事件,并将回调子例程传输给FORMREAD 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事件,并将回调子例程传输给FORMREAD 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_insertEXPORTINGcontents = lv_bufferCHANGINGposition = lv_pos.lv_text = '标题'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_style = 'HEADING'."插入两行空行CALL METHOD document->new_line.CALL METHOD document->add_iconEXPORTINGsap_icon = 'ICON_CUSTOMER'."STRONG是加粗lv_text = '用户名:'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_emphasis = 'Strong'."间隔CALL METHOD document->add_gapEXPORTINGwidth = 2.lv_text = '斌将军'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_fontsize = cl_dd_document=>largesap_fontstyle = cl_dd_document=>serifsap_emphasis = cl_dd_document=>emphasis."间隔CALL METHOD document->add_gapEXPORTINGwidth = 10.CALL METHOD document->add_iconEXPORTINGsap_icon = 'ICON_DATE'.lv_text = '日期:'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_emphasis = 'Strong'.CALL METHOD document->add_gapEXPORTINGwidth = 2.lv_text = '20240420'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_style = 'Key'.CALL METHOD document->add_gapEXPORTINGwidth = 10.CALL METHOD document->add_iconEXPORTINGsap_icon = 'ICON_INFORMATION'.lv_text = '链接:'.CALL METHOD document->add_textEXPORTINGtext = lv_textsap_emphasis = 'Strong'.CALL METHOD document->add_linkEXPORTINGurl = '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_9000EXPORTINGrepid = sy-repiddynnr = '9000'side = cl_gui_docking_container=>dock_at_top "ALV贴屏幕左边,从左边算屏幕宽度,extension = 1000"屏幕宽度style = cl_gui_control=>ws_child "可选参数,设置ALV是否可用手动拖动大小EXCEPTIONScntl_error = 1cntl_system_error = 2create_error = 3lifetime_error = 4lifetime_dynpro_dynpro_link = 5OTHERS = 6.IF sy-subrc <> 0.MESSAGE s001(00) WITH '屏幕初始化失败'.LEAVE LIST-PROCESSING.ENDIF."拆分成3行1列部分CREATE OBJECT g_splitterEXPORTINGparent = g_container_9000rows = 3columns = 1."第一行第一列 "用作页眉CALL METHOD g_splitter->get_containerEXPORTINGrow = 1column = 1RECEIVINGcontainer = g_container_t.CALL METHOD g_splitter->set_row_height "设置行高EXPORTINGid = 1 "第几行height = 20."第二行第一列 用作展示主要数据CALL METHOD g_splitter->get_containerEXPORTINGrow = 2column = 1RECEIVINGcontainer = g_container_m."第三行第一列 "用作页脚CALL METHOD g_splitter->get_containerEXPORTINGrow = 3column = 1RECEIVINGcontainer = g_container_e.CALL METHOD g_splitter->set_row_height "设置行高EXPORTINGid = 3 "第几行height = 20.CREATE OBJECT g_grid "中间部分为展示主数据EXPORTINGi_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_gridIMPORTING e_dyndoc_id.METHODS handle_end_of_list FOR EVENT end_of_list OF cl_gui_alv_gridIMPORTING 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_displayEXPORTINGi_save = 'A'i_default = 'X' "允许定义默认布局is_layout = gs_layoutCHANGINGit_outtab = gt_alvit_fieldcatalog = gt_fieldcatEXCEPTIONSinvalid_parameter_combination = 1program_error = 2too_many_lines = 3OTHERS = 4.IF sy-subrc <> 0.ENDIF.CREATE OBJECT g_dyndoc_tEXPORTINGstyle = '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_eventsEXPORTINGi_event_name = 'TOP_OF_PAGE'i_dyndoc_id = g_dyndoc_t.CREATE OBJECT g_dyndoc_eEXPORTINGstyle = 'ALV_GRID'."对于页脚使用END_OF_LIST事件CALL METHOD g_grid->list_processing_eventsEXPORTINGi_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_insertEXPORTINGcontents = lv_bufferCHANGINGposition = lv_pos."页脚HTML视图IF g_html_e IS INITIAL.CREATE OBJECT g_html_eEXPORTINGparent = g_container_e.ENDIF.CALL METHOD document->merge_document.document->html_control = g_html_e."显示CALL METHOD document->display_documentEXPORTINGreuse_control = 'X'parent = g_container_eEXCEPTIONShtml_display_error = 1.
ENDFORM.
"--------------------@斌将军--------------------
参考代码,可通过链接获取
链接:https://pan.baidu.com/s/18bNmfmC9zSh1Si8W6AhQCQ?pwd=c0oj
提取码:c0oj
5、总结
以上就是关于ALV合并单元格的介绍,希望对您有所帮助。
定期更文,欢迎关注