记录SAP开发时写的包含一些基础知识的程序例子
效果图1:
效果图2:
文件总览:
逻辑流:屏幕100和200的逻辑流是一样的,调用的同一个PBO和同一个PAI(详见PBO和PAI代码)
100屏幕元素:【功能码】刷新的为“REFRESH”,添加信息的为“ADD”,其他按钮的功能码跟名称一样
200屏幕屏幕元素:【功能码】显示信息列表为“LIST”,批量导入为“BATIN”(此功能本文未贴代码,读者可以自行删除“批量导入”按钮),确定添加为“INS”
性别单选按钮组参数:
状态栏参数:
主文件代码:
*&---------------------------------------------------------------------*
*& Report ZQZ01
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report zqz01.
include zqz01top.
include zqz01f01.
include zqz01pbo.
include zqz01pai.
start-of-selection.
call screen 200.
top代码:
*&---------------------------------------------------------------------*
*& 包含 ZQZ01TOP
*&---------------------------------------------------------------------*
*功能码定义
data: ok_code type sy-ucomm,
save_ok type sy-ucomm.
*数据内表和结构的定义
data: it_tab1 type table of zqzstd,
wa_tab1 like zqzstd.
*alv表格对象、容器对象、屏幕名定义
data: alv_grid type ref to cl_gui_alv_grid,
custom_container type ref to cl_gui_custom_container,
g_name type scrfname value 'ALVDATA'.
*数据录入参数定义
data: stno type zqzstd-stno,
name type zqzstd-name,
age type zqzstd-age,
sex type zqzstd-sex,
birthday type zqzstd-birthday.
*性别单选项参数定义
data s1 type c value 'X'.
data s2 type c.
*将数据和复选框定义为新的类型
types: begin of gs_tab1.
types: checkbox type c.
include structure zqzstd.
types: end of gs_tab1.
*定义带复选框新类型的内表和结构
data: gt_tab1 type gs_tab1 occurs 0 with header line.
*& gs_tab1 不是表类型,所以不能直接使用with header line,需要加occurs 0.
data: gw_tab1 like gt_tab1.
*定义布局和字段目录参数
data: gs_layout type lvc_s_layo,
gt_fieldcat type lvc_t_fcat.
*定义打印用的内表和结构
data: it_print type table of zqzstd,
wa_print like zqzstd.
f01代码:
*&---------------------------------------------------------------------*
*& 包含 ZQZ01F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& insert函数
*&---------------------------------------------------------------------*
form insert.
wa_tab1-stno = stno.
wa_tab1-name = name.
wa_tab1-age = age.
loop at screen.
if s1 = 'X' and screen-group1 = 'G1'.
sex = 'M'.
elseif s2 = 'X' and screen-group1 = 'G1'..
sex = 'F'.
endif.
endloop.
wa_tab1-sex = sex.
wa_tab1-birthday = birthday.
insert into zqzstd values wa_tab1.
if sy-subrc <> 0.
message i001(00) with '添加数据失败!'.
else.
message s001(00) with '添加数据成功!'.
append wa_tab1 to it_tab1.
refresh gt_tab1.
loop at it_tab1 into wa_tab1.
move-corresponding wa_tab1 to gt_tab1.
append gt_tab1.
endloop.
endif.
endform.
*&---------------------------------------------------------------------*
*& delete函数
*&---------------------------------------------------------------------*
form delete changing gt_tab1 type standard table.
data: it_del type table of zqzstd,
wa_del like zqzstd.
loop at gt_tab1 into gw_tab1.
if gw_tab1-checkbox = 'X'.
wa_del-stno = gw_tab1-stno.
wa_del-name = gw_tab1-name.
wa_del-age = gw_tab1-age.
wa_del-sex = gw_tab1-sex.
wa_del-birthday = gw_tab1-birthday.
append wa_del to it_del.
* 将工作区里的数据append进要删除的数据的表it_del
delete table gt_tab1 from gw_tab1.
endif.
endloop.
delete zqzstd from table it_del.
if sy-subrc <> 0.
message e001(00) with '删除操作失败'.
else.
message s001(00) with '删除操作成功'.
endif.
endform.
*&---------------------------------------------------------------------*
*& 字段目录创建函数
*&---------------------------------------------------------------------*
form build_fieldcat changing pt_fieldcat type lvc_t_fcat.
data ls_fcat type lvc_s_fcat.
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'ZQZSTD'
changing
ct_fieldcat = pt_fieldcat.
* ls_fcat-checkbox = &1.
* ls_fcat-edit = &2.
* ls_fcat-outputlen = &3.
* ls_fcat-fieldname = &4.
* ls_fcat-seltext = &5.
define add_field.
CLEAR ls_fcat.
ls_fcat-checkbox = &1.
ls_fcat-edit = &2.
ls_fcat-outputlen = &3.
ls_fcat-fieldname = &4.
ls_fcat-coltext = &5.
*& 半自动创建fieldcat中coltext才是自定义自段
APPEND ls_fcat TO pt_fieldcat .
end-of-definition.
add_field 'X' 'X' 10 'CHECKBOX' '复选框'.
* add_field '' '' 10 'STNO' '编号'.
* add_field '' '' 10 'NAME' '姓名'.
* add_field '' '' 5 'AGE' '年龄'.
* add_field '' '' 5 'SEX' '性别'.
* add_field '' '' 10 'BIRTHDAY' '生日'.
endform.
*&---------------------------------------------------------------------*
*& 获取数据函数
*&---------------------------------------------------------------------*
form build_data.
data l_index type i.
select * into table it_tab1 from zqzstd.
loop at it_tab1 into wa_tab1.
move-corresponding wa_tab1 to gt_tab1.
append gt_tab1.
endloop.
endform.
*&---------------------------------------------------------------------*
*& 表单打印函数
*&---------------------------------------------------------------------*
form print.
data: fname type tdsfname,
control_parameters type ssfctrlop,
output_options type ssfcompop.
call function 'SSF_FUNCTION_MODULE_NAME'
exporting
formname = 'ZQZSTD01'
importing
fm_name = fname.
call function fname
exporting
control_parameters = control_parameters
output_options = output_options
tables
it_tab = it_print.
endform.
*&---------------------------------------------------------------------*
*& 全选函数
*&---------------------------------------------------------------------*
form sel_all changing gt_tab1 type standard table.
loop at gt_tab1 into gw_tab1.
if gw_tab1-checkbox is initial or gw_tab1-checkbox eq '-'.
gw_tab1-checkbox = 'X'.
modify gt_tab1 from gw_tab1.
endif.
endloop.
call method alv_grid->refresh_table_display.
endform.
*&---------------------------------------------------------------------*
*& 取消全选函数
*&---------------------------------------------------------------------*
form desel_all changing gt_tab1 type standard table.
loop at gt_tab1 into gw_tab1.
if gw_tab1-checkbox eq 'X'.
gw_tab1-checkbox = ' '.
modify gt_tab1 from gw_tab1.
endif.
endloop.
call method alv_grid->refresh_table_display.
endform.
*&---------------------------------------------------------------------*
*& 反选函数
*&---------------------------------------------------------------------*
form inv changing gt_tab1 type standard table.
loop at gt_tab1 into gw_tab1.
if gw_tab1-checkbox eq 'X'.
gw_tab1-checkbox = ' '.
modify gt_tab1 from gw_tab1.
elseif gw_tab1-checkbox is initial or gw_tab1-checkbox eq '-'.
gw_tab1-checkbox = 'X'.
modify gt_tab1 from gw_tab1.
endif.
endloop.
call method alv_grid->refresh_table_display.
endform.
pai代码:
*&---------------------------------------------------------------------*
*& 包含 ZQZ01PAI
*&---------------------------------------------------------------------*
module user_command_0100 input.
call method alv_grid->check_changed_data.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'EXIT'.
leave program.
when 'INS'.
perform insert.
when 'LIST'.
leave to screen 100.
when 'ADD'.
call screen 200.
when 'PRINT'.
refresh it_print.
loop at gt_tab1[] into gt_tab1.
if gt_tab1-checkbox = 'X'.
wa_print-stno = gt_tab1-stno.
wa_print-name = gt_tab1-name.
wa_print-age = gt_tab1-age.
wa_print-sex = gt_tab1-sex.
wa_print-birthday = gt_tab1-birthday.
append wa_print to it_print.
endif.
endloop.
perform print.
when 'SEL_ALL'.
perform sel_all changing gt_tab1[].
when 'DESEL_ALL'.
perform desel_all changing gt_tab1[].
when 'INV'.
perform inv changing gt_tab1[].
when 'DEL'.
perform delete changing gt_tab1[].
when 'MOD'.
when 'BATIN'.
call transaction 'ZQZT016'.
when 'REFRESH'.
select * from zqzstd into table it_tab1.
call method alv_grid->refresh_table_display.
endcase.
endmodule.
pbo代码:
*&---------------------------------------------------------------------*
*& 包含 ZQZ01PBO
*&---------------------------------------------------------------------*
module status_0100 output.
set pf-status 'STATUS1'.
if custom_container is initial.
select * into table it_tab1 from zqzstd.
create object custom_container
exporting
container_name = g_name.
create object alv_grid
exporting
i_parent = custom_container.
perform build_fieldcat changing gt_fieldcat.
perform build_data.
gs_layout-cwidth_opt = 'X'.
call method alv_grid->set_table_for_first_display
exporting
i_structure_name = 'ZQZSTD'
is_layout = gs_layout
* i_callback_user_command = 'USER_COMMAND_0100'
changing
it_fieldcatalog = gt_fieldcat
it_outtab = gt_tab1[].
endif.
call method alv_grid->refresh_table_display.
endmodule.
表结构:
smartforms参数:
总览:
用LOOP循环将从程序传过来的内表IT_TAB读到工作区WA_TAB
点击下图中红框框住的按钮,显示字段列表:
字段列表:
将字段拖入到文本里面
在下图的“输出结构”中设置该文本在模板中的位置(第几行第几列)
表格设计器
模板: