开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第5天,点击查看活动详情
HELLO, 这里是百里一个学习中的ABAPER,这里记录工作学习中遇到的bug,知识内容等内容.今天讲的是在工作中经常会使用的数据下钻,通过跳出小界面的方式展示关联数据.今天结合工作经验这里做下技术总结.
在ALV报表展示中.会出现关键字段下钻,展示某个界面或者系统自带界面.此时需要给增加 user_command
事件. 下钻内容包括,展示具体某个界面,系统界面,与关键字对应的ALV界面.
下钻弹窗数据展示的关键技术为调用函数 cl_salv_table=>factory
展示一个新的ALV容器数据. 首先先跟本身第一层开发ALV方式一样, 定义默认基础数据集,权限检查,定义ALV layout ,fieldcat 等相关属性内容. 同时设定对应的staus 及usercommd 方便操作用户事件.设定第一层ALV展示数据的基础上,设定hot字段,在按照关键字段关联对应下一级ALV展示数据.
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = go_alv "导出ALV的容器对象
CHANGING t_table = lt_item[] ). "内容表
CATCH cx_root INTO cxroot.
excmsg = cxroot->get_text( ).
MESSAGE e000(oo) WITH excmsg.
ENDTRY.
go_funlst = go_alv->get_functions( ).
go_funlst->set_all( 'X' ).
此例子第一层为VBAK 表 VBELN 字段,第二层内容为对应VBELN 单号对应的 料号数据及料号长描述例子.
设定ALV 定义相关结构,工作区内表.同时定义必要字段如ALV展示相关变量.usercomed 变量.格式及样式变量字段.
TYPE-POOLS: slis.
TYPE-POOLS: kcde.
*&---------------------------------------------------------------------*
*& TABLES
*&---------------------------------------------------------------------*
TABLES: vbap,vbak.
*----------------------------------------------------------------------*
* GLOBAL INTERNAL TABLES DECLARATION
*----------------------------------------------------------------------*
DATA: gt_file TYPE filetable.
*----------------------------------------------------------------------*
* GLOBAL VARIANTS DECLARATION
*----------------------------------------------------------------------*
DATA: g_repid TYPE sy-repid.
DATA: gt_field TYPE slis_t_fieldcat_alv.
*定义读入EXCEL的内表
TYPES : BEGIN OF typ_alv ,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat, "创建日期
vkorg TYPE vbak-vkorg, "销售组织
vkbur TYPE vbak-vkbur, "销售办事处
ernam TYPE vbak-ernam, "创建人
sel TYPE c , " 复选框
END OF typ_alv .
DATA : gs_alv TYPE typ_alv,
gt_print TYPE TABLE OF typ_alv,
gt_alv TYPE TABLE OF typ_alv.
* ALV 用
DATA:
gs_layout TYPE lvc_s_layo,
gv_repid TYPE repid,
gt_fieldcat TYPE lvc_t_fcat,
ls_fieldcat TYPE lvc_s_fcat.
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
*创建字段宏定义
DEFINE add_field.
ls_fieldcat-fieldname = '&1' . "字段名称
ls_fieldcat-ref_table = &2. "关联表格
ls_fieldcat-ref_field = &3."参考字段
ls_fieldcat-coltext = &4. "描述文本
APPEND ls_fieldcat TO gt_fieldcat.
CLEAR ls_fieldcat.
END-OF-DEFINITION.
筛选关键字段,where 条件.
*--------选择条件
SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:
s_vkorg FOR vbak-vkorg OBLIGATORY,"销售组织
s_vkbur FOR vbak-vkbur ,"销售办公室
s_ernam FOR vbak-ernam."创建人
SELECTION-SCREEN END OF BLOCK bl01.
INITIALIZATION .
AT SELECTION-SCREEN OUTPUT .
*----------------------------------------------------------------------*
* EVENT OCCURS AFTER THE SELECTION SCREEN HAS BEEN PROCESSED
*----------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM frm_auth_check. "权限检查
PERFORM frm_getdata . "获取数据 --GETTING DATA
PERFORM frm_display_data. "展示ALV
END-OF-SELECTION.
*&------------------
FORM frm_getdata .
SELECT vbak~vbeln,
vbak~erdat,"创建日期
vbak~vkorg,"销售组织
vbak~vkbur, "销售办事处
vbak~ernam
FROM vbak
WHERE vbak~vkorg IN @s_vkorg
AND vbak~vkbur IN @s_vkbur
AND vbak~ernam IN @s_ernam
INTO CORRESPONDING FIELDS OF TABLE @gt_alv.
ENDFORM.
FORM frm_display_data .
DATA : lv_html_header TYPE slis_formname .
*定义输出模式
CLEAR:gs_layout,gt_fieldcat.
gs_layout-cwidth_opt = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fname = 'SEL'.
gv_repid = sy-repid.
PERFORM frm_set_fieldcat.
.
* TRY .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = gv_repid
i_callback_user_command = 'FRM_USER_COMMAND'
i_callback_pf_status_set = 'FRM_PF_STATUS'
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
i_save = 'A'
TABLES
t_outtab = gt_alv
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.
FORM frm_set_fieldcat .
add_field:
vbeln 'VBAK' 'VBELN' '销售订单',
erdat 'VBAK' 'ERDAT' '创建日期',
vkorg 'VBAK' 'VKORG' '销售组织',
vkbur 'VBAK' 'VKBUR' '销售办事处',
ernam 'VBAK' 'ERNAM' '创建人'.
ENDFORM.
FORM frm_pf_status USING extab TYPE slis_t_extab.
DATA: fcode TYPE TABLE OF sy-ucomm.
CLEAR:fcode[].
SET PF-STATUS 'STANDARD' EXCLUDING fcode[] .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_USER_COMMAND
*&---------------------------------------------------------------------*
* 自定义用户动作
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
*自定义用户动作
FORM frm_user_command USING p_ucomm TYPE sy-ucomm rs_selfield TYPE slis_selfield.
DATA : l_ucomm TYPE sy-ucomm .
IF g_grid IS INITIAL.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
ENDIF.
"失去焦点触发更新内表
CALL METHOD g_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
CALL METHOD g_grid->check_changed_data.
rs_selfield-refresh = 'X'.
rs_selfield-col_stable = 'X'.
rs_selfield-row_stable = 'X'.
l_ucomm = p_ucomm .
CLEAR p_ucomm .
DATA : l_uname TYPE sy-uname,
l_datum TYPE sy-datum,
l_uzeit TYPE sy-uzeit.
CASE l_ucomm.
WHEN '&IC1'. "双击
READ TABLE gt_alv INDEX rs_selfield-tabindex INTO DATA(wa_sel_temp). "双击行
IF sy-subrc = 0 .
* SET PARAMETER ID 'VL' FIELD WA_SEL_TEMP-VBELN .
* CALL TRANSACTION 'VL03N' AND SKIP FIRST SCREEN ."SKIP FIRST SCREEN 忽略SELECTION SCREEN
PERFORM displat_item USING wa_sel_temp-vbeln.
ENDIF.
CLEAR:wa_sel_temp.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& FORM DISPLAT_ITEM
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
FORM displat_item USING p_vbeln.
DATA: go_alv TYPE REF TO cl_salv_table,
go_funlst TYPE REF TO cl_salv_functions_list.
DATA: cxroot TYPE REF TO cx_root,
excmsg TYPE c.
CLEAR:go_alv,go_funlst.
SELECT vbap~vbeln,
vbap~posnr,
vbap~matnr,
vbap~ZMENG,
makt~maktx
FROM vbap
LEFT JOIN makt ON makt~matnr EQ vbap~matnr AND makt~spras = '1'
WHERE vbeln = @p_vbeln
INTO TABLE @DATA(lt_item).
IF sy-subrc = 0.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv "导出ALV的容器对象
CHANGING
t_table = lt_item[] ). "内容表
CATCH cx_root INTO cxroot.
excmsg = cxroot->get_text( ).
MESSAGE e000(oo) WITH excmsg.
ENDTRY.
go_funlst = go_alv->get_functions( ).
go_funlst->set_all( 'X' ).
"重新设置列名称
go_alv->get_columns( )->get_column( 'MAKTX' )->set_long_text( 'TES删除' )."
IF go_alv IS BOUND."设置窗口大小与位置-
go_alv->set_screen_popup(
start_column = 30
end_column = 160
start_line = 5
end_line = 20 ).
go_alv->display( ).
ENDIF.
ENDIF.
ENDFORM.
如图展示内容结果,一级对应的字段为,VBAK 表中的 销售订单,创建日期,销售组织,销售办事处,创建人等字段, 通过usercomed ,双击数据VBELN下钻到下一层ALV数据. 下层ALV数据展示内容为 销售订单凭证,行号,料号,目标数量,同时可以使用 go_alv->get_columns( )->get_column( 'MAKTX' )->set_long_text( 'TES删除' )."
给对应的字段附加更换列名.
ALV数据下钻是在工作中经常使用的必要内容.能够熟练使用ALV下钻并按照关键字展示下级相关ALV数据是一个ABAPER 必会内容. 此文详细讲解了必要技术.希望能够帮助大家 .
贵有恒何必三更起五更睡,最无益只怕一日曝十日寒。 这里是百里,一个年近30的中年程序员.希望能够帮到您.