ALV功能总结
一.关于FUNCTION 'REUSE_ALV_GRID_DISPLAY'常用参数说明及用法
REPORT Z_ALV_TEST01
NO STANDARD PAGE HEADING
MESSAGE-ID ZX.
*** Global Data declear
INCLUDE Z_ALV_TEST01TOP.
INCLUDE Z_ALV_TEST01F01.
*** initial data
INITIALIZATION.
*** prepare report data
START-OF-SELECTION.
perform get_data.
perform alv_show.
*** output report
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Include Z_ALV_TEST01TOP
*&---------------------------------------------------------------------*
TABLES:MARA,MBEW,EKPO,MAKT.
SELECT-OPTIONS:S_MATNR FOR MARA-MATNR.
DATA:BEGIN OF IT_MATNR OCCURS 0,
BOX TYPE C,"用来控制输出画面左测的可选择该行,如果选中某行,则该栏位值为‘X’
MATKL LIKE MARA-MATKL,
MATNR LIKE MARA-MATNR,
MTART LIKE MARA-MTART,
MAKTX LIKE MAKT-MAKTX,
MEINS LIKE MARA-MEINS,
MBRSH LIKE MARA-MBRSH,
MENGE LIKE EKPO-MENGE,
END OF IT_MATNR.
TYPES:BEGIN OF WA_MATNR,
MATKL LIKE MARA-MATKL,
MATNR LIKE MARA-MATNR,
MTART LIKE MARA-MTART,
MAKTX LIKE MAKT-MAKTX,
MEINS LIKE MARA-MEINS,
MBRSH LIKE MARA-MBRSH,
MENGE LIKE EKPO-MENGE,
END OF WA_MATNR.
DATA:IT_MATNR_OLD LIKE IT_MATNR OCCURS 0 WITH HEADER LINE.
FIELD-SYMBOLS <MATNR> TYPE WA_MATNR.
TYPE-POOLS: slis.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
lt_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE,
it_events TYPE slis_t_event,
lt_event_exit TYPE slis_t_event_exit,
ls_event_exit TYPE slis_event_exit,
pt_top_of_page TYPE slis_t_listheader,
gs_print TYPE slis_print_alv,
gs_layout TYPE slis_layout_alv .
*&-------------------------------------------------------------------
*& Include Z_ALV_TEST01F01
*&-------------------------------------------------------------------
*& Form get_data
*&-------------------------------------------------------------------
FORM get_data .
SELECT *
INTO CORRESPONDING FIELDS OF TABLE IT_MATNR
FROM MARA
WHERE MATNR IN S_MATNR.
LOOP AT IT_MATNR.
SELECT SINGLE MENGE
INTO IT_MATNR-MENGE
FROM EKPO
WHERE MATNR = IT_MATNR-MATNR.
SELECT SINGLE MAKTX
INTO IT_MATNR-MAKTX
FROM MAKT
WHERE MATNR = IT_MATNR-MATNR
AND SPRAS = 'M'.
MODIFY IT_MATNR.
ENDLOOP.
IT_MATNR_OLD[] = IT_MATNR[].
ENDFORM. " get_data
*&-------------------------------------------------------------------
*& Form set_pf_status
*&-------------------------------------------------------------------
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'ALV_PF_STATUS'.
ENDFORM. "Set_pf_status
此FORM的作用是在标准的ALV工具栏上添加自定义的按钮
该名称必须和FUNCTION 参数I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'对应起来,即FORM的名称和变量名称要一样.
GUI STATUS 'ALV_PF_STATUS'是自己定义的,因为自定义后会把标准的ALV工具栏覆盖,所以还要把标准工具栏复制过来.
方法如下:
T-CODE :SE90 Program Library-- GUI Status
program name:SAPLKKBL GUI Status:STANDARD_FULLSCREEN 标准的ALV工具栏名称,F8运行,选择菜单栏GUI Status---COPY,最后输入你的程序名以及你自定义的GUI Status名,此例即为ALV_PF_STATUS,再添加按钮.
*&-------------------------------------------------------------------
*& Form ALV_USER_COMMAND
*&-------------------------------------------------------------------
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
form ALV_USER_COMMAND using r_ucomm like sy-ucomm
rs_selfield type slis_selfield. "响应ALV点击自定义按钮之后的事件
case r_ucomm.
when 'EXIT2'. "在set pf-status '9100'.的屏幕状态里面自定义的按钮名称
LEAVE TO SCREEN 0.
WHEN '&DATA_SAVE'. "修改屏幕字段物料描述MAKTX,此时的&DATA_SAVE是标准GUI的参数,I_CALLBACK_PF_STATUS_SET 不要进行设置
LOOP AT IT_MATNR.
READ TABLE IT_MATNR_OLD ASSIGNING <MATNR>
WITH KEY MATNR = IT_MATNR-MATNR.
CHECK IT_MATNR-MAKTX <> <matnr>-MAKTX.
UPDATE MAKT SET MAKTX = IT_MATNR-MAKTX WHERE MATNR = IT_MATNR-MATNR
AND SPRAS = 'M'.
ENDLOOP.
WHEN '&IC1'."表示双击
CHECK rs_selfield-tabindex > 0.
READ TABLE IT_MATNR INDEX rs_selfield-tabindex.
SET PARAMETER ID : 'MAT' FIELD IT_MATNR-MATNR.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
endcase.
rs_selfield-fieldname表示双击的字段名,有时候有用.
endform. "ALV_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form alv_show
*&---------------------------------------------------------------------*
FORM alv_show .
PERFORM prepare_alv_field. "设置屏幕输出拦位的描述和格式
PERFORM eventtab_build USING it_events[].
"触发ALV事件,比如TOP_OF_PAGE, ALV_END_OF_LIST, ALV_END_OF_PAGE
PERFORM build_sortcat. "设置排序和分类汇总
PERFORM build_layout. "设置输出格式
PERFORM build_eventexit."设置ALV按钮增强功能
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-cprog/SY-REPID "程式名称
i_background_id = 'ALV_BACKGROUND'
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
"如果ALV使用自定义的工具栏,如果使用系统工具栏则忽略该行
I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND' "自定义工具栏的事件
* I_CALLBACK_TOP_OF_PAGE = GT_LIST_TOP_OF_PAGE[]
I_CALLBACK_HTML_TOP_OF_PAGE = 'ALV_TOP_OF_PAGE'"自定义ALV表头
使用HTML语言对表头的输出格式进行控制,包括输出文字的大小,字体及颜色等,总之在HTML语言中能实现的在这里都能实现.具体实现见FORM ALV_TOP_OF_PAGE
当I_CALLBACK_TOP_OF_PAGE和I_CALLBACK_HTML_TOP_OF_PAGE同时设置时,只有I_CALLBACK_HTML_TOP_OF_PAGE起作用.
it_fieldcat = it_fieldcat[]"必须要设置的参数,设定输出目录
it_events = it_events[] "如果有事件触发
* i_structure_name = 'it_matnr'
i_save = 'A' "保存样式
is_layout = gs_layout "对输出样式的设定
is_print = gs_print
it_sort = lt_sort[]"如果有排序和分类汇总,需要该行
TABLES
t_outtab = it_matnr"ALV输出的内表
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " alv_show
*&---------------------------------------------------------------------*
*& Form prepare_alv_field
*&---------------------------------------------------------------------*
FORM prepare_alv_field.
DEFINE m_fieldcat.
it_fieldcat-fieldname = &1. "输出字段名,注意一定要是大写
it_fieldcat-reptext_ddic = &2. "输出显示的文字说明
* it_fieldcat-col_pos = &3. "输出的位置,即第几列
* it_fieldcat-outputlen = &4. "该显示文字所占的字长
it_fieldcat-emphasize = &3. "设置输出的颜色
it_fieldcat-edit = &4. "设置为可编辑字段,可进行修改
append it_fieldcat.
END-OF-DEFINITION.
m_fieldcat 'MATKL' '物料群組' 'C110' ''.
m_fieldcat 'MATNR' '物料' 'C210' ''.
m_fieldcat 'MTART' '物料类型' 'C310' ''.
m_fieldcat 'MBRSH' '行业领域' 'C410' ''.
m_fieldcat 'MEINS' '基本计量单位' 'C510' ''.
m_fieldcat 'MENGE' '数量' 'C610' ''.
m_fieldcat 'MAKTX' '物料描述' '' 'X'.
ENDFORM. " prepare_alv_field
*&---------------------------------------------------------------------*
*& Form ALV_TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM alv_top_of_page USING cl_dd TYPE REF TO cl_dd_document.
DATA: m_p TYPE i.
DATA: m_buff TYPE string.
*表头其实完全可以是一个html文件,自己使用html语言进行格式控制
m_buff = '<html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
*m_buff = '<left><H2>配件报废品种汇总表</H2></left>'.
m_buff = '<H4>配件报废品种汇总</H4>'.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
m_buff = sy-datum.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
m_buff = '</html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_sortcat
*&---------------------------------------------------------------------*
FORM build_sortcat .
CLEAR lt_sort. "此部分用于排序和分类汇总
MOVE 1 TO lt_sort-spos.
lt_sort-fieldname = 'MATKL'. "表示按照该栏位名来汇总
lt_sort-up = 'X'. "表示按照上面栏位升序排列
lt_sort-subtot = 'X'.
APPEND lt_sort.
CLEAR lt_sort.
lt_sort-spos = 1.
lt_sort-fieldname = 'MATNR'.
lt_sort-down = 'X'.
lt_sort-subtot = 'X'.
APPEND lt_sort.
ENDFORM. " build_sortcat
*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
FORM build_layout .
gs_layout-totals_text = 'totals'.
gs_layout-zebra = 'X'.
gs_layout-no_vline = 'X'.
gs_layout-colwidth_optimize = 'X'."自动设置宽度
gs_layout-detail_popup = 'X'.
gs_layout-detail_initial_lines = 'X'.
gs_layout-detail_titlebar = 'Detail Title Bar'.
gs_layout-box_fieldname = 'BOX'. "用来控制输出画面左测的可选择该行,如果选中某行,
则该栏位值为‘X’
ENDFORM. " build_layout
*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM TOP_OF_PAGE.
DATA: ls_line TYPE slis_listheader.
CLEAR pt_top_of_page[].
CLEAR ls_line.
ls_line-typ = 'H'.
SELECT SINGLE butxt INTO ls_line-info
FROM t001
WHERE bukrs = '3000'.
APPEND ls_line TO pt_top_of_page.
CLEAR ls_line.
ls_line-typ = 'S'.
ls_line-key = 'TITLE'.
ls_line-info = sy-title.
APPEND ls_line TO pt_top_of_page.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = pt_top_of_page.
ENDFORM.
*---------------------------------------------------------------------*
* FORM alv_end_of_list *
*---------------------------------------------------------------------*
form alv_end_of_list.
data:i_list_comments TYPE slis_t_listheader.
data:w_list_comments TYPE slis_listheader.
clear: i_list_comments.
w_list_comments-typ = 'H'. "H = Header, S = Selection, A = Action
w_list_comments-key = 'TITLE'.
w_list_comments-info = 'End of list'.
append w_list_comments to i_list_comments.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = i_list_comments.
endform.
*---------------------------------------------------------------------*
* FORM ALV_END_OF_PAGE *
*---------------------------------------------------------------------*
FORM ALV_END_OF_PAGE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_eventexit
*&---------------------------------------------------------------------*
FORM build_eventexit .
DATA c_refresh TYPE syucomm VALUE '&REFRESH'.
CLEAR ls_event_exit.
ls_event_exit-ucomm = c_refresh. " Refresh
ls_event_exit-after = 'X'.
APPEND ls_event_exit TO lt_event_exit.
ENDFORM. " build_eventexit
二.关于FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'分层显示常用参数说明及用法
REPORT Z_DAVID_ALVTEST.
TABLES : ekko,
ekpo.
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.
TYPE-POOLS: slis.
DATA: BEGIN OF headertab OCCURS 0,
ebeln LIKE ekko-ebeln,
bstyp LIKE ekko-bstyp,
bsart LIKE ekko-bsart,
statu LIKE ekko-statu,
expand TYPE xfeld,"用于设置是否可扩展,在这里必须要定义此字段
END OF headertab.
DATA: BEGIN OF itemtab OCCURS 0,
ebeln LIKE ekpo-ebeln,
ebelp LIKE ekpo-ebelp,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
menge LIKE ekpo-menge,
netpr LIKE ekpo-netpr,
peinh LIKE ekpo-peinh,
netwr LIKE ekpo-netwr,
END OF itemtab.
DATA: ls_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
ls_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE,
ls_keyinfo TYPE slis_keyinfo_alv,
ls_layout TYPE slis_layout_alv.
START-OF-SELECTION.
PERFORM readdata.
PERFORM build_fieldcat.
PERFORM build_sortcat.
PERFORM build_layout.
PERFORM build_keyinfo.
PERFORM display_alv.
*&---------------------------------------------------------------------*
*& Form readdata
*&---------------------------------------------------------------------*
FORM readdata .
SELECT ebeln bstyp bsart statu
INTO TABLE headertab
FROM ekko
WHERE ebeln IN s_ebeln.
IF NOT headertab[] IS INITIAL.
SELECT ebeln ebelp matnr werks menge netpr peinh netwr
INTO TABLE itemtab
FROM ekpo
FOR ALL ENTRIES IN headertab
WHERE ebeln = headertab-ebeln.
ENDIF.
ENDFORM. " readdata
*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
FORM build_fieldcat .
DEFINE alv_fieldcat.
ls_fieldcat-tabname = &1.
ls_fieldcat-fieldname = &2.
ls_fieldcat-ref_tabname = &3.
append ls_fieldcat.
END-OF-DEFINITION.
alv_fieldcat 'headertab' 'EBELN' 'EKKO'.
alv_fieldcat 'headertab' 'BSTYP' 'EKKO'.
alv_fieldcat 'headertab' 'BSART' 'EKKO'.
alv_fieldcat 'headertab' 'STATU' 'EKKO'.
alv_fieldcat 'itemtab' 'EBELN' 'EKPO'.
alv_fieldcat 'itemtab' 'EBELP' 'EKPO'.
alv_fieldcat 'itemtab' 'MATNR' 'EKPO'.
alv_fieldcat 'itemtab' 'WERKS' 'EKPO'.
alv_fieldcat 'itemtab' 'MENGE' 'EKPO'.
alv_fieldcat 'itemtab' 'NETPR' 'EKPO'.
alv_fieldcat 'itemtab' 'PEINH' 'EKPO'.
alv_fieldcat 'itemtab' 'NETWR' 'EKPO'.
ENDFORM. " build_fieldcat
*&---------------------------------------------------------------------*
*& Form build_sortcat
*&---------------------------------------------------------------------*
FORM build_sortcat .
DEFINE m_sort.
ls_sort-tabname = &1.
ls_sort-fieldname = &2.
ls_sort-up = &3.
append ls_sort.
END-OF-DEFINITION.
m_sort 'headertab' 'EBELN' 'X'.
m_sort 'itemtab' 'EBELP' 'X'.
ENDFORM. " build_sortcat
*&---------------------------------------------------------------------*
*& Form build_keyinfo
*&---------------------------------------------------------------------*
FORM build_keyinfo .
ls_keyinfo-header01 = 'EBELN'.
ls_keyinfo-item01 = 'EBELN'.
ls_keyinfo-item02 = 'EBELP'.
ENDFORM. " build_keyinfo
*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
FORM build_layout .
ls_layout-group_change_edit = 'X'.
ls_layout-colwidth_optimize = 'X'.
ls_layout-zebra = 'X'.
ls_layout-detail_popup = 'X'.
ls_layout-get_selinfos = 'X'.
ls_layout-expand_fieldname = 'EXPAND'.
ENDFORM. " build_layout
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
FORM display_alv .
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
* i_callback_user_command = 'USER_COMMAND'
is_layout = ls_layout
it_fieldcat = ls_fieldcat[]
it_sort = ls_sort[]
i_tabname_header = 'headertab'
i_tabname_item = 'itemtab'
is_keyinfo = ls_keyinfo
i_save = 'A'
TABLES
t_outtab_header = headertab
t_outtab_item = itemtab
EXCEPTIONS
program_error = 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.
ENDFORM. " display_alv